Слияние с удалённым репозиторием
63
clip_score_ru.py
Normal file
|
|
@ -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}%")
|
||||
BIN
images/img1.png
Normal file
|
After Width: | Height: | Size: 2 MiB |
BIN
images/img2.png
Normal file
|
After Width: | Height: | Size: 1.2 MiB |
BIN
images/img3.png
Normal file
|
After Width: | Height: | Size: 892 KiB |
BIN
images/img4.png
Normal file
|
After Width: | Height: | Size: 1 MiB |
BIN
images/img5.png
Normal file
|
After Width: | Height: | Size: 599 KiB |
BIN
images/img6.png
Normal file
|
After Width: | Height: | Size: 1.7 MiB |
BIN
images/img7.png
Normal file
|
After Width: | Height: | Size: 1.6 MiB |
BIN
images/img8.png
Normal file
|
After Width: | Height: | Size: 830 KiB |
BIN
images/img9.png
Normal file
|
After Width: | Height: | Size: 1.9 MiB |
1
texts/img1
Normal file
|
|
@ -0,0 +1 @@
|
|||
combine two photos below
|
||||
1
texts/img2
Normal file
|
|
@ -0,0 +1 @@
|
|||
make meme with sponge bob
|
||||
1
texts/img3
Normal file
|
|
@ -0,0 +1 @@
|
|||
make meme with green pig from angry birds
|
||||
1
texts/img4
Normal file
|
|
@ -0,0 +1 @@
|
|||
make sunset in anime style
|
||||
1
texts/img5
Normal file
|
|
@ -0,0 +1 @@
|
|||
make photo about черемша but very very fat
|
||||
1
texts/img6
Normal file
|
|
@ -0,0 +1 @@
|
|||
make meme with sponge bob
|
||||
1
texts/img7
Normal file
|
|
@ -0,0 +1 @@
|
|||
make meme with green pig from angry birds
|
||||
1
texts/img8
Normal file
|
|
@ -0,0 +1 @@
|
|||
make sunset in anime style
|
||||
1
texts/img9
Normal file
|
|
@ -0,0 +1 @@
|
|||
make photo about черемша but very very fat
|
||||