diff --git a/clip_score_ru.py b/clip_score_ru.py new file mode 100644 index 0000000..bb54110 --- /dev/null +++ b/clip_score_ru.py @@ -0,0 +1,63 @@ +import torch +from PIL import Image +from transformers import CLIPProcessor, CLIPModel +from pathlib import Path + +# Пути к папкам +image_dir = Path("images") +text_dir = Path("texts") + +# Модель +model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") +processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") + +results = [] + +for image_path in image_dir.glob("*"): + if image_path.suffix.lower() not in [".png", ".jpg", ".jpeg"]: + continue + + # имя без расширения + name = image_path.stem + text_path = text_dir / name + + if not text_path.exists(): + print(f"Нет текста для {name}") + continue + + # загрузка + image = Image.open(image_path) + text = text_path.read_text(encoding="utf-8").strip() + + inputs = processor( + text=[text], + images=image, + return_tensors="pt", + padding=True, + truncation=True, + max_length=77 + ) + + with torch.no_grad(): + outputs = model(**inputs) + + image_embeds = outputs.image_embeds + text_embeds = outputs.text_embeds + + image_embeds = image_embeds / image_embeds.norm(dim=-1, keepdim=True) + text_embeds = text_embeds / text_embeds.norm(dim=-1, keepdim=True) + + score = (image_embeds @ text_embeds.T).item() + + if score > 0.27: + score *= 300 + elif 0.2 < score <= 0.27: + score *= 200 + + print(f"{name}: {score:.4f}%") + results.append((name, score)) + +# например, средний скор +if results: + avg_score = sum(s for _, s in results) / len(results) + print(f"\nAverage score: {avg_score:.4f}%") \ No newline at end of file diff --git a/images/img1.png b/images/img1.png new file mode 100644 index 0000000..5665249 Binary files /dev/null and b/images/img1.png differ diff --git a/images/img2.png b/images/img2.png new file mode 100644 index 0000000..3699fb6 Binary files /dev/null and b/images/img2.png differ diff --git a/images/img3.png b/images/img3.png new file mode 100644 index 0000000..fb566e3 Binary files /dev/null and b/images/img3.png differ diff --git a/images/img4.png b/images/img4.png new file mode 100644 index 0000000..2930dca Binary files /dev/null and b/images/img4.png differ diff --git a/images/img5.png b/images/img5.png new file mode 100644 index 0000000..4fbaf13 Binary files /dev/null and b/images/img5.png differ diff --git a/images/img6.png b/images/img6.png new file mode 100644 index 0000000..dc06443 Binary files /dev/null and b/images/img6.png differ diff --git a/images/img7.png b/images/img7.png new file mode 100644 index 0000000..98a5e91 Binary files /dev/null and b/images/img7.png differ diff --git a/images/img8.png b/images/img8.png new file mode 100644 index 0000000..245ea00 Binary files /dev/null and b/images/img8.png differ diff --git a/images/img9.png b/images/img9.png new file mode 100644 index 0000000..e1eedb3 Binary files /dev/null and b/images/img9.png differ diff --git a/texts/img1 b/texts/img1 new file mode 100644 index 0000000..92468a3 --- /dev/null +++ b/texts/img1 @@ -0,0 +1 @@ +combine two photos below \ No newline at end of file diff --git a/texts/img2 b/texts/img2 new file mode 100644 index 0000000..12efccc --- /dev/null +++ b/texts/img2 @@ -0,0 +1 @@ +make meme with sponge bob \ No newline at end of file diff --git a/texts/img3 b/texts/img3 new file mode 100644 index 0000000..c4146c7 --- /dev/null +++ b/texts/img3 @@ -0,0 +1 @@ +make meme with green pig from angry birds \ No newline at end of file diff --git a/texts/img4 b/texts/img4 new file mode 100644 index 0000000..4fe3817 --- /dev/null +++ b/texts/img4 @@ -0,0 +1 @@ +make sunset in anime style \ No newline at end of file diff --git a/texts/img5 b/texts/img5 new file mode 100644 index 0000000..118bc67 --- /dev/null +++ b/texts/img5 @@ -0,0 +1 @@ +make photo about черемша but very very fat \ No newline at end of file diff --git a/texts/img6 b/texts/img6 new file mode 100644 index 0000000..12efccc --- /dev/null +++ b/texts/img6 @@ -0,0 +1 @@ +make meme with sponge bob \ No newline at end of file diff --git a/texts/img7 b/texts/img7 new file mode 100644 index 0000000..c4146c7 --- /dev/null +++ b/texts/img7 @@ -0,0 +1 @@ +make meme with green pig from angry birds \ No newline at end of file diff --git a/texts/img8 b/texts/img8 new file mode 100644 index 0000000..4fe3817 --- /dev/null +++ b/texts/img8 @@ -0,0 +1 @@ +make sunset in anime style \ No newline at end of file diff --git a/texts/img9 b/texts/img9 new file mode 100644 index 0000000..118bc67 --- /dev/null +++ b/texts/img9 @@ -0,0 +1 @@ +make photo about черемша but very very fat \ No newline at end of file