rayguan commited on
Commit
8f587e4
1 Parent(s): 37c936f

Upload 6 files

Browse files
HallusionBench.json ADDED
The diff for this file is too large to render. See raw diff
 
HallusionBench_result_sample.json ADDED
The diff for this file is too large to render. See raw diff
 
evaluation.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ from tqdm import tqdm
4
+ import numpy as np
5
+ from prettytable import PrettyTable
6
+ import os
7
+ import time
8
+ from utils import *
9
+
10
+ import openai
11
+
12
+
13
+ ### to evaluate your method, implement and run generate_answer function!
14
+
15
+ root_dir = "."
16
+ input_file_name = "HallusionBench_result.json"
17
+ save_json_path_vd = "./hallusion_output_vd_model.json"
18
+ save_json_path_vs = "./hallusion_output_vs_model.json"
19
+ # load_json = False
20
+ load_json = True
21
+ model_output_entry = "model_prediction"
22
+ model_correctness_entry = "gpt4v_output_gpt_check"
23
+
24
+
25
+ def generate_answer(data, model_output_entry):
26
+
27
+ ## TODO
28
+ ## implement this section with yout model!
29
+ ## your_function(img_filename, question) -> "0" (No), "1" (Yes), "2" (Uncertain)
30
+ # for r in data:
31
+ # r[model_output_entry] = your_function(r["filename"], r["question"])
32
+
33
+ return data
34
+
35
+
36
+ if __name__ == "__main__":
37
+
38
+ data_vd = []
39
+ data_vs = []
40
+ with open(input_file_name) as json_file:
41
+ datas = json.load(json_file)
42
+
43
+ for data in tqdm(datas):
44
+ if data['category'] == 'VD':
45
+ data_vd.append(data)
46
+ if data['category'] == 'VS':
47
+ data_vs.append(data)
48
+
49
+ data_vd = evaluate_by_chatgpt(data_vd, model_output_entry, model_correctness_entry, load_json=load_json, save_json_path=save_json_path_vd)
50
+ data_vd = check_same_by_chatgpt(data_vd, model_output_entry, load_json=load_json, save_json_path=save_json_path_vd)
51
+ #time.sleep(60) #
52
+ try:
53
+ data_vs = evaluate_by_chatgpt(data_vs, model_output_entry, model_correctness_entry, load_json=load_json, save_json_path=save_json_path_vs)
54
+ data_vs = check_same_by_chatgpt(data_vs, model_output_entry, load_json=load_json, save_json_path=save_json_path_vs)
55
+ except:
56
+ time.sleep(60)
57
+ data_vs = evaluate_by_chatgpt(data_vs, model_output_entry, model_correctness_entry, load_json=load_json, save_json_path=save_json_path_vs)
58
+ data_vs = check_same_by_chatgpt(data_vs, model_output_entry, load_json=load_json, save_json_path=save_json_path_vs)
59
+
60
+ print("##### GPT Evaluate #####")
61
+
62
+ data_vd = assign_correctness(data_vd, correctness_entry=model_correctness_entry)
63
+ data_vs = assign_correctness(data_vs, correctness_entry=model_correctness_entry)
64
+ data = data_vd + data_vs
65
+
66
+ all_data = get_eval_all(data, model_correctness_entry)
67
+ all_vd = get_eval_all(data_vd, model_correctness_entry)
68
+ all_vs = get_eval_all(data_vs, model_correctness_entry)
69
+
70
+ table1 = [["per question", "Total"],
71
+ ["VD", round(100 * all_vd["correct"]/all_vd["total"], 4)],
72
+ ["VS", round(100 * all_vs["correct"]/all_vs["total"], 4)],
73
+ ["Overall", round(100 * all_data["correct"]/all_data["total"], 4)]]
74
+ tab1 = PrettyTable(table1[0])
75
+ tab1.add_rows(table1[1:])
76
+
77
+
78
+ q_acc_gpt = round(100 * all_data["correct"]/all_data["total"], 4)
79
+
80
+ all_data = get_eval_pair_all(data, model_correctness_entry)
81
+ easy = get_eval_pair_easy(data)
82
+ hard = get_eval_pair_hard(data)
83
+ all_vd = get_eval_pair_all(data_vd, model_correctness_entry)
84
+ easy_vd = get_eval_pair_easy(data_vd)
85
+ hard_vd = get_eval_pair_hard(data_vd)
86
+ all_vs = get_eval_pair_all(data_vs, model_correctness_entry)
87
+ easy_vs = get_eval_pair_easy(data_vs)
88
+ hard_vs = get_eval_pair_hard(data_vs)
89
+ # question pair level
90
+ table3 = [["per question pair", "Easy", "Hard", "Total"],
91
+ ["VD", round(100 * easy_vd["correct"]/easy_vd["total"], 4), round(100 * hard_vd["correct"]/hard_vd["total"], 4), round(100 * all_vd["correct"]/all_vd["total"], 4)],
92
+ ["VS", round(100 * easy_vs["correct"]/easy_vs["total"], 4), round(100 * hard_vs["correct"]/hard_vs["total"], 4), round(100 * all_vs["correct"]/all_vs["total"], 4)],
93
+ ["Overall", round(100 * easy["correct"]/easy["total"], 4), round(100 * hard["correct"]/hard["total"], 4), round(100 * all_data["correct"]/all_data["total"], 4)]]
94
+ tab3 = PrettyTable(table3[0])
95
+ tab3.add_rows(table3[1:])
96
+ #print(tab3)
97
+
98
+
99
+ fig_all = get_eval_fig(data)
100
+ fig_vd = get_eval_fig(data_vd)
101
+ fig_vs = get_eval_fig(data_vs)
102
+
103
+ # image level
104
+ table2 = [["per figure", "Correct", "Wrong", "Score"],
105
+ ["VD", round(100 * fig_vd["correct"]/fig_vd["total"], 4), round(100 * fig_vd["inconsistent"]/fig_vd["total"], 4) + round(100 * fig_vd["wrong"]/fig_vd["total"], 4), round(fig_vd["score"], 4)],
106
+ ["VS", round(100 * fig_vs["correct"]/fig_vs["total"], 4), round(100 * fig_vs["inconsistent"]/fig_vs["total"], 4) + round(100 * fig_vs["wrong"]/fig_vs["total"], 4), round(fig_vs["score"], 4)],
107
+ ["Overall", round(100 * fig_all["correct"]/fig_all["total"], 4), round(100 * fig_all["inconsistent"]/fig_all["total"], 4) + round(100 * fig_all["wrong"]/fig_all["total"], 4), round(fig_all["score"], 4)]]
108
+ tab2 = PrettyTable(table2[0])
109
+ tab2.add_rows(table2[1:])
110
+
111
+ pair_acc_gpt = round(100 * all_data["correct"]/all_data["total"], 4)
112
+ figure_acc_gpt = round(100 * fig_all["correct"]/fig_all["total"], 4)
113
+ easy_acc_gpt = round(100 * easy["correct"]/easy["total"], 4)
114
+ hard_acc_gpt = round(100 * hard["correct"]/hard["total"], 4)
115
+
116
+
117
+
118
+ print("##### Question Stats #####")
119
+ print("Easy Questions: " + str(easy_vd["total_q"]) + "(Visual Dependent) + " + str(easy_vs["total_q"]) + "(Visual Supplement)")
120
+ print("Hard Questions: " + str(hard_vd["total_q"]) + "(Visual Dependent) + " + str(hard_vs["total_q"]) + "(Visual Supplement)")
121
+ print("Total Questions: " + str(all_data["total_q"]))
122
+
123
+
124
+ print("##### Figure Stats #####")
125
+ print("Visual Dependent Figures: " + str(fig_vd["total"]))
126
+ print("Visual Supplement Figures: " + str(fig_vs["total"]))
127
+ print("Total Figures: " + str(fig_all["total"]))
128
+
129
+ print("##### Leaderboard Stats #####")
130
+
131
+ table = [["", "Acc per question pair (qAcc)", "Acc per figure (fAcc)", "Acc per easy question (easy aAcc)", "Acc per hard question (hard aAcc)", "Acc per question (aAcc)"],
132
+ ["GPT Eval", pair_acc_gpt, figure_acc_gpt, easy_acc_gpt, hard_acc_gpt, q_acc_gpt]]
133
+ leaderboard = PrettyTable(table[0])
134
+ leaderboard.add_rows(table[1:])
135
+ print(leaderboard)
136
+
137
+
138
+ stats = yes_ratio_stats(data)
139
+
140
+ table = [["", "Yes/No Bias (Pct Diff)", "Yes/No Bias (FP Ratio)", "Consistency Test (correct)", "Consistency Test (inconsistent)", "Consistency Test (wrong)", "LH", "VI", "Mixed"],
141
+ ["GPT Eval", stats["diff"], stats["fp"], round(100 * fig_all["correct"]/fig_all["total"], 4), round(100 * fig_all["inconsistent"]/fig_all["total"], 4), round(100 * fig_all["wrong"]/fig_all["total"], 4), round(100 * all_data["LH_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4), round(100 * all_data["VI_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4), round(100 * all_data["Mix_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4)]]
142
+ test = PrettyTable(table[0])
143
+ test.add_rows(table[1:])
144
+ print(test)
gpt4v_benchmark.py ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ from tqdm import tqdm
4
+ import numpy as np
5
+ from prettytable import PrettyTable
6
+ import os
7
+ from utils import *
8
+
9
+ import openai
10
+
11
+ ### to evaluate your method, implement and run generate_answer function!
12
+
13
+ root_dir = "."
14
+ llava = False
15
+ # llava = True
16
+ # load_json = False
17
+ load_json = True
18
+ input_file_name = "HallusionBench.tsv"
19
+
20
+ save_json_path_vd = "./hallusion_output_vd.json"
21
+ save_json_path_vs = "./hallusion_output_vs.json"
22
+ model_output_entry = "gpt4v_output"
23
+ model_correctness_entry = "gpt4v_output_gpt_check"
24
+ model_correctness_entry_human = "gpt4v_output_human_check"
25
+
26
+ if llava:
27
+ save_json_path_vd = "./hallusion_output_vd_llava.json"
28
+ save_json_path_vs = "./hallusion_output_vs_llava.json"
29
+ model_output_entry = "llava_1_5_output"
30
+ model_correctness_entry = "llava_1_5_output_gpt_check"
31
+ model_correctness_entry_human = "llava_1_5_output_human_check"
32
+
33
+
34
+ col_idx = {
35
+ 'category':0,
36
+ 'subcategory':1,
37
+ 'visual_input':2,
38
+ 'set_id':3,
39
+ 'figure_id':4,
40
+ 'sample_note':5,
41
+ 'question_id':6,
42
+ 'question':7,
43
+ 'gt_answer_details':8,
44
+ 'gt_answer':9,
45
+ 'gpt4v_output':10,
46
+ 'gpt4v_output_human_check': 11,
47
+ 'llava_1_5_output':12,
48
+ 'llava_1_5_output_human_check': 13,
49
+ }
50
+
51
+
52
+
53
+ def generate_answer(data, model_output_entry):
54
+
55
+ ## TODO
56
+ ## implement this section with yout model!
57
+ ## your_function(img_filename, question) -> "0" (No), "1" (Yes), "2" (Uncertain)
58
+ # for r in data:
59
+ # r[model_output_entry] = your_function(r["filename"], r["question"])
60
+
61
+ return data
62
+
63
+
64
+
65
+ if __name__ == "__main__":
66
+
67
+ data_vd = []
68
+ data_vs = []
69
+ with open(input_file_name) as file:
70
+ tsv_file = csv.reader(file, delimiter="\t")
71
+ flag = 0
72
+ for line in tsv_file:
73
+ if line[0] not in ["VD", "VS"]:
74
+ continue
75
+ data_dict = {}
76
+ for k, v in col_idx.items():
77
+ data_dict[k] = line[v]
78
+
79
+ data_dict["filename"] = get_image_file_location(root_dir, data_dict)
80
+ if line[0] == "VD":
81
+ data_vd.append(data_dict)
82
+ else:
83
+ data_vs.append(data_dict)
84
+
85
+ ## TODO
86
+ data_vd = generate_answer(data_vd, model_output_entry)
87
+ data_vs = generate_answer(data_vs, model_output_entry)
88
+ ## END
89
+
90
+ data_vd = evaluate_by_chatgpt(data_vd, model_output_entry, model_correctness_entry, load_json=load_json, save_json_path=save_json_path_vd)
91
+ data_vd = check_same_by_chatgpt(data_vd, model_output_entry, load_json=load_json, save_json_path=save_json_path_vd)
92
+ data_vs = evaluate_by_chatgpt(data_vs, model_output_entry, model_correctness_entry, load_json=load_json, save_json_path=save_json_path_vs)
93
+ data_vs = check_same_by_chatgpt(data_vs, model_output_entry, load_json=load_json, save_json_path=save_json_path_vs)
94
+
95
+ data_vd = assign_correctness(data_vd, correctness_entry=model_correctness_entry_human)
96
+ data_vs = assign_correctness(data_vs, correctness_entry=model_correctness_entry_human)
97
+ data = data_vd + data_vs
98
+
99
+ all_data = get_eval_all(data, model_correctness_entry_human)
100
+ all_vd = get_eval_all(data_vd, model_correctness_entry_human)
101
+ all_vs = get_eval_all(data_vs, model_correctness_entry_human)
102
+ human_check_correctness = [i["correct"] for i in data]
103
+
104
+ print("##### Human Evaluate #####")
105
+
106
+ # question level
107
+ table1 = [["per question", "Total"],
108
+ ["VD", round(100 * all_vd["correct"]/all_vd["total"], 4)],
109
+ ["VS", round(100 * all_vs["correct"]/all_vs["total"], 4)],
110
+ ["Overall", round(100 * all_data["correct"]/all_data["total"], 4)]]
111
+ tab1 = PrettyTable(table1[0])
112
+ tab1.add_rows(table1[1:])
113
+ print(tab1)
114
+ q_acc_human = round(100 * all_data["correct"]/all_data["total"], 4)
115
+
116
+
117
+ all_data = get_eval_pair_all(data, model_correctness_entry_human)
118
+ easy = get_eval_pair_easy(data)
119
+ hard = get_eval_pair_hard(data)
120
+ all_vd = get_eval_pair_all(data_vd, model_correctness_entry_human)
121
+ easy_vd = get_eval_pair_easy(data_vd)
122
+ hard_vd = get_eval_pair_hard(data_vd)
123
+ all_vs = get_eval_pair_all(data_vs, model_correctness_entry_human)
124
+ easy_vs = get_eval_pair_easy(data_vs)
125
+ hard_vs = get_eval_pair_hard(data_vs)
126
+ # question pair level
127
+ table3 = [["per question pair", "Easy", "Hard", "Total"],
128
+ ["VD", round(100 * easy_vd["correct"]/easy_vd["total"], 4), round(100 * hard_vd["correct"]/hard_vd["total"], 4), round(100 * all_vd["correct"]/all_vd["total"], 4)],
129
+ ["VS", round(100 * easy_vs["correct"]/easy_vs["total"], 4), round(100 * hard_vs["correct"]/hard_vs["total"], 4), round(100 * all_vs["correct"]/all_vs["total"], 4)],
130
+ ["Overall", round(100 * easy["correct"]/easy["total"], 4), round(100 * hard["correct"]/hard["total"], 4), round(100 * all_data["correct"]/all_data["total"], 4)]]
131
+ tab3 = PrettyTable(table3[0])
132
+ tab3.add_rows(table3[1:])
133
+ print(tab3)
134
+
135
+
136
+ fig_all = get_eval_fig(data)
137
+ fig_vd = get_eval_fig(data_vd)
138
+ fig_vs = get_eval_fig(data_vs)
139
+ fig_all_human = fig_all
140
+ all_data_human = all_data
141
+ # image level
142
+ table2 = [["per figure", "Correct", "Inconsistant", "Wrong", "Score"],
143
+ ["VD", round(100 * fig_vd["correct"]/fig_vd["total"], 4), round(100 * fig_vd["inconsistent"]/fig_vd["total"], 4), round(100 * fig_vd["wrong"]/fig_vd["total"], 4), round(100 * fig_vd["score"], 4)],
144
+ ["VS", round(100 * fig_vs["correct"]/fig_vs["total"], 4), round(100 * fig_vs["inconsistent"]/fig_vs["total"], 4), round(100 * fig_vs["wrong"]/fig_vs["total"], 4), round(100 * fig_vs["score"], 4)],
145
+ ["Overall", round(100 * fig_all["correct"]/fig_all["total"], 4), round(100 * fig_all["inconsistent"]/fig_all["total"], 4), round(100 * fig_all["wrong"]/fig_all["total"], 4), round(100 * fig_all["score"], 4)]]
146
+ tab2 = PrettyTable(table2[0])
147
+ tab2.add_rows(table2[1:])
148
+ print(tab2)
149
+
150
+ pair_acc_human = round(100 * all_data["correct"]/all_data["total"], 4)
151
+ figure_acc_human = round(100 * fig_all["correct"]/fig_all["total"], 4)
152
+ easy_acc_human = round(100 * easy["correct"]/easy["total"], 4)
153
+ hard_acc_human = round(100 * hard["correct"]/hard["total"], 4)
154
+
155
+ stats_human = yes_ratio_stats(data)
156
+
157
+ # from IPython import embed;embed()
158
+ ############################################
159
+
160
+ print("##### GPT Evaluate #####")
161
+
162
+ data_vd = assign_correctness(data_vd, correctness_entry=model_correctness_entry)
163
+ data_vs = assign_correctness(data_vs, correctness_entry=model_correctness_entry)
164
+ data = data_vd + data_vs
165
+
166
+ all_data = get_eval_all(data, model_correctness_entry)
167
+ all_vd = get_eval_all(data_vd, model_correctness_entry)
168
+ all_vs = get_eval_all(data_vs, model_correctness_entry)
169
+ gpt_check_correctness = [i["correct"] for i in data]
170
+
171
+ # question level
172
+ table1 = [["per question", "Total"],
173
+ ["VD", round(100 * all_vd["correct"]/all_vd["total"], 4)],
174
+ ["VS", round(100 * all_vs["correct"]/all_vs["total"], 4)],
175
+ ["Overall", round(100 * all_data["correct"]/all_data["total"], 4)]]
176
+ tab1 = PrettyTable(table1[0])
177
+ tab1.add_rows(table1[1:])
178
+ print(tab1)
179
+
180
+ q_acc_gpt = round(100 * all_data["correct"]/all_data["total"], 4)
181
+
182
+ all_data = get_eval_pair_all(data, model_correctness_entry)
183
+ easy = get_eval_pair_easy(data)
184
+ hard = get_eval_pair_hard(data)
185
+ all_vd = get_eval_pair_all(data_vd, model_correctness_entry)
186
+ easy_vd = get_eval_pair_easy(data_vd)
187
+ hard_vd = get_eval_pair_hard(data_vd)
188
+ all_vs = get_eval_pair_all(data_vs, model_correctness_entry)
189
+ easy_vs = get_eval_pair_easy(data_vs)
190
+ hard_vs = get_eval_pair_hard(data_vs)
191
+ # question pair level
192
+ table3 = [["per question pair", "Easy", "Hard", "Total"],
193
+ ["VD", round(100 * easy_vd["correct"]/easy_vd["total"], 4), round(100 * hard_vd["correct"]/hard_vd["total"], 4), round(100 * all_vd["correct"]/all_vd["total"], 4)],
194
+ ["VS", round(100 * easy_vs["correct"]/easy_vs["total"], 4), round(100 * hard_vs["correct"]/hard_vs["total"], 4), round(100 * all_vs["correct"]/all_vs["total"], 4)],
195
+ ["Overall", round(100 * easy["correct"]/easy["total"], 4), round(100 * hard["correct"]/hard["total"], 4), round(100 * all_data["correct"]/all_data["total"], 4)]]
196
+ tab3 = PrettyTable(table3[0])
197
+ tab3.add_rows(table3[1:])
198
+ print(tab3)
199
+
200
+
201
+ fig_all = get_eval_fig(data)
202
+ fig_vd = get_eval_fig(data_vd)
203
+ fig_vs = get_eval_fig(data_vs)
204
+
205
+ # image level
206
+ table2 = [["per figure", "Correct", "Wrong", "Score"],
207
+ ["VD", round(100 * fig_vd["correct"]/fig_vd["total"], 4), round(100 * fig_vd["inconsistent"]/fig_vd["total"], 4) + round(100 * fig_vd["wrong"]/fig_vd["total"], 4), round(fig_vd["score"], 4)],
208
+ ["VS", round(100 * fig_vs["correct"]/fig_vs["total"], 4), round(100 * fig_vs["inconsistent"]/fig_vs["total"], 4) + round(100 * fig_vs["wrong"]/fig_vs["total"], 4), round(fig_vs["score"], 4)],
209
+ ["Overall", round(100 * fig_all["correct"]/fig_all["total"], 4), round(100 * fig_all["inconsistent"]/fig_all["total"], 4) + round(100 * fig_all["wrong"]/fig_all["total"], 4), round(fig_all["score"], 4)]]
210
+ tab2 = PrettyTable(table2[0])
211
+ tab2.add_rows(table2[1:])
212
+ print(tab2)
213
+
214
+ pair_acc_gpt = round(100 * all_data["correct"]/all_data["total"], 4)
215
+ figure_acc_gpt = round(100 * fig_all["correct"]/fig_all["total"], 4)
216
+ easy_acc_gpt = round(100 * easy["correct"]/easy["total"], 4)
217
+ hard_acc_gpt = round(100 * hard["correct"]/hard["total"], 4)
218
+
219
+ ##############################
220
+
221
+
222
+ print("##### Question Stats #####")
223
+ print("Easy Questions: " + str(easy_vd["total_q"]) + "(Visual Dependent) + " + str(easy_vs["total_q"]) + "(Visual Supplement)")
224
+ print("Hard Questions: " + str(hard_vd["total_q"]) + "(Visual Dependent) + " + str(hard_vs["total_q"]) + "(Visual Supplement)")
225
+ print("Total Questions: " + str(all_data["total_q"]))
226
+
227
+
228
+ print("##### Figure Stats #####")
229
+ print("Visual Dependent Figures: " + str(fig_vd["total"]))
230
+ print("Visual Supplement Figures: " + str(fig_vs["total"]))
231
+ print("Total Figures: " + str(fig_all["total"]))
232
+
233
+ print("##### Leaderboard Stats #####")
234
+
235
+ table = [["", "Acc per question pair (qAcc)", "Acc per figure (fAcc)", "Acc per easy question (easy aAcc)", "Acc per hard question (hard aAcc)", "Acc per question (aAcc)"],
236
+ ["Human Eval", pair_acc_human, figure_acc_human, easy_acc_human, hard_acc_human, q_acc_human],
237
+ ["GPT Eval", pair_acc_gpt, figure_acc_gpt, easy_acc_gpt, hard_acc_gpt, q_acc_gpt]]
238
+ leaderboard = PrettyTable(table[0])
239
+ leaderboard.add_rows(table[1:])
240
+ print(leaderboard)
241
+
242
+ print(all_data["total"], all_data["wrong"], all_data["LH"], all_data["VI"], all_data["Mix"])
243
+ print(all_data["total_q"], all_data["LH_cg"], all_data["VI_cg"], all_data["Mix_cg"])
244
+
245
+ print(len(gpt_check_correctness))
246
+ print(len(human_check_correctness))
247
+ print(sum(np.array(human_check_correctness) == np.array(gpt_check_correctness)))
248
+ print(sum(np.array(human_check_correctness) == np.array(gpt_check_correctness)) / len(gpt_check_correctness))
249
+
250
+
251
+ yes = [int(i["gt_answer"]) for i in data]
252
+ print(sum(yes))
253
+ print(len(yes))
254
+ print(sum(yes)/len(yes))
255
+
256
+ stats_gpt = yes_ratio_stats(data)
257
+
258
+ table = [["", "Yes/No Bias (Pct Diff)", "Yes/No Bias (FP Ratio)", "Consistency Test (correct)", "Consistency Test (inconsistent)", "Consistency Test (wrong)", "LH", "VI", "Mixed"],
259
+ ["Human Eval", stats_human["diff"], stats_human["fp"], round(100 * fig_all_human["correct"]/fig_all_human["total"], 4), round(100 * fig_all_human["inconsistent"]/fig_all_human["total"], 4), round(100 * fig_all_human["wrong"]/fig_all_human["total"], 4), round(100 * all_data_human["LH_cg"]/(all_data_human["LH_cg"] + all_data_human["VI_cg"] + all_data_human["Mix_cg"]), 4), round(100 * all_data_human["VI_cg"]/(all_data_human["LH_cg"] + all_data_human["VI_cg"] + all_data_human["Mix_cg"]), 4), round(100 * all_data_human["Mix_cg"]/(all_data_human["LH_cg"] + all_data_human["VI_cg"] + all_data_human["Mix_cg"]), 4)],
260
+ ["GPT Eval", stats_gpt["diff"], stats_gpt["fp"], round(100 * fig_all["correct"]/fig_all["total"], 4), round(100 * fig_all["inconsistent"]/fig_all["total"], 4), round(100 * fig_all["wrong"]/fig_all["total"], 4), round(100 * all_data["LH_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4), round(100 * all_data["VI_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4), round(100 * all_data["Mix_cg"]/(all_data["LH_cg"] + all_data["VI_cg"] + all_data["Mix_cg"]), 4)]]
261
+ test = PrettyTable(table[0])
262
+ test.add_rows(table[1:])
263
+ print(test)
tsv_to_json.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from tqdm import tqdm
3
+ import os
4
+
5
+ def get_image_file_location(root, row):
6
+ if int(row['visual_input']) == 0:
7
+ return None
8
+ img_file = row['set_id'] + "_" + row['figure_id'] + ".png"
9
+ return os.path.join(root, row['category'], row['subcategory'], img_file)
10
+ col_idx = {
11
+ 'category':0,
12
+ 'subcategory':1,
13
+ 'visual_input':2,
14
+ 'set_id':3,
15
+ 'figure_id':4,
16
+ 'sample_note':5,
17
+ 'question_id':6,
18
+ 'question':7,
19
+ 'gt_answer_details':8,
20
+ 'gt_answer':9,
21
+ # 'gpt4v_output':10,
22
+ # 'gpt4v_output_human_check': 11,
23
+ # 'llava_1_5_output':12,
24
+ # 'llava_1_5_output_human_check': 13,
25
+ }
26
+ import csv
27
+ import json
28
+ data_vd = []
29
+ data_vs = []
30
+ root_dir = "."
31
+ input_file_name = 'HallusionBench.tsv'
32
+ with open(input_file_name) as file:
33
+ tsv_file = csv.reader(file, delimiter="\t")
34
+ flag = 0
35
+ for line in tsv_file:
36
+ # if line[0] not in ["VD", "VS"]:
37
+ # if line[0] in ["NOTE", "category"]:
38
+ if "VD" not in line[0] and "VS" not in line[0]:
39
+ continue
40
+ data_dict = {}
41
+ try:
42
+ for k, v in col_idx.items():
43
+ data_dict[k] = line[v]
44
+ assert int(line[col_idx["gt_answer"]]) == 0 or int(line[col_idx["gt_answer"]]) == 1 or int(line[col_idx["gt_answer"]]) == 2
45
+ # assert int(line[col_idx["gpt4v_output_human_check"]]) == 0 or int(line[col_idx["gpt4v_output_human_check"]]) == 1 or int(line[col_idx["gpt4v_output_human_check"]]) == 2
46
+ # assert int(line[col_idx["llava_1_5_output_human_check"]]) == 0 or int(line[col_idx["llava_1_5_output_human_check"]]) == 1 or int(line[col_idx["llava_1_5_output_human_check"]]) == 2
47
+ except:
48
+ from IPython import embed;embed()
49
+
50
+ data_dict["filename"] = get_image_file_location(root_dir, data_dict)
51
+ if line[0] == "VD":
52
+ data_vd.append(data_dict)
53
+ else:
54
+ data_vs.append(data_dict)
55
+
56
+ result = data_vs + data_vd
57
+ print(len(result))
58
+ result1 = []
59
+
60
+ for re in result:
61
+ result1.append({"category": re['category'].strip(),
62
+ "subcategory": re['subcategory'].strip(),
63
+ "visual_input": re['visual_input'].strip(),
64
+ "set_id": re['set_id'].strip(),
65
+ "figure_id": re['figure_id'].strip(),
66
+ "sample_note": re['sample_note'].strip(),
67
+ "question_id": re['question_id'].strip(),
68
+ "question": re['question'].strip(),
69
+ "gt_answer": re['gt_answer'].strip(),
70
+ "gt_answer_details": re["gt_answer_details"].strip(),
71
+ "filename": re['filename'].strip() if re['filename'] else re['filename']})
72
+
73
+ print(len(result))
74
+ with open('./HallusionBench.json', 'w') as f:
75
+ json.dump(result, f)
utils.py ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ from tqdm import tqdm
4
+ import numpy as np
5
+ from prettytable import PrettyTable
6
+ import os
7
+ import time
8
+ import openai
9
+ import threading
10
+
11
+ try:
12
+ with open("apikey.txt", "r") as f:
13
+ api_key = f.read()
14
+ except:
15
+ api_key = ''
16
+
17
+
18
+ def get_image_file_location(root, row):
19
+ if int(row['visual_input']) == 0:
20
+ return None
21
+ img_file = row['set_id'] + "_" + row['figure_id'] + ".png"
22
+ return os.path.join(root, row['category'], row['subcategory'], img_file)
23
+
24
+
25
+
26
+ def evaluate_by_chatgpt(data, output_entry, correctness_entry, gpt_model="gpt-4", load_json=False, save_json_path="./hallusion_output.json"):
27
+ if load_json and os.path.exists(save_json_path):
28
+ with open(save_json_path, 'r') as f:
29
+ output = json.load(f)
30
+ else:
31
+ output = []
32
+ for sample in tqdm(data[len(output):]):
33
+ prompt = 'Imagine you are an intelligent teacher. Thoroughly read the question, reference answer and the prediction answer to ensure a clear understanding of the information provided. Assess the correctness of the predictions. '
34
+ prompt += 'If the prediction answer does not conflict with the reference answer, please generate “correct”. If the prediction answer conflict with the reference answer, please generate “incorrect”. If the prediction answer is unclear about the answer, please generate "unclear". \n\n Question:'
35
+ prompt += sample['question']
36
+ prompt += '\nReference answer: '
37
+ prompt += sample['gt_answer_details']
38
+ prompt += '\nPrediction answer:'
39
+ prompt += sample[output_entry]
40
+ prompt += '\nOutput:'
41
+
42
+ # https://github.com/openai/openai-python/issues/322#issuecomment-1767841683
43
+ while True:
44
+ try:
45
+ response = openai.ChatCompletion.create(
46
+ model=gpt_model,
47
+ messages=[{"role": "user", "content": prompt}],
48
+ api_key=api_key,
49
+ request_timeout=5)
50
+ break
51
+ except:
52
+ print("Timeout, retrying...")
53
+ time.sleep(5) # Wait for 5 seconds before retrying
54
+
55
+ output_text = response['choices'][0]['message']['content']
56
+
57
+
58
+ if 'incorrect' in output_text.lower():
59
+ gpt_correctness = "0"
60
+
61
+ elif 'correct' in output_text.lower():
62
+ gpt_correctness = "1"
63
+ else:
64
+ gpt_correctness = "2"
65
+
66
+ sample[correctness_entry] = gpt_correctness
67
+
68
+ output.append(sample)
69
+
70
+ with open(save_json_path, 'w') as f:
71
+ json.dump(output, f)
72
+
73
+ return output
74
+
75
+ def check_same_by_chatgpt(data, output_entry, gpt_model="gpt-4", load_json=False, save_json_path="./hallusion_output.json"):
76
+
77
+ orig_response = {}
78
+
79
+ for r in data:
80
+ if str(r["figure_id"]) == "0":
81
+ key = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
82
+ orig_response[key] = r[output_entry]
83
+
84
+ for sample in tqdm(data):
85
+ if "same" not in sample.keys():
86
+ key = "_".join([sample["category"], sample["subcategory"], str(sample["set_id"]), str(sample["question_id"])])
87
+ response2 = orig_response[key]
88
+
89
+ prompt = 'Imagine you are an intelligent teacher. Thoroughly read the two responses to two different questions. Assess the consistency of the information provided within those two responses. '
90
+ prompt += 'You do not know the specific questions, but you can asssess the consistency among the two responses by checking for logical conflicts if both responses are correct. '
91
+ prompt += 'If response1 does not conflict with response2, please generate “same”. Otherwise, generate "different". \n\n response1:'
92
+ prompt += sample[output_entry]
93
+ prompt += '\nresponse2: '
94
+ prompt += response2
95
+ prompt += '\nOutput:'
96
+
97
+ # https://github.com/openai/openai-python/issues/322#issuecomment-1767841683
98
+ while True:
99
+ try:
100
+ response = openai.ChatCompletion.create(
101
+ model=gpt_model,
102
+ messages=[{"role": "user", "content": prompt}],
103
+ api_key=api_key,
104
+ request_timeout=5)
105
+
106
+ break
107
+ except:
108
+ print("Timeout, retrying...")
109
+ time.sleep(5) # Wait for 5 seconds before retrying
110
+
111
+
112
+ output_text = response['choices'][0]['message']['content']
113
+
114
+ gpt_same = "0"
115
+
116
+ if 'same' in output_text.lower():
117
+ gpt_same = "1"
118
+
119
+ elif 'different' in output_text.lower():
120
+ gpt_same = "0"
121
+
122
+
123
+ sample["same"] = gpt_same
124
+
125
+ with open(save_json_path, 'w') as f:
126
+ json.dump(data, f)
127
+
128
+ return data
129
+
130
+ def get_eval_fig(data): # per figure
131
+
132
+ eval_fig_dict = dict()
133
+
134
+ for r in data:
135
+ if r["category"] == "VS" and str(r["figure_id"]) == "0": # no figure
136
+ continue
137
+ name = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["figure_id"])])
138
+ if name in eval_fig_dict:
139
+ c, t = eval_fig_dict[name]
140
+ eval_fig_dict[name] = (c + r["correct"], t+1)
141
+ else:
142
+ eval_fig_dict[name] = (r["correct"], 1)
143
+
144
+ eval_fig_stat = {}
145
+ eval_fig_stat["note"] = "all accuracy per image (consistency test)"
146
+ eval_fig_stat["total"] = len(eval_fig_dict.keys())
147
+ eval_fig_stat["correct"] = 0
148
+ eval_fig_stat["wrong"] = 0
149
+ eval_fig_stat["inconsistent"] = 0
150
+ eval_fig_stat["score"] = 0
151
+
152
+
153
+ for v in eval_fig_dict.values():
154
+ if v[0] == v[1]:
155
+ eval_fig_stat["correct"] += 1
156
+ elif v[0] == 0:
157
+ eval_fig_stat["wrong"] += 1
158
+ else:
159
+ eval_fig_stat["inconsistent"] += 1
160
+ eval_fig_stat["score"] += (v[0] / v[1])
161
+
162
+ eval_fig_stat["score"] = eval_fig_stat["score"] / eval_fig_stat["total"]
163
+ return eval_fig_stat
164
+
165
+ def get_eval_all(data, model_correctness_entry): # per question
166
+
167
+ eval_all_dict = dict()
168
+ eval_all_stat = {}
169
+ eval_all_stat["LH"] = 0
170
+ eval_all_stat["VI"] = 0
171
+ eval_all_stat["Mix"] = 0
172
+
173
+ for r in data:
174
+ name = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["figure_id"]), str(r["question_id"])])
175
+ assert name not in eval_all_dict
176
+
177
+ eval_all_dict[name] = r["correct"]
178
+
179
+ if str(r["category"]) == "VD": # VD
180
+ if str(r["figure_id"]) == "0":
181
+ if str(r[model_correctness_entry]) == "0" or str(r[model_correctness_entry]) == "2":
182
+ eval_all_stat["VI"] += 1
183
+ else:
184
+ if str(r[model_correctness_entry]) == "0":
185
+ eval_all_stat["Mix"] += 1
186
+ elif str(r[model_correctness_entry]) == "2":
187
+ eval_all_stat["VI"] += 1
188
+ else: # VS
189
+ if str(r["visual_input"]) == "0": # no visual
190
+ if str(r[model_correctness_entry]) == "0":
191
+ eval_all_stat["LH"] += 1
192
+ else: # original visual or modified visual (isual_input == 1 or 2)
193
+ if str(r[model_correctness_entry]) == "0":
194
+ eval_all_stat["Mix"] += 1
195
+ elif str(r[model_correctness_entry]) == "2":
196
+ eval_all_stat["VI"] += 1
197
+
198
+ eval_all_stat["note"] = "all accuracy per question"
199
+ eval_all_stat["total"] = len(eval_all_dict.keys())
200
+ eval_all_stat["correct"] = np.count_nonzero(list(eval_all_dict.values()))
201
+ eval_all_stat["wrong"] = eval_all_stat["total"] - eval_all_stat["correct"]
202
+
203
+ return eval_all_stat
204
+
205
+ def get_eval_pair_all(data, model_correctness_entry): # per question pair
206
+
207
+ orig_correctness = dict()
208
+ counter = 0
209
+ lh_counter = 0
210
+ vi_counter = 0
211
+ both_counter = 0
212
+
213
+ for r in data:
214
+ if str(r["figure_id"]) == "0":
215
+ key = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
216
+ orig_correctness[key] = r[model_correctness_entry]
217
+
218
+ get_eval_pair_dict = dict()
219
+ get_analysis_pair_dict = dict()
220
+
221
+ for r in data:
222
+ name = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
223
+ if name in get_eval_pair_dict:
224
+ c, t = get_eval_pair_dict[name]
225
+ get_eval_pair_dict[name] = (c + r["correct"], t+1)
226
+ else:
227
+ get_eval_pair_dict[name] = (r["correct"], 1)
228
+ counter += 1
229
+
230
+ # (LH, VI)
231
+ analysis = (0, 0)
232
+ if str(r["figure_id"]) == "0": # when it's original question
233
+ if str(r["category"]) == "VD": # VD
234
+ if str(r[model_correctness_entry]) == "0" or str(r[model_correctness_entry]) == "2":
235
+ analysis = (0, 1) # VI -- get original image wrong, bad vision
236
+ else: # VS
237
+ if str(r[model_correctness_entry]) == "0":
238
+ analysis = (1, 0) # LH -- wrong answer without visual, making things up
239
+ else: # when it's not original question
240
+ key = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
241
+ orig_c = orig_correctness[key]
242
+ if str(r["category"]) == "VD": # VD
243
+ if str(orig_c) == "1" and str(r[model_correctness_entry]) == "0":
244
+ if str(r["same"]) == "1":
245
+ analysis = (1, 1) # Mixed -- orig correct but modified wrong, with the same answer as the original question, could be bad vision or language hallucination
246
+ else:
247
+ analysis = (0, 1) # VI -- orig correct but modified wrong, but answer differently, only due to bad vision
248
+ elif str(orig_c) == "1" and str(r[model_correctness_entry]) == "2":
249
+ analysis = (0, 1) # VI -- orig correct but modified uncertain, bad vision
250
+ elif str(r[model_correctness_entry]) == "0" or str(r[model_correctness_entry]) == "2":
251
+ # when orig_c == 0 or 2 and current is wrong
252
+ analysis = (0, 1) # VI -- when original is wrong and current is wrong, bad vision
253
+ else: # VS
254
+ key = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
255
+ orig_c = orig_correctness[key]
256
+ if str(orig_c) == "0": # No visual wrong
257
+ if str(r[model_correctness_entry]) == "0" and str(r["same"]) == "1":
258
+ analysis = (1, 0) # LH -- same answer with and without visual, LH overtake visual
259
+ elif str(r[model_correctness_entry]) == "0":
260
+ analysis = (1, 1) # LH -- different answer with and without visual but both wrong, both language and visual are bad
261
+ elif str(r[model_correctness_entry]) == "2":
262
+ analysis = (1, 1) # Mixed -- no visual wrong, but with visual uncertain, could be either
263
+ elif str(orig_c) == "2":# No visual uncertain
264
+ if str(r[model_correctness_entry]) == "0" or str(r[model_correctness_entry]) == "2":
265
+ analysis = (0, 1) # VI -- no visual uncertain, with visual still wrong or uncertain, visual capability is bad
266
+ else: # No visual correct
267
+ if str(r[model_correctness_entry]) == "2":
268
+ analysis = (0, 1) # VI -- no visual correct, with visual uncertain, visual capability is bad
269
+ elif str(r[model_correctness_entry]) == "0": # current is wrong
270
+ if str(r["visual_input"]) == "1": # common sense visual question
271
+ analysis = (0, 1) # VI -- no visual correct, with visual wrong on common sense question, visual capability is bad
272
+ elif str(r["visual_input"]) == "2": # counter-common sense visual question
273
+ if str(r["same"]) == "1":
274
+ analysis = (1, 0) # LH -- with visual correct, but modified question wrong with the same answer, not considering visual so the error is attributed to Language
275
+ else:
276
+ analysis = (0, 1) # VI -- with visual correct, but modified question wrong with different answers, visual capability is bad
277
+ else:
278
+ assert False, "Data error"
279
+
280
+ if analysis[0] > 0 and analysis[1] > 0:
281
+ both_counter += 1
282
+ elif analysis[0] > 0:
283
+ lh_counter += 1
284
+ elif analysis[1] > 0:
285
+ vi_counter += 1
286
+
287
+
288
+ if name in get_analysis_pair_dict:
289
+ lh, vi = get_analysis_pair_dict[name]
290
+ get_analysis_pair_dict[name] = (lh + analysis[0], vi + analysis[1])
291
+ else:
292
+ get_analysis_pair_dict[name] = analysis
293
+
294
+ eval_all_pair_stat = {}
295
+ eval_all_pair_stat["note"] = "all accuracy per question pair"
296
+ eval_all_pair_stat["total"] = len(get_eval_pair_dict.keys())
297
+ eval_all_pair_stat["total_q"] = counter
298
+ eval_all_pair_stat["correct"] = 0
299
+ eval_all_pair_stat["wrong"] = 0
300
+ eval_all_pair_stat["LH"] = 0
301
+ eval_all_pair_stat["VI"] = 0
302
+ eval_all_pair_stat["Mix"] = 0
303
+
304
+ eval_all_pair_stat["LH_cg"] = lh_counter
305
+ eval_all_pair_stat["VI_cg"] = vi_counter
306
+ eval_all_pair_stat["Mix_cg"] = both_counter
307
+
308
+ # for v in get_eval_pair_dict.values():
309
+ # if v[0] == v[1]:
310
+ # eval_all_pair_stat["correct"] += 1
311
+ # else:
312
+ # eval_all_pair_stat["wrong"] += 1
313
+
314
+ # for v in get_analysis_pair_dict.values():
315
+ # if v[0] > 0 and v[1] > 0:
316
+ # eval_all_pair_stat["Mix"] += 1
317
+ # elif v[0] > 0:
318
+ # eval_all_pair_stat["LH"] += 1
319
+ # elif v[1] > 0:
320
+ # eval_all_pair_stat["VI"] += 1
321
+
322
+ for k in get_eval_pair_dict.keys():
323
+ v = get_eval_pair_dict[k]
324
+ a = get_analysis_pair_dict[k]
325
+ if v[0] == v[1]:
326
+ eval_all_pair_stat["correct"] += 1
327
+ else:
328
+ eval_all_pair_stat["wrong"] += 1
329
+ if a[0] > 0 and a[1] > 0:
330
+ eval_all_pair_stat["Mix"] += 1
331
+ elif a[0] > 0:
332
+ eval_all_pair_stat["LH"] += 1
333
+ elif a[1] > 0:
334
+ eval_all_pair_stat["VI"] += 1
335
+
336
+ assert (eval_all_pair_stat["wrong"] == (eval_all_pair_stat["Mix"] + eval_all_pair_stat["LH"] + eval_all_pair_stat["VI"]))
337
+
338
+ return eval_all_pair_stat
339
+
340
+ def get_eval_pair_easy(data):
341
+
342
+ get_eval_pair_dict = dict()
343
+ counter = 0
344
+
345
+ for r in data:
346
+ if str(r["visual_input"]) == "2":
347
+ # if str(r["figure_id"]) != "0":
348
+ continue
349
+ name = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
350
+ if name in get_eval_pair_dict:
351
+ c, t = get_eval_pair_dict[name]
352
+ get_eval_pair_dict[name] = (c + r["correct"], t+1)
353
+ else:
354
+ get_eval_pair_dict[name] = (r["correct"], 1)
355
+ counter += 1
356
+
357
+ eval_all_pair_stat = {}
358
+ eval_all_pair_stat["note"] = "all accuracy per question pair"
359
+ eval_all_pair_stat["total"] = len(get_eval_pair_dict.values())
360
+ eval_all_pair_stat["total_q"] = counter
361
+ eval_all_pair_stat["correct"] = 0
362
+ eval_all_pair_stat["wrong"] = 0
363
+
364
+ for v in get_eval_pair_dict.values():
365
+ if v[0] == v[1]:
366
+ eval_all_pair_stat["correct"] += 1
367
+ else:
368
+ eval_all_pair_stat["wrong"] += 1
369
+
370
+ return eval_all_pair_stat
371
+
372
+ def get_eval_pair_hard(data):
373
+
374
+ get_eval_pair_dict = dict()
375
+ counter = 0
376
+
377
+ for r in data:
378
+ if str(r["visual_input"]) != "2":
379
+ # if str(r["figure_id"]) == "0":
380
+ continue
381
+ name = "_".join([r["category"], r["subcategory"], str(r["set_id"]), str(r["question_id"])])
382
+ if name in get_eval_pair_dict:
383
+ c, t = get_eval_pair_dict[name]
384
+ get_eval_pair_dict[name] = (c + r["correct"], t+1)
385
+ else:
386
+ get_eval_pair_dict[name] = (r["correct"], 1)
387
+ counter += 1
388
+
389
+ eval_all_pair_stat = {}
390
+ eval_all_pair_stat["note"] = "all accuracy per question pair"
391
+ eval_all_pair_stat["total"] = len(get_eval_pair_dict.values())
392
+ eval_all_pair_stat["total_q"] = counter
393
+ eval_all_pair_stat["correct"] = 0
394
+ eval_all_pair_stat["wrong"] = 0
395
+
396
+ for v in get_eval_pair_dict.values():
397
+ if v[0] == v[1]:
398
+ eval_all_pair_stat["correct"] += 1
399
+ else:
400
+ eval_all_pair_stat["wrong"] += 1
401
+
402
+ return eval_all_pair_stat
403
+
404
+ def assign_correctness(data_arr, correctness_entry):
405
+ for r in data_arr:
406
+ assert int(r[correctness_entry]) == 0 or int(r[correctness_entry]) == 1 or int(r[correctness_entry]) == 2
407
+ if r["category"] == "VS" and int(r["figure_id"]) == 0: # if there is no visual supplement and the model does not know, count it as correct
408
+ r["correct"] = 1 if int(r[correctness_entry]) == 1 or int(r[correctness_entry]) == 2 else 0
409
+ else:
410
+ r["correct"] = 1 if int(r[correctness_entry]) == 1 else 0
411
+ return data_arr
412
+
413
+
414
+ def yes_ratio_stats(data):
415
+
416
+
417
+ yes_gt = [int(i["gt_answer"]) for i in data]
418
+ yes_pred = [int(int(i["correct"]) == int(i["gt_answer"])) for i in data]
419
+
420
+ fp_sample = [i for i in data if int(i["correct"]) == 0]
421
+ fp = [int(i["gt_answer"]) for i in fp_sample]
422
+
423
+ stats = {}
424
+ stats["diff"] = sum(yes_pred)/len(yes_pred) - sum(yes_gt)/len(yes_gt)
425
+ stats["fp"] = (len(fp) - sum(fp))/len(fp)
426
+
427
+ return stats
428
+