-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual7w.py
More file actions
157 lines (137 loc) · 5.48 KB
/
visual7w.py
File metadata and controls
157 lines (137 loc) · 5.48 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import sys
sys.path.append('./')
import os
import json
import numpy as np
from tqdm import tqdm
from PIL import Image
import torch
from torch.utils.data import Dataset, DataLoader
import argparse
import torch
from PIL import Image
from generation import Llama3_Vision
import random
def pil_collate_fn(batch):
image, question, choices, answer, qa_id, q_type = zip(*batch)
return list(image), list(question), list(choices), list(answer), list(qa_id), list(q_type)
class Visual7W(Dataset):
def __init__(self, image_dir, annotation_file) -> None:
super().__init__()
self.image_dir = image_dir
self.datas = [] #42031
with open(annotation_file, 'r') as file:
datas = json.load(file)
for data in datas['images']:
if data['split']=='test':
self.datas.extend(data['qa_pairs'])
def __len__(self):
return len(self.datas)
def __getitem__(self, index):
sample = self.datas[index]
question = sample['question']
choices = sample['multiple_choices']
answer = sample['answer']
qa_id = sample['qa_id']
image_id = sample['image_id']
image_name = f"v7w_{image_id}.jpg"
image_path = os.path.join(self.image_dir, image_name)
image = [Image.open(image_path).convert('RGB')]
q_type = sample['type']
return image, question, choices, answer, qa_id, q_type
def parse_args():
parser = argparse.ArgumentParser(description="VISUAL7W Eval")
parser.add_argument(
"--answer_path", type=str, default="results/eval_visual7w.json"
)
parser.add_argument(
"--annotation_path", type=str,
)
parser.add_argument(
"--model_path", type=str,
)
parser.add_argument(
"--image_dir", type=str,
)
parser.add_argument(
"--batch_size", type=int, default=6
)
parser.add_argument(
"--n_workers", type=int, default=4
)
parser.add_argument(
"--seed", type=int, default=42
)
parser.add_argument("--return_tokens", action="store_true", help="Ritorna tokens predetti (default: spenta)")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
os.makedirs(os.path.dirname(args.answer_path), exist_ok=True)
for k, v in args._get_kwargs():
pad = ' '.join(['' for _ in range(25-len(k))])
print(f"{k}:{pad} {v}", flush=True)
torch.cuda.set_device(0)
torch.manual_seed(1)
np.random.seed(1)
print('Initializing Model')
model = Llama3_Vision(args.model_path)
model.eval()
print('Initialization Finished')
dataset = Visual7W(image_dir=args.image_dir, annotation_file=args.annotation_path)
dataloader = DataLoader(dataset, batch_size = args.batch_size, num_workers=args.n_workers, shuffle=False, pin_memory=True, drop_last=False, collate_fn=pil_collate_fn)
print("Starting...")
predictions = []
with torch.no_grad():
for data in tqdm(dataloader):
images, questions, choices, answers, qa_ids, q_types = data
prompts = []
for question, choice, answer in zip(questions, choices, answers):
all_options = choice + [answer]
random.shuffle(all_options)
message = [
{
"role": "user",
"content": [
{"type": "image"},
{
"type": "text",
"text": (
"Look at the image carefully and answer this visual question based on the provided choices. "
"Respond with the correct answer only. Do not include any additional text.\n "
f"Question: {question}\n "
f"Choices: \n"
f" {all_options[0]}\n"
f" {all_options[1]}\n"
f" {all_options[2]}\n"
f" {all_options[3]}"
)
}
]
}
]
prompts.append(message)
for prompt in prompts:
print(f"Prompt:{prompt}\n", flush=True)
results = model.generate(prompts=prompts, images=images, max_gen_tokens=25, return_tokens=args.return_tokens)
# (B, generated_length)
if args.return_tokens:
for question, pred, qa_id, q_type, answer in zip(questions, results, qa_ids, q_types, answers):
predictions.append({
'qa_id': qa_id,
'answer': pred.tolist(),
'gt_answer': answer,
'question': question,
'q_type': q_type,
})
else:
for question, pred, qa_id, q_type, answer in zip(questions, results, qa_ids, q_types, answers):
predictions.append({
'qa_id': qa_id,
'answer': pred,
'gt_answer': answer,
'question': question,
'q_type': q_type,
})
with open(args.answer_path, 'w') as f:
json.dump(predictions, f, indent=4)