-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual7w_with_attention_blocking.py
More file actions
157 lines (135 loc) · 5.7 KB
/
visual7w_with_attention_blocking.py
File metadata and controls
157 lines (135 loc) · 5.7 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
157
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_tokens, qa_id, answer, q_type = zip(*batch)
return list(image), list(question), list(choices), list(answer_tokens), list(qa_id), list(answer), list(q_type)
class Visual7W(Dataset):
def __init__(self, image_dir) -> None:
super().__init__()
self.image_dir = image_dir
with open("tracing_information_flow/dataset/visual7w/filtered_dataset_visual7w.json", "r") as fp:
self.datas = json.load(fp)
def __len__(self):
return len(self.datas)
def __getitem__(self, index):
sample = self.datas[index]
qa_id = sample['qa_id']
question = sample['question']
answer_tokens = sample['answer_tokens']
answer = sample['gt_answer']
choices = sample['multiple_choices']
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_tokens, qa_id, answer, q_type
def parse_args():
parser = argparse.ArgumentParser(description="AVQA Eval")
parser.add_argument(
"--answer_path", type=str, default="results/results_cmflowinfo_visual7w"
)
parser.add_argument(
"--model_path", type=str,
)
parser.add_argument(
"--image_dir", type=str,
)
parser.add_argument(
"--batch_size", type=int, default=1
)
parser.add_argument(
"--start_idx", type=int, default=0
)
parser.add_argument(
"--n_workers", type=int, default=4
)
parser.add_argument(
"--seed", type=int, default=42
)
parser.add_argument(
"--block_types", nargs='+', type=str, default=['full_attention', 'question_to_last', 'image_to_last', 'image_to_question', 'last_to_last'], choices=['last_to_last', 'image_to_last', 'question_to_last', 'image_to_question', 'full_attention'], help="Blocking types to use"
)
parser.add_argument(
"--k", type=int, default=9, help="Number of blocking window to use"
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
os.makedirs(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)
random.seed(1)
print('Initializing Model')
model = Llama3_Vision(args.model_path)
model.eval()
print('Initialization Finished')
dataset = Visual7W(image_dir=args.image_dir)
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 = []
index = 0
with torch.no_grad():
for data in tqdm(dataloader):
images, questions, choices, answer_tokens, qa_ids, answers, q_types = data
prompts = []
if index < args.start_idx:
index += len(questions)
continue
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)
prob_layers = model.generate_multimodal_with_attention_blocking(prompts=prompts, answer_tokens=answer_tokens, images=images, max_gen_len=512, block_types=args.block_types, k=args.k)
# dict(block_type, list of probabilities) with probabilities = (B, n_layers)/(B)
sample_dict = dict()
for idx, question_id in enumerate(qa_ids):
answer_path = args.answer_path + "/" + str(question_id) + ".json"
for block_type in args.block_types:
if block_type in ['last_to_last', 'image_to_last', 'question_to_last', 'image_to_question', 'full_attention']:
sample_dict[block_type] = prob_layers[block_type][idx].tolist()
else:
raise NotImplementedError
with open(answer_path, 'w') as f:
json.dump(sample_dict, f, indent=4)
index += len(questions)