-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.py
More file actions
361 lines (287 loc) · 10.2 KB
/
baseline.py
File metadata and controls
361 lines (287 loc) · 10.2 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import argparse
import os
import json
import sys
import re
import ast
import torch
from tqdm import tqdm
from rtpt import RTPT
from utils.dataset_utils import load_data, load_dataset
from models.internvl.main import InternVLPrompter
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import PIL
from PIL import Image
import requests
from io import BytesIO
from utils.prompters import get_prompter
def plot_train_imgs(data_sample, id):
pos_imgs_paths, neg_imgs_paths, test_imgs = data_sample
image_paths = pos_imgs_paths + [test_imgs[0]] + neg_imgs_paths + [test_imgs[1]]
plot_images(image_paths, id)
def plot_images(image_paths, id):
fig, axs = plt.subplots(4, 4, figsize=(10, 15))
# axs = axs.flatten()
for i, path in enumerate(image_paths):
if i <= 6:
x = i % 2
y = i // 2
else:
x = i % 2 + 2
y = (i - 7) // 2
# Open the image
img = Image.open(path)
axs[y, x].imshow(img)
axs[y, x].set_title(f"Image {i + 1}")
# Open the image
img = Image.open(path)
axs[y, x].imshow(img)
axs[y, x].set_title(f"Image {i + 1}")
# remove axes
for ax in axs.flat:
ax.axis("off")
plt.tight_layout()
# save the figure
plt.savefig(f"results/qualitative/bongard_op_train_imgs_bp_{id}.png")
plt.show()
def parse_response(response, dict_key="rule"):
# Step 1: Try to extract Python code block (optional)
code_block = re.search(r"```python(.*?)```", response, re.DOTALL)
code = code_block.group(1).strip() if code_block else response.strip()
# Step 2: Find a dictionary assignment pattern like "x = {...}"
dict_match = re.search(r"=\s*(\{.*\})", code, re.DOTALL)
if not dict_match:
raise ValueError("No dictionary found after '=' in the response.")
dict_str = dict_match.group(1)
# Step 3: Safely parse the string into a Python dict
try:
parsed_dict = ast.literal_eval(dict_str)
except Exception as e:
raise ValueError(f"Failed to parse dictionary: {e}")
# Step 4: Return the desired key or whole dict
if dict_key in parsed_dict:
return parsed_dict[dict_key]
return parsed_dict
def eval(
data_sample,
bp_id,
prompter,
xil=False,
max_imgs=None,
think=False,
):
result = {}
pos_imgs = data_sample[0]
neg_imgs = data_sample[1]
train_imgs = pos_imgs + neg_imgs
print("NUMBER OF TRAINING IMAGES:", len(train_imgs))
prompt_path = "prompts/baseline_prompt.txt"
prompt = open(prompt_path, "r").read()
n_imgs = len(train_imgs)
prompt = prompt.replace("{n}", str(n_imgs))
prompt = prompt.replace("{m}", str(len(pos_imgs)))
prompt = prompt.replace("{o}", str(len(neg_imgs)))
pos_test_imgs = data_sample[2]
neg_test_imgs = data_sample[3]
test_imgs = pos_test_imgs + neg_test_imgs
# assert len(neg_test_imgs) == 10 or len(neg_test_imgs) == 6
all_output_tokens = 0
print("Think: ", think)
# prompt the model with the training images
if think:
print("Using thinking steps...")
raw_response = prompter.prompt_with_images(
prompt_text=prompt,
paths=train_imgs,
url=False,
max_new_tokens=32768,
use_memory=True,
thinking=think,
overwrite_memory=False, # TODO
)
else:
raw_response = prompter.prompt_with_images(
prompt_text=prompt,
paths=train_imgs,
url=False,
max_new_tokens=5000,
use_memory=True,
overwrite_memory=False,
)
print(f"Response for training images: {raw_response}")
print(f"Output TOKEN: {all_output_tokens}")
# parse the python rule from the response
try:
response = parse_response(raw_response, dict_key="rule")
print(f"Parsed rule: {response}")
except Exception as e:
print(f"Error parsing response: {e}")
response = "Error parsing response"
# use rule to predict the test images
test_prompt = f"Given the rule '{response}', determine if the image follows the rule or not. Answer with 'Yes' or 'No', nothing else."
# test_prompt = f"Given the rule \"{response}\", determine if the following image follows the rule or not. Please answer with 'Yes' or 'No'."
print(f"Test prompt: {test_prompt}")
pos_test_responses = []
for pos_test_img in pos_test_imgs:
if think:
test_response_1 = prompter.prompt_with_images(
prompt_text=test_prompt,
paths=[pos_test_img],
url=False,
max_new_tokens=32768,
use_memory=True,
thinking=think,
)
else:
test_response_1 = prompter.prompt_with_images(
prompt_text=test_prompt, paths=[pos_test_img], url=False
)
pos_test_responses.append(test_response_1)
neg_test_responses = []
for neg_test_img in neg_test_imgs:
if think:
test_response_2 = prompter.prompt_with_images(
prompt_text=test_prompt,
paths=[neg_test_img],
url=False,
max_new_tokens=32768,
use_memory=True,
thinking=think,
)
else:
test_response_2 = prompter.prompt_with_images(
prompt_text=test_prompt, paths=[neg_test_img], url=False
)
neg_test_responses.append(test_response_2)
print(f"Response for test images: ")
print(f"Positive: {pos_test_responses}")
print(f"Negative: {neg_test_responses}")
pos_correct_answers = 0
neg_correct_answers = 0
for pos_test_response in pos_test_responses:
if "yes" in pos_test_response.lower() and "no" not in pos_test_response.lower():
pos_correct_answers += 1
for neg_test_response in neg_test_responses:
if "no" in neg_test_response.lower() and "yes" not in neg_test_response.lower():
neg_correct_answers += 1
print(
f"Correct answers: {pos_correct_answers+neg_correct_answers} out of {len(test_imgs)}"
)
balanced_acc = (
pos_correct_answers / len(pos_test_imgs)
+ neg_correct_answers / len(neg_test_imgs)
) / 2
result["full_response"] = raw_response
result["rule"] = response
result["pos_test_responses"] = pos_test_responses
result["neg_test_responses"] = neg_test_responses
result["accuracy"] = balanced_acc
result["output_tokens"] = all_output_tokens
return balanced_acc, result
def main(args):
data = load_data(args.dataset, max_imgs=args.max_imgs)
prompter = get_prompter(
args.model,
args.dataset,
args.seed,
sampling=not args.no_sampling,
reasoning=args.think,
)
# Create RTPT object
rtpt = RTPT(
name_initials="XX",
experiment_name=f"Baseline_{args.dataset}_{args.model}_{args.seed}_{args.max_imgs}",
max_iterations=len(data),
)
rtpt.start()
all_accs = []
results = []
for bp_id, data_sample in enumerate(tqdm(data, desc="Evaluating Problems")):
acc, result = eval(
data[bp_id],
bp_id,
prompter,
xil=args.xil,
max_imgs=args.max_imgs,
think=args.think,
)
all_accs.append(acc)
results.append(result)
# save results to a json file
if args.xil:
results_file = f"results/qualitative/{args.dataset}/xil/direct_results_{args.model}_{args.seed}_{args.max_imgs}.json"
elif args.think:
results_file = f"results/qualitative/{args.dataset}/direct_results_{args.model}_{args.seed}_{args.max_imgs}_think.json"
if args.no_sampling:
results_file = f"results/qualitative/{args.dataset}/no_sampling/direct_results_{args.model}_{args.seed}_{args.max_imgs}_think.json"
elif args.no_sampling:
results_file = f"results/qualitative/{args.dataset}/no_sampling/direct_results_{args.model}_{args.seed}_{args.max_imgs}.json"
else:
results_file = f"results/qualitative/{args.dataset}/direct_results_{args.model}_{args.seed}_{args.max_imgs}.json"
# if file folder does not exists, create it
os.makedirs(os.path.dirname(results_file), exist_ok=True)
with open(results_file, "w") as f:
json.dump(results, f, indent=4)
rtpt.step(subtitle=f"it={bp_id}")
print(f"Average accuracy: {sum(all_accs) / len(all_accs)}")
# get std
print(f"Standard deviation: {torch.std(torch.tensor(all_accs)).item()}")
# plot times 100 and latex format
all_accs = [x * 100 for x in all_accs]
print(
f"Mean accuracy: {sum(all_accs) / len(all_accs):.2f} \\pm {torch.std(torch.tensor(all_accs)).item():.2f}"
)
# All tokens used:
tokens = prompter.get_produced_tokens()
print(f"Total tokens used: {tokens}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Evaluate Bongard Problems")
parser.add_argument(
"--model",
type=str,
default="InternVL3-8B",
help="Name of the model to use for evaluation",
)
parser.add_argument(
"--dataset",
type=str,
default="CLEVR-Hans3-unconfounded",
help="Dataset to use for evaluation",
)
parser.add_argument(
"--xil",
action="store_true",
help="Use XIL for evaluation",
)
parser.add_argument(
"--seed",
type=int,
default=0,
help="Random seed for initialization",
)
parser.add_argument(
"--limit_imgs",
action="store_true",
help="Use limited amount of imgs",
)
parser.add_argument(
"--max_imgs",
type=int,
default=6,
help="Max number of images to use for training",
)
parser.add_argument(
"--think",
action="store_true",
help="Use thinking steps (only for Ovis)",
)
parser.add_argument(
"--no_sampling",
action="store_true",
)
args = parser.parse_args()
# args.xil = True
print(f"\nSTARTING EVALUATION WITH MODEL {args.model} ON DATASET {args.dataset}\n")
torch.cuda.empty_cache()
main(args)