-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain_plus.py
More file actions
254 lines (209 loc) · 9.34 KB
/
main_plus.py
File metadata and controls
254 lines (209 loc) · 9.34 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
from PIL import Image
from transformers import AutoModelForCausalLM, AutoProcessor, Qwen2VLForConditionalGeneration, AutoModel, AutoTokenizer, Qwen2_5_VLForConditionalGeneration, AutoModelForVision2Seq
import datasets
from datasets import load_dataset, concatenate_datasets
from tools import base64_to_image, clean_string, split_model, load_image
import os
import json
import re
import argparse
import torch
from tqdm import tqdm
from qwen_vl_utils import process_vision_info
Image.MAX_IMAGE_PIXELS = None
parser = argparse.ArgumentParser(description="ReasonMap Evaluation")
parser.add_argument(
"--model_path",
type=str,
default="moonshotai/Kimi-VL-A3B-Thinking",
help="Path to the model directory or model name from Hugging Face Hub.",
)
parser.add_argument(
"--cache_path",
type=str,
default="/mnt/data/fsc/cache",
help="Path to the cache directory.",
)
args = parser.parse_args()
model_path = args.model_path
cache_path = args.cache_path
dataset = load_dataset("FSCCS/ReasonMap-Plus", split="test")
print(f"Filtered Dataset: {dataset}")
if model_path == "moonshotai/Kimi-VL-A3B-Thinking" or model_path == "moonshotai/Kimi-VL-A3B-Instruct":
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map="auto",
trust_remote_code=True,
cache_dir=cache_path,
).eval()
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
elif model_path == "Qwen/Qwen2.5-VL-3B-Instruct" or model_path == "Qwen/Qwen2.5-VL-32B-Instruct" or model_path == "Qwen/Qwen2.5-VL-72B-Instruct" or model_path == "Qwen/Qwen2.5-VL-7B-Instruct":
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True, cache_dir=cache_path)
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map="auto",
trust_remote_code=True,
cache_dir=cache_path,
).eval()
else:
raise ValueError(f"Unsupported model path: {model_path}")
correct_count_wo_via_stops_short = 0
weighted_correct_count_wo_via_stops_short = 0
correct_count_wo_via_stops_long = 0
weighted_correct_count_wo_via_stops_long = 0
correct_count_w_via_stops = 0
for i_sample, sample in enumerate(tqdm(dataset, desc="Evaluating samples")):
country = sample["country"]
city = sample["city"]
# continue inference
model_shortname = model_path.split("/")[-1].strip()
out_dir = f"./results_plus/{country}/{city}"
filename = f"{i_sample}_{model_shortname}.json"
filepath = os.path.join(out_dir, filename)
if os.path.exists(filepath):
continue
question = sample["question"]
station1 = sample["station_1"]
station2 = sample["station_2"]
image_path = f"./maps/{sample['country']}/{sample['city']}.png"
# load metro data in json
with open(f"./stations/{country}/{city}.json", "r", encoding="utf-8") as f:
metro_data = json.load(f)
for route_name, stations in metro_data.items():
metro_data[route_name] = [
s.replace("(换乘站)", "")
.replace("(支线起始站)", "")
.replace(" (Transfer Station)", "")
.replace(" (Branch-starting Station)", "")
for s in stations
]
######################################################################################################
if model_path == "moonshotai/Kimi-VL-A3B-Thinking" or model_path == "moonshotai/Kimi-VL-A3B-Instruct":
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image_path}
] + [{"type": "text", "text": question}],
},
]
text = processor.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
image = Image.open(sample["figure"])
inputs = processor(images=[image], text=text, return_tensors="pt", padding=True, truncation=True).to("cuda")
generated_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
response = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
# token count for short response
tokens = processor.tokenizer(response, return_tensors="pt").input_ids
token_count = tokens.size(1)
print("Token count:", token_count)
final_answer = response.split("◁/think▷")[-1].strip()
elif model_path == "Qwen/Qwen2.5-VL-3B-Instruct" or model_path == "Qwen/Qwen2.5-VL-32B-Instruct" or model_path == "Qwen/Qwen2.5-VL-72B-Instruct" or model_path == "Qwen/Qwen2.5-VL-7B-Instruct" or model_path == "ChenShawn/DeepEyes-7B":
image_path = sample["figure"]
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": f"{image_path}",
},
{"type": "text", "text": question},
],
}
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],images=image_inputs,videos=video_inputs,padding=True,return_tensors="pt",
)
inputs = inputs.to("cuda")
generated_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
response = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
# token count for short response
tokens = processor.tokenizer(response, return_tensors="pt").input_ids
token_count = tokens.size(1)
print("Token count:", token_count)
final_answer = response[0]
print(f"Final Answer:\n")
print(final_answer)
print("\n")
#############################################################################################
# Evaluation pipeline
# get answer
model_answer = final_answer
if 'oxed{' not in model_answer:
for flag in ['the final answer is', 'the answer is', 'the correct answer is', 'the answer should be']:
raw_model_answer = model_answer
model_answer = model_answer.split(flag)[-1].strip()
if flag in raw_model_answer:
model_answer = model_answer.split('\n')[0].split('. ')[0]
flag = flag.replace('the', 'The')
raw_model_answer = model_answer
model_answer = model_answer.split(flag)[-1].strip()
if flag in raw_model_answer:
model_answer = model_answer.split('\n')[0].split('. ')[0]
elif model_answer.count('oxed{') > 1:
model_answer = '\\boxed{' + model_answer.split('oxed{')[-1]
from utils import find_math_answer
model_answer = find_math_answer(model_answer).rstrip('.').lstrip(':').strip()
# evaluate the answer
ground_truth = int(sample["answer"])
correct = None
if "TorF" in sample["type"]:
ground_truth = "yes" if ground_truth == 1 else "no"
correct = model_answer.lower() == ground_truth
elif sample["type"] == "Counting2" or sample["type"] == "Counting3":
try:
model_answer = int(model_answer)
correct = int(model_answer) == ground_truth
except (ValueError, TypeError):
correct = False
elif sample["type"] == "Counting1":
model_answer = model_answer.lower()
if model_answer in ['a', 'b', 'c', 'd']:
mapping = {'a': 0, 'b': 1, 'c': 2, 'd': 3}
model_answer = int(mapping[model_answer])
correct = model_answer == ground_truth
else:
correct = False
# save the question, final answer and metrics to json
model_shortname = model_path.split("/")[-1].strip()
os.makedirs(f"./results_plus/{sample['country']}/{sample['city']}", exist_ok=True)
with open(f"./results_plus/{sample['country']}/{sample['city']}/{i_sample}_{model_shortname}.json", "w", encoding="utf-8") as f:
json.dump({
"model": model_path,
"country": sample['country'],
"city": sample['city'],
"type": sample['type'],
"question": question,
"full answer": response,
"token count": token_count,
"final answer": final_answer,
"acc short": int(correct),
"difficulty city": sample['difficulty_city'],
"city line count": sample['city_line_count'],
"city transfer count": sample['city_transfer_count']}, f, ensure_ascii=False, indent=4)
# clean cache
torch.cuda.empty_cache()
# tool code for load previous answer, use for test
# with open(f"./results_plus/{sample['country']}/{sample['city']}/{i}.json", "r", encoding="utf-8") as f:
# data = json.load(f)
# final_answer = data["final answer"]
print("-" * 20)
print("Done")