-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGQA_test.py
188 lines (156 loc) · 7.01 KB
/
GQA_test.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import argparse
import os
import random
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import gradio as gr
from minigpt4.common.config import Config
from minigpt4.common.dist_utils import get_rank
from minigpt4.common.registry import registry
from minigpt4.conversation.conversation import Chat, CONV_VISION
# imports modules for registration
from minigpt4.datasets.builders import *
from minigpt4.models import *
from minigpt4.processors import *
from minigpt4.runners import *
from minigpt4.tasks import *
from datasets import load_dataset
from tqdm import tqdm
from matplotlib import pyplot as plt
import json
import os
from GQA.GQADataset import GQADataset
from torch.utils.data import DataLoader
from PIL import Image
def parse_args():
parser = argparse.ArgumentParser(description="Demo")
parser.add_argument("--cfg-path", required=True, help="path to configuration file.")
parser.add_argument("--gpu-id", type=int, default=0, help="specify the gpu to load the model.")
parser.add_argument(
"--options",
nargs="+",
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file (deprecate), "
"change to --cfg-options instead.",
)
args = parser.parse_args()
return args
def setup_seeds(config):
seed = config.run_cfg.seed + get_rank()
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
cudnn.benchmark = False
cudnn.deterministic = True
# ========================================
# Model Initialization
# ========================================
print('Initializing Things')
args = parse_args()
cfg = Config(args)
model_config = cfg.model_cfg
model_config.device_8bit = args.gpu_id
model_cls = registry.get_model_class(model_config.arch)
model = model_cls.from_config(model_config).to('cuda:{}'.format(args.gpu_id))
vis_processor_cfg = cfg.datasets_cfg.cc_sbu_align.vis_processor.train
vis_processor = registry.get_processor_class(vis_processor_cfg.name).from_config(vis_processor_cfg)
print('Initialization Finished')
def get_response(input_text, input_image):
chat = Chat(model, vis_processor, device='cuda:{}'.format(args.gpu_id))
conv = CONV_VISION.copy()
image_list = []
status = chat.upload_img(input_image, conv, image_list)
chat.ask(input_text, conv)
out_text, out_token = chat.answer(conv=conv,
img_list=image_list,
num_beams= 10,
temperature=1,
max_new_tokens=200,
max_length=2000)
return out_text
dataset = GQADataset("/data/ossowski/GQA", "test", f"cuda:{args.gpu_id}")
dataloader = DataLoader(dataset, 1, shuffle=False, num_workers=2)
for i, batch in enumerate(tqdm(dataloader)):
#print(batch['path_to_image'][0])
if os.path.exists(batch['path_to_image'][0]):
img = Image.open(batch['path_to_image'][0]).convert('RGB')
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.title(batch["question"][0])
out_text = get_response(batch['question'][0], img)
plt.xlabel(f"Correct Answer: {batch['answer'][0]}\n" + f"Model Output: {out_text}")
plt.savefig(f"GQA_output/test{i}.png")
plt.close()
#description_0 = get_response("Describe the image in 1 sentence", image_0)
#description_1 = get_response("Describe the image in 1 sentence", image_1)
# for _ in range(10):
# details = []
# for i, shape in enumerate(image_1_annotations["shapes"]):
# coords = shape["points"]
# x1, y1 = list(map(int, coords[0]))
# x2, y2 = list(map(int, coords[1]))
# cropped = image_1.crop((x1, y1, x2, y2))
# plt.imshow(cropped)
# plt.savefig(f"{i}.png")
# chat = Chat(model, vis_processor, device='cuda:{}'.format(args.gpu_id))
# conv = CONV_VISION.copy()
# image_list = []
# status = chat.upload_img(cropped, conv, image_list)
# out_text = get_response("Describe this object in detail with one sentence.", cropped)
# out_text, out_token = chat.answer(conv=conv,
# img_list=image_list,
# num_beams= 10,
# temperature=1,
# max_new_tokens=50,
# max_length=2000)
# # center = f"({(x1 + x2) // 2},{(y1 + y2) // 2})"
# # dimensions = f"({abs(x2 - x1)},{abs(y2 - y1)})"
# details.append(f"At pixel {x1}, {y1} with width {abs(x2 - x1)} and height {abs(y2 - y1)}, {out_text}")
# for detail in details:
# description_0 += " " + detail
# print("-----------------------------------")
# conv = CONV_VISION.copy()
# status = chat.upload_img(image_0, conv, image_list)
# chat.ask("Describe the image in detail.", conv)
# out_text, out_token = chat.answer(conv=conv,
# img_list=image_list,
# num_beams=1,
# temperature=0.01,
# max_new_tokens=300,
# max_length=2000)
# chat_responses.append(out_text.replace("\n", "\t"))
# chat.ask(f'Which of these 2 captions is more appropriate for the image: "{caption_0}" or "{caption_1}"? Answer in one sentence.', conv)
# out_text, out_token = chat.answer(conv=conv,
# img_list=image_list,
# num_beams=1,
# temperature=0.01,
# max_new_tokens=300,
# max_length=2000)
# chat_responses.append(out_text.replace("\n", "\t"))
# responses.append(f"{i},0|||{chat_responses[0]}|||{chat_responses[1]}")
# chat = Chat(model, vis_processor, device='cuda:{}'.format(args.gpu_id))
# image_list = []
# chat_responses = []
# conv = CONV_VISION.copy()
# status = chat.upload_img(image_1, conv, image_list)
# chat.ask("Describe the image in detail.", conv)
# out_text, out_token = chat.answer(conv=conv,
# img_list=image_list,
# num_beams=1,
# temperature=0.01,
# max_new_tokens=300,
# max_length=2000)
# chat_responses.append(out_text.replace("\n", "\t"))
# chat.ask(f'Which of these 2 captions is more appropriate for the image: "{caption_0}" or "{caption_1}"? Answer in one sentence.', conv)
# out_text, out_token = chat.answer(conv=conv,
# img_list=image_list,
# num_beams=1,
# temperature=0.01,
# max_new_tokens=300,
# max_length=2000)
# chat_responses.append(out_text.replace("\n", "\t"))
# responses.append(f"{i},1|||{chat_responses[0]}|||{chat_responses[1]}")
# with open("outputs/responses.txt", "w") as f:
# for response in responses:
# f.write(f"{response}\n")