-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
44 lines (38 loc) · 1.5 KB
/
Copy pathevaluate.py
File metadata and controls
44 lines (38 loc) · 1.5 KB
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
38
39
40
41
42
43
44
from nltk.translate.bleu_score import sentence_bleu
import nltk
nltk.download('punkt')
nltk.download('punkt_tab')
from transformers import Blip2Processor, Blip2ForConditionalGeneration
from PIL import Image
import torch
import os
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained(
"Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16
)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
def generate_caption(image_path):
image = Image.open(image_path).convert("RGB")
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
generated_ids = model.generate(**inputs, max_new_tokens=50)
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
return caption
image_folder = "elc_testing_pic"
image_files = os.listdir(image_folder)
total_bleu = 0
count = 0
for image_file in image_files:
image_path = os.path.join(image_folder, image_file)
generated_caption = generate_caption(image_path)
print(f"Image: {image_file}")
print(f"Generated Caption: {generated_caption}")
reference = input("Enter reference caption for this image: ")
reference_tokens = [reference.lower().split()]
generated_tokens = generated_caption.lower().split()
bleu = sentence_bleu(reference_tokens, generated_tokens)
print(f"BLEU Score: {bleu:.4f}")
print("---")
total_bleu += bleu
count += 1
print(f"\nAverage BLEU Score: {total_bleu/count:.4f}")