-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate_utils.py
More file actions
422 lines (373 loc) · 16.7 KB
/
evaluate_utils.py
File metadata and controls
422 lines (373 loc) · 16.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import json
import re
import numpy as np
def weighted_row_sum(data, third_rows, weight_col=1, start_col=2):
data = np.array(data)
m,n = data.shape
rows = slice(m-third_rows, m)
cols = slice(start_col, None)
weighted_sum = np.sum(data[rows, cols].astype(float) * data[rows, weight_col].astype(float)[:, np.newaxis], axis=0) / np.sum(data[rows, weight_col].astype(float))
weighted_sum = ['Mean',np.sum(data[rows, weight_col].astype(float))] + weighted_sum.tolist()
temp = data.tolist()
temp.append(weighted_sum)
return temp
def weighted_total(data, weight_col=1, start_col=2):
data = np.array(data)
m,n = data.shape
rows = slice(0, m)
cols = slice(start_col, None)
weighted_sum = np.sum(data[rows, cols].astype(float) * data[rows, weight_col].astype(float)[:, np.newaxis], axis=0) / np.sum(data[rows, weight_col].astype(float))
weighted_sum = ['Total',np.sum(data[rows, weight_col].astype(float))] + weighted_sum.tolist()
return weighted_sum
def box_iou(boxA, boxB):
boxA = [int(x) for x in boxA]
boxB = [int(x) for x in boxB]
xA = max(boxA[0], boxB[0])
xB = min(boxA[2], boxB[2])
yA = max(boxA[1], boxB[1])
yB = min(boxA[3], boxB[3])
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
iou = interArea / float(boxAArea + boxBArea - interArea)
return iou
def clean_string(s):
while s and (s[0] in ":[]()' ."):
s = s[1:]
while s and (s[-1] in ":[]()' ."):
s = s[:-1]
return s
def convert_if_number(answer):
if isinstance(answer, (int, float)):
return str(answer)
return answer
def remove_symbols(input_string):
if 'correct answer is:' in input_string:
input_string = input_string.split('correct answer is:')[-1]
cleaned_string = re.sub(r'[\*\n\""]', '', input_string)
return cleaned_string
def extract_options(text):
pattern = re.compile(r"\[([^\]]+)\]")
matches = pattern.findall(text)
if matches:
option_string = matches[-1]
if "'" not in option_string:
option_list = option_string.split(", ")
else:
option_list = [item.strip().strip("'") for item in option_string.split("', '")]
return option_list
return []
def compare_and_count(array_a, array_b):
count = 0
for a, b in zip(array_a, array_b):
if a == 1 and b == 1: count+=1
if a > b:count+=1
return count
def isfile(path):
return os.path.isfile(path)
def load_json_data(path):
with open(path,'r',encoding='utf-8') as json_f:
task_data = json.load(json_f)
return task_data
def save_json_data(path,data):
with open(path,'w',encoding='utf-8') as json_f:
json.dump(data,json_f,ensure_ascii=False,indent=4)
def Geneal_criterion_QA(third_task_data,MODEL=None):
ques_total_num = 0
right_num = 0
obey_insytruction = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
for q_ind, pred in enumerate(prediction):
# print(sample['image_path'])
ques_nopath = ''.join(sample['questions'][q_ind].lower().split(';')[1:])
tips = extract_options(ques_nopath)
# print(tips)
if len(tips)==0: pass
# if len(tips)!=0: print('No tips',sample['image_path'])
# print('No tips',sample['image_path'])
# print(ques_nopath)
pred = remove_symbols(pred)
ques_total_num += 1
clean_pred = clean_string(pred).lower()
options_nums = clean_pred.split("', '")
reference_q_ind = convert_if_number(reference[q_ind]).lower()
if len(options_nums)==1:
if clean_pred in ques_nopath:
obey_insytruction+=1
if clean_pred==reference_q_ind:
right_num+=1
elif reference_q_ind in clean_pred:
### filter
if reference_q_ind in tips:
tips.remove(reference_q_ind)
if not any(tip in clean_pred for tip in tips):
right_num+=1
return ques_total_num,right_num/ques_total_num,obey_insytruction/ques_total_num,0
def Grounding_criterion_QA(third_task_data,MODEL=None):
if MODEL ==None:
print('MODEL Input Lacked')
return -1
resize_model_lists = ["qwen", "internvl", "gemini","DriveMM",'ivl']
ques_total_num = 0
right_num = 0
loc_union = []
obey_insytruction = 0
PATTERN = re.compile(r'\[\s*([^\],]*\d+[^\],]*)\s*,\s*([^\],]*\d+[^\],]*)\s*,\s*([^\],]*\d+[^\],]*)\s*,\s*([^\],]*\d+[^\],]*)\s*\]')
box_num = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
for q_ind, pred in enumerate(prediction):
# print(sample['image_path'])
ques_total_num += 1
ques_nopath = ''.join(sample['questions'][q_ind].lower().split(';')[1:])
if 'located in the image?' in ques_nopath:
matches = PATTERN.findall(pred)
cleaned_matches = [[float(re.sub(r'[^0-9.]', '', part)) for part in match] for match in matches]
if len(matches)==1:
box_num+=1
obey_insytruction+=1
predict_bbox = cleaned_matches[0]
else:
predict_bbox = [0.0, 0.0, 0.0, 0.0]
if sum(predict_bbox) <4:
predict_bbox = [x * 1000 for x in predict_bbox]
if any(mn.lower() in MODEL.lower() for mn in resize_model_lists):
bbox_gt = sample['reference'][q_ind]
width,height = sample['dimension']
bbox_gt = [int(1000*bbox_gt[0]/width), int(1000*bbox_gt[1]/height), int(1000*bbox_gt[2]/width), int(1000*bbox_gt[3]/height)]
elif MODEL =="gemini":
bbox_gt = [bbox_gt[1], bbox_gt[0], bbox_gt[3], bbox_gt[2]]
else:
bbox_gt = sample['reference'][q_ind]
iou = box_iou(predict_bbox, bbox_gt)
if iou > 0.5: right_num+=1
loc_union.append(iou)
else:
tips = extract_options(ques_nopath)
# if len(tips)==0:
# print('No tips',sample['image_path'])
# print(sample['questions'][q_ind])
pred = remove_symbols(pred)
clean_pred = clean_string(pred).lower()
options_nums = clean_pred.split("', '")
reference_q_ind = convert_if_number(reference[q_ind]).lower()
if len(options_nums)==1:
if clean_pred in ques_nopath:
obey_insytruction+=1
if clean_pred==reference_q_ind:
right_num+=1
elif reference_q_ind in clean_pred:
### filter
if reference_q_ind in tips:
tips.remove(reference_q_ind)
if not any(tip in clean_pred for tip in tips):
right_num+=1
mean_iou = sum(loc_union)/len(loc_union)
return ques_total_num, right_num/ques_total_num, obey_insytruction/ques_total_num, mean_iou
def Relation_criterion_QA(third_task_data,MODEL=None):
ques_total_num = 0
total_score = 0
obey_insytruction = 0
totol_improve_score = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
scores_list = []
for q_ind, pred in enumerate(prediction):
ques_total_num+=1
if 'corresponds to' in pred:
# pattern = r'(?<!\d)(-?\d+|[0-9]+/[0-9]+)(?!\d)'
pattern = r'corresponds to No.([+-]?\d+|[+-]?\d+/\d+)'
match = re.search(pattern, pred)
if match:
pred_num = match.group(1).split('/')
# print(pred_num)
else:
pred_num = []
elif 'corresponding to' in pred:
pattern = r"corresponding to.*is\s+(-?\d+(?:/\d+)*)"
match = re.search(pattern, pred)
if match:
pred_num = match.group(1).split("/")
else:
pred_num = []
else:
pattern = r"(-?\d+(?:/\d+)*)"
match = re.findall(pattern, pred)
if match:
obey_insytruction+=1
pred_num = match[-1].split("/")
# print(pred_num)
else:
pred_num = []
ref_num = reference[q_ind].split('/')
if any(p_num not in ref_num for p_num in pred_num):
scores_list.append(0)
continue
else:
temp = 0
# for p_num in pred_num:
# if p_num in ref_num:
# total_score += 1
# break
for p_num in pred_num:
if p_num in ref_num:
temp += 1/len(ref_num)
total_score += 1/len(ref_num)
scores_list.append(temp)
scores_list = np.array(scores_list)
scores = compare_and_count(scores_list[len(scores_list)//2:], scores_list[:len(scores_list)//2])
totol_improve_score += scores
return ques_total_num,total_score/ques_total_num,obey_insytruction/ques_total_num,totol_improve_score*2/ques_total_num
def RoadChange_criterion_QA(third_task_data,MODEL=None):
ques_total_num = 0
right_num = 0
obey_insytruction = 0
totol_improve_score = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
scores_list = []
for q_ind, pred in enumerate(prediction):
ques_nopath = ''.join(sample['questions'][q_ind].lower().split(';')[1:])
tips = extract_options(ques_nopath)
pred = remove_symbols(pred)
ques_total_num += 1
clean_pred = clean_string(pred).lower()
options_nums = clean_pred.split("', '")
reference_q_ind = convert_if_number(reference[q_ind]).lower()
if len(options_nums)==1:
if clean_pred in ques_nopath:
obey_insytruction+=1
if clean_pred==reference_q_ind:
right_num+=1
scores_list.append(1)
elif reference_q_ind in clean_pred:
### filter
if reference_q_ind in tips:
tips.remove(reference_q_ind)
if not any(tip in clean_pred for tip in tips):
right_num+=1
scores_list.append(1)
else:
scores_list.append(0)
else:
scores_list.append(0)
else:
scores_list.append(0)
scores_list = np.array(scores_list)
scores = compare_and_count(scores_list[len(scores_list)//2:], scores_list[:len(scores_list)//2])
totol_improve_score += scores
return ques_total_num,right_num/ques_total_num,obey_insytruction/ques_total_num,totol_improve_score*2/ques_total_num
def RoadSpeed_criterion_QA(third_task_data,MODEL=None):
ques_total_num = 0
right_num = 0
obey_insytruction = 0
totol_improve_score = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
scores_list = []
for q_ind, pred in enumerate(prediction):
ques_total_num+=1
pattern = r'\[\s*(-?\d+)\s*,\s*(-?\d+)\s*\]'
matches = re.findall(pattern, pred)
matches_gt = re.findall(pattern, reference[q_ind])
# print(reference[q_ind])
ref_gt = [matches_gt[0][0],matches_gt[0][1]]
# print(ref_gt)
temp = 0
if len(matches)==1:
pred_limit = [matches[0][0],matches[0][1]]
obey_insytruction+=1
for a, b in zip(ref_gt,pred_limit):
if a==b:
temp+=0.5
right_num+=temp
scores_list.append(temp)
scores_list = np.array(scores_list)
scores = compare_and_count(scores_list[len(scores_list)//2:], scores_list[:len(scores_list)//2])
totol_improve_score += scores
return ques_total_num,right_num/ques_total_num,obey_insytruction/ques_total_num,totol_improve_score*2/ques_total_num
# return ques_total_num,right_num,obey_insytruction,totol_improve_score/2
def Judge_criterion_QA(third_task_data,MODEL=None):
des_ques_total_num = 0
judge_ques_total_num = 0
des_right_num = 0
judge_right_num = 0
obey_insytruction = 0
for d_ind, sample in enumerate(third_task_data):
reference = sample['reference']
prediction = sample['prediction']
for q_ind, pred in enumerate(prediction):
ques_nopath = ''.join(sample['questions'][q_ind].lower().split(';')[1:])
tips = extract_options(ques_nopath)
# print(tips)
if len(tips)==0: pass
pred = remove_symbols(pred)
clean_pred = clean_string(pred).lower()
options_nums = clean_pred.split("', '")
# reference_q_ind = convert_if_number(reference[q_ind]).lower()
reference_q_ind = clean_string(convert_if_number(reference[q_ind])).lower()
if 'yes' == reference_q_ind or 'no' == reference_q_ind:
judge_ques_total_num+=1
else:
des_ques_total_num += 1
if len(options_nums)==1:
# if clean_pred in ques_nopath:
if ''.join(clean_pred.split(';') in ques_nopath:
obey_insytruction+=1
if clean_pred==reference_q_ind:
if 'yes' == reference_q_ind or 'no' == reference_q_ind:
judge_right_num+=1
else:
des_right_num+=1
elif reference_q_ind in clean_pred:
### filter
if reference_q_ind in tips:
tips.remove(reference_q_ind)
if not any(tip in clean_pred for tip in tips):
if 'yes' == reference_q_ind or 'no' == reference_q_ind:
judge_right_num+=1
else:
des_right_num+=1
else:
pass
if des_ques_total_num==0:
return (judge_ques_total_num+des_ques_total_num),des_right_num,obey_insytruction/(judge_ques_total_num+des_ques_total_num),judge_right_num/judge_ques_total_num
else:
return (judge_ques_total_num+des_ques_total_num),des_right_num/des_ques_total_num,obey_insytruction/(judge_ques_total_num+des_ques_total_num),judge_right_num/judge_ques_total_num
func_mapping = {
'Pavement_Marking': Geneal_criterion_QA,
'Traffic_Sign': Geneal_criterion_QA,
'Traffic_Light': Geneal_criterion_QA,
'Right_Of_Way': Geneal_criterion_QA,
'Light': Geneal_criterion_QA,
'Weather': Geneal_criterion_QA,
'Lane_Recognition': Geneal_criterion_QA,
'Vehicle_Status': Geneal_criterion_QA,
'Vehicle_Recognition': Grounding_criterion_QA,
'VRU_Recognition': Grounding_criterion_QA,
'Obstruction_Recognition': Grounding_criterion_QA,
'Light_Lane_Relation': Relation_criterion_QA,
'Sign_Sign_Relation': Relation_criterion_QA,
'Sign_Lane_Relation': Relation_criterion_QA,
'Lane_Change_Relation': RoadChange_criterion_QA,
'Lane_Speed_Relation': RoadSpeed_criterion_QA,
'VRU_Cutin': Judge_criterion_QA,
'Vehicle_Cutin': Judge_criterion_QA,
'VRU_Cross': Judge_criterion_QA,
'Long_Short_Parking':Geneal_criterion_QA,
'Vehicle_Bahavior': Geneal_criterion_QA,
'VRU_Bahavior': Geneal_criterion_QA,
'Key_Obsturction_Detection': Judge_criterion_QA,
'Spatial_Temporal_Reasoning': Judge_criterion_QA,
'Risk_Prediction': Judge_criterion_QA,
'Drive_Efficiency': Geneal_criterion_QA,
'Longitudinal': Geneal_criterion_QA,
'Lateral': Geneal_criterion_QA
}