Spaces:
Running
Running
File size: 898 Bytes
2c50826 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 |
from typing import Dict
import numpy as np
import torch
from PIL import Image
from torchmetrics.multimodal import CLIPImageQualityAssessment
class CLIPIQAMetric:
def __init__(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.metric = CLIPImageQualityAssessment(
model_name_or_path="clip_iqa",
data_range=255.0,
prompts=("quality",)
)
self.metric.to(self.device)
@property
def name(self) -> str:
return "clip_iqa"
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()
image_tensor = image_tensor.unsqueeze(0)
image_tensor = image_tensor.to(self.device)
scores = self.metric(image_tensor)
return {"clip_iqa": scores.item()}
|