[Feature][Hunyuan image 3.0] AR + DIT with kv reuse. (#3346)
Signed-off-by: dengyunyang <584797741@qq.com>
This commit is contained in:
1 parent
ce8a7dfd2d
commit
5b61e7f1f1
8 files changed
+1198
-135
No files matched your search
@@ -142,6 +142,40 @@ def compute_image_ssim_psnr(
|
||||
return ssim_value, psnr_value
|
||||
|
||||
|
||||
class CLIPScorer:
|
||||
def __init__(self, model_name_or_path: str = "openai/clip-vit-base-patch16"):
|
||||
from transformers import CLIPModel, CLIPProcessor
|
||||
|
||||
self._model = CLIPModel.from_pretrained(model_name_or_path)
|
||||
self._processor = CLIPProcessor.from_pretrained(model_name_or_path)
|
||||
self._model.eval()
|
||||
|
||||
def score(self, image: Image.Image, text: str) -> float:
|
||||
inputs = self._processor(text=[text], images=[image], return_tensors="pt", padding=True)
|
||||
with torch.no_grad():
|
||||
outputs = self._model(**inputs)
|
||||
img_emb = outputs.image_embeds
|
||||
txt_emb = outputs.text_embeds
|
||||
img_emb = img_emb / img_emb.norm(p=2, dim=-1, keepdim=True)
|
||||
txt_emb = txt_emb / txt_emb.norm(p=2, dim=-1, keepdim=True)
|
||||
similarity = (img_emb * txt_emb).sum(dim=-1)
|
||||
return float(similarity.item() * 100)
|
||||
|
||||
def assert_score(self, *, model_name: str, image: Image.Image, text: str, threshold: float) -> None:
|
||||
value = self.score(image, text)
|
||||
print(f"{model_name} CLIP score:")
|
||||
print(f" CLIPScore: value={value:.4f}, threshold>={threshold:.4f}, higher_is_better=True")
|
||||
assert value >= threshold, (
|
||||
f"CLIP score below threshold for {model_name}: got {value:.4f}, expected >= {threshold:.4f}."
|
||||
)
|
||||
|
||||
|
||||
def _pil_to_clip_tensor(image: Image.Image) -> torch.Tensor:
|
||||
array = np.asarray(image.convert("RGB"), dtype=np.uint8)
|
||||
tensor = torch.from_numpy(array).permute(2, 0, 1).unsqueeze(0)
|
||||
return tensor
|
||||
|
||||
|
||||
def _pil_to_batched_tensor(image: Image.Image, *, compare_mode: str) -> torch.Tensor:
|
||||
array = np.asarray(image.convert(compare_mode), dtype=np.float32) / 255.0
|
||||
tensor = torch.from_numpy(array).permute(2, 0, 1).unsqueeze(0)
|
||||
|
||||
Reference in new issue
Block a user