File size: 985 Bytes
2c50826
 
 
 
 
 
 
 
 
 
199a7d9
 
 
 
 
 
 
2c50826
 
 
 
199a7d9
2c50826
4f41410
199a7d9
2c50826
 
 
199a7d9
2c50826
199a7d9
 
 
4f41410
2c50826
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import Dict

import numpy as np
import torch
from PIL import Image
from torchmetrics.image.arniqa import ARNIQA


class ARNIQAMetric:
    def __init__(self):
        self.device = torch.device(
            "cuda"
            if torch.cuda.is_available()
            else "mps"
            if torch.backends.mps.is_available()
            else "cpu"
        )
        self.metric = ARNIQA(
            regressor_dataset="koniq10k",
            reduction="mean",
            normalize=True,
            autocast=False,
        )
        self.metric.to(self.device)

    @property
    def name(self) -> str:
        return "arniqa"

    def compute_score(self, image: Image.Image, prompt: str) -> Dict[str, float]:
        image_tensor = (
            torch.from_numpy(np.array(image)).permute(2, 0, 1).float() / 255.0
        )
        image_tensor = image_tensor.unsqueeze(0).to(self.device)
        score = self.metric(image_tensor)
        return {"arniqa": score.item()}