-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate.py
299 lines (260 loc) · 12.3 KB
/
evaluate.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
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
import re
from rouge import Rouge
import argparse
import os
import json
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
class Eval:
def __init__(self):
self.periodStrip = re.compile("(?!<=\d)(\.)(?!\d)")
self.commaStrip = re.compile("(\d)(\,)(\d)")
self.punct = [
";",
r"/",
"[",
"]",
'"',
"{",
"}",
"(",
")",
"=",
"+",
"\\",
"_",
"-",
">",
"<",
"@",
"`",
",",
"?",
"!",
]
def char(self, index):
if index < 26:
return chr(index+65)
elif index < 52:
return 'A'+chr(index+65-26)
else:
return 'B'+chr(index+65-26-26)
def processPunctuation(self, inText):
outText = inText
for p in self.punct:
if (p + " " in inText or " " + p in inText) or (
re.search(self.commaStrip, inText) != None
):
outText = outText.replace(p, "")
else:
outText = outText.replace(p, " ")
outText = self.periodStrip.sub("", outText, re.UNICODE)
return outText
def process(self, answer):
answer = answer.replace("\n", " ")
answer = answer.replace("\t", " ")
answer = answer.strip()
answer = self.processPunctuation(answer)
answer = answer.strip('\'')
answer = answer.strip('\"')
answer = answer.strip().lower()
return answer
def get_image_quantity_level(self, sample):
# 2-5 6-31 32-109
image_num = len(sample['image'])
if image_num < 6:
return 'Few'
elif image_num > 31:
return 'Many'
else:
return 'Medium'
def evaluate_rouge(self, predictions, core_json):
# get image_quantity_level
if len(predictions) != len(core_json['data']):
raise ValueError(f'There is prediction absent.')
new_pres = {d['sample_id']: d for d in predictions}
for sample in core_json['data']:
new_pres[int(sample['sample_id'])]['image_quantity_level'] = sample['image_quantity_level']
new_pres[int(sample['sample_id'])]['image'] = sample['task_instance']['images_path']
for pre in new_pres.values():
assert 'image_quantity_level' in pre.keys()
rouge = Rouge()
acc = {'f': []}
eval_list = []
image_quantity_level_cnt = {'Few': [], 'Medium': [], 'Many': []}
for i, res in enumerate(predictions):
sample_id = res['sample_id']
gt_ans = self.process(res["gt_response"])
pred_ans = self.process(res["pred_response"])
assert gt_ans != ''
if pred_ans == '':
score = 0
else:
score = rouge.get_scores(pred_ans, gt_ans)[0]['rouge-l']['f']
acc['f'].append(score)
image_quantity_level_cnt[self.get_image_quantity_level(res)].append(score)
eval_list.append({'id':str(sample_id),'score':str(round(score,3))})
return {
'Rouge-L f': np.mean(acc['f']),
'image_quantity_level-Accuracy': {k: np.mean(v) if len(v)!=0 else 0 for k, v in image_quantity_level_cnt.items()},
'image_quantity_level-Result': {k: [sum(v), len(v)] for k, v in image_quantity_level_cnt.items()}}, eval_list
def match_choice(self, text, option):
'''Return: A B C D...'''
def preprocess_option_string(option_string):
# First, preprocess the option text to normalize it
processed_option = self.process(option_string)
# Then, escape any special regex characters in the processed option text
# List of regex special characters that need to be escaped
special_chars = ["\\", ".", "^", "$", "*", "+", "?", "{", "}", "[", "]", "|", "(", ")"]
# Escape the special characters by prefixing them with a backslash
for char in special_chars:
if char in processed_option:
processed_option = processed_option.replace(char, "\\" + char)
# escaped_option = escape_special_chars(processed_option)
return processed_option
if text == "":
return 'C'
try:
# Maybe start from the head
# 1. Char+Choice: `A. Blastomycosis`
option_str = "|".join([preprocess_option_string(f"{k} {v}")for k,v in option.items()])
option_pattern = rf'({option_str})'
option_res = re.search(option_pattern, text, re.S) # NOTE we dont use match_all
if option_res:
return (option_res.group(0)[0]).upper()
# 2. Choice: `Blastomycosis`
option_str = "|".join([preprocess_option_string(v).replace(' ', '') for k,v in option.items()])
option_pattern = rf'({option_str})'
option_res = re.search(option_pattern, text.replace(' ', ''), re.S) # NOTE we dont use match_all
if option_res:
for k, v in option.items():
if option_res[0].strip() == preprocess_option_string(v).replace(' ', ''):
return k.upper()
# 3. Char: `A` `AB`
if len(text) in [1,2] and text.upper() in option.keys():
return text.upper()
# use gpt extract
except Exception as e:
print(f"something wrong during match_choice {text}: {e}")
return text
return "".join([i.upper() for i in text if i.upper() in option])
def judge_multi_choice(self, sample):
sample_id = sample['sample_id']
gt_ans = sample["gt_response"]
pred_ans = sample["pred_response"]
choice_list = sample['choice_list']
assert gt_ans in choice_list
# Convert choice_list to a dictionary format expected by match_choice
option_dict = {self.char(i): choice for i, choice in enumerate(choice_list)}
# Use match_choice to determine the selected answer from pred_ans
selected_answer = self.match_choice(pred_ans, option_dict)
# Check if the selected answer matches the ground truth
gt_ans_chr = self.char(choice_list.index(sample["gt_response"]))
if selected_answer == gt_ans_chr:
return 1, selected_answer
else:
return 0, selected_answer
def process_sample(self, sample):
sample["gt_response"] = self.process(sample["gt_response"])
sample["pred_response"] = self.process(sample["pred_response"])
for i in range(len(sample['choice_list'])):
sample["choice_list"][i] = self.process(sample["choice_list"][i])
def evaluate_multichoice(self, predictions, core_json):
'''
predictions: raw prediction file output by models
'''
# get choice_list & image_quantity_level
if len(predictions) != len(core_json['data']):
raise ValueError(f'There is prediction absent. {len(predictions)}!={len(core_json["data"])}')
new_pres = {d['sample_id']: d for d in predictions}
for sample in core_json['data']:
new_pres[int(sample['sample_id'])]['choice_list'] = sample['task_instance']['choice_list']
new_pres[int(sample['sample_id'])]['image_quantity_level'] = sample['image_quantity_level']
new_pres[int(sample['sample_id'])]['image'] = sample['task_instance']['images_path']
for pre in new_pres.values():
assert 'choice_list' in pre.keys()
assert 'image_quantity_level' in pre.keys()
correct = 0
eval_list = []
image_quantity_level_cnt = {'Few': [], 'Medium': [], 'Many': []}
for i, sample in enumerate(predictions):
# Process string
self.process_sample(sample)
# Score
score, extracted_answer = self.judge_multi_choice(sample)
sample['extracted'] = extracted_answer
sample['result'] = score
eval_list.append({'id':str(sample['sample_id']), 'score': str(score)})
correct += score
image_quantity_level_cnt[self.get_image_quantity_level(sample)].append(score)
return predictions, {
'Accuracy': correct/len(predictions),
'image_quantity_level-Accuracy': {k: np.mean(v) if len(v)!=0 else 0 for k, v in image_quantity_level_cnt.items()},
'image_quantity_level-Result': {k: [sum(v), len(v)] for k, v in image_quantity_level_cnt.items()}}, eval_list
def evaluate_needle(self, predictions, core_json, needle=True):
# get choice_list & image_quantity_level
if len(predictions) != len(core_json['data']):
raise ValueError(f'There is prediction absent. {len(predictions)}!={len(core_json["data"])}')
new_pres = {d['sample_id']: d for d in predictions}
for sample in core_json['data']:
new_pres[int(sample['sample_id'])]['image_quantity_level'] = sample['image_quantity_level']
new_pres[int(sample['sample_id'])]['image'] = sample['task_instance']['images_path']
for pre in new_pres.values():
assert 'image_quantity_level' in pre.keys()
correct = 0
eval_list = []
image_quantity_level_cnt = {'Few': [], 'Medium': [], 'Many': []}
for i, sample in enumerate(predictions):
# Process string
sample_id = sample['sample_id']
gt_ans = self.process(sample["gt_response"])
pred_ans = self.process(sample["pred_response"])
# Score
if needle:
score = 1 if gt_ans in pred_ans.split() else 0
else:
score = 1 if gt_ans in pred_ans else 0
sample['result'] = score
eval_list.append({'id':str(sample['sample_id']), 'score': str(score)})
correct += score
image_quantity_level_cnt[self.get_image_quantity_level(sample)].append(score)
return {
'Accuracy': correct/len(predictions),
'image_quantity_level-Accuracy': {k: np.mean(v) if len(v)!=0 else 0 for k, v in image_quantity_level_cnt.items()},
'image_quantity_level-Result': {k: [sum(v), len(v)] for k, v in image_quantity_level_cnt.items()}}, eval_list
def main(args):
dataset = args.dataset
result_dir = args.result_dir
model_name = result_dir.split('/')[-1]
core_annotation = json.load(open(os.path.join(args.data_dir, dataset, f'{dataset}-adv.json' if args.adv else f'{dataset}.json')))
question_type = core_annotation['meta_data']['question_type']
# Load predictions
output_dir = os.path.join(result_dir, dataset)
if not os.path.exists(os.path.join(output_dir, 'pred.json')):
raise ValueError(f'{model_name}--{dataset} No prediction file found')
preds = json.load(open(os.path.join(output_dir, 'pred.json')))
assert preds and len(preds) != 0
# Get scores
scorer = Eval()
if 'NeedleInAHaystack' in dataset or 'MMCoQA' in dataset:
eval_result, eval_list = \
scorer.evaluate_needle(preds, core_annotation, needle='NeedleInAHaystack' in dataset)
elif question_type == 'open-ended':
eval_result,eval_list = scorer.evaluate_rouge(preds, core_annotation)
elif question_type == 'multi-choice':
predictions_with_extracted_answers, eval_result, eval_list = scorer.evaluate_multichoice(preds, core_annotation)
json.dump(predictions_with_extracted_answers, open(os.path.join(output_dir, 'pred_with_extracted.json'),'w'), indent=4)
else:
raise ValueError('Dataset not supported')
print(f"{model_name}: {dataset}: {eval_result}")
json.dump(eval_result, open(os.path.join(output_dir, 'eval.json'),'w'))
json.dump(eval_list, open(os.path.join(output_dir, 'eval_score.json'),'w'), indent=4)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir', type=str, required=True)
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--result-dir', type=str, required=True)
parser.add_argument('--adv', action='store_true', help='Use adversarial data for evaluation.')
args = parser.parse_args()
main(args)