-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtdiuc_metrics.py
170 lines (147 loc) · 6.03 KB
/
tdiuc_metrics.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
from __future__ import division
import json
import csv
import numpy as np
from collections import defaultdict
from scipy import stats
import sys, argparse
import json
import sys
py_version = sys.version_info[0 ]
def load_json(json_preds, gt_ann_a, answerkey):
preds_json = json.load(open(json_preds))
return preds_json
def tdiuc_mean_per_class(predictions, gt_ann, answerkey, convert_preds=True):
return mean_per_class(predictions, gt_ann, answerkey, convert_preds)
def get_gt_ann_map(gt_ann):
gt_ann_map = {}
for ann in gt_ann:
gt_ann_map[ann['question_id']] = ann
return gt_ann_map
def mean_per_class(predictions, overall_gt_ann, answerkey, convert_preds=True):
# predictions = preds_to_answer_ixs(predictions, gt_ann, answerkey)
res_json = {}
res = defaultdict(list)
pred_answer_list = []
gt_answer_list = []
notfound = 0
gt_ann_map = get_gt_ann_map(overall_gt_ann)
for idx, pred in enumerate(predictions):
qid = pred['question_id']
pred_ans = pred['answer']
qid_gt_ann = gt_ann_map[qid]
gt_answer = qid_gt_ann['answers'][0]['answer']
gt_type = qid_gt_ann['question_type']
res[gt_type + '_pred'].append(pred_ans)
if py_version == 2:
gt_answer_present =answerkey.has_key(gt_answer)
else:
gt_answer_present = gt_answer in answerkey
if gt_answer_present:
#gt_idx = int(answerkey[gt_answer])
#res[gt_type + '_gt'].append(gt_idx)
res[gt_type + '_gt'].append(gt_answer)
# gt_answers_idx.append(gt_idx)
pred_answer_list.append(pred_ans)
gt_answer_list.append(gt_answer)
# if gt_idx == pred:
if gt_answer == pred_ans:
res[gt_type + '_t'].append(pred_ans)
else:
res[gt_type + '_f'].append(pred_ans)
else:
gt_answer_list.append(-1)
pred_answer_list.append(pred_ans)
res[gt_type + '_f'].append(pred_ans)
res[gt_type + '_gt'].append(-1)
notfound += 1
print("\n %d of validation answers were not in the answerkey" % notfound)
types = list(set([a['question_type'] for a in overall_gt_ann]))
sum_acc = []
eps = 1e-10
# print('\nNOT USING PER-ANSWER NORMALIZATION\n')
res_json['without_normalization'] = []
for tp in types:
denom = len(res[tp + '_t'] + res[tp + '_f'])
if denom == 0:
acc = 0
else:
acc = 100 * (len(res[tp + '_t']) / denom)
sum_acc.append(acc + eps)
# print('Accuracy for %s is %.2f' % (tp, acc))
res_json['without_normalization'].append({'type': tp, 'accuracy': acc})
res_json['arithmetic_mpt'] = np.mean(np.array(sum_acc))
# print('Arithmetic MPT Accuracy is %.2f' % (np.mean(np.array(sum_acc))))
res_json['harmonic_mpt'] = stats.hmean(sum_acc)
# print('Harmonic MPT Accuracy is %.2f' % (stats.hmean(sum_acc)))
for pr in predictions:
gt_ann_map[pr['question_id']]['answers'][0]
n_acc = 100 * np.mean(np.array(pred_answer_list) == np.array(gt_answer_list))
res_json['overall_without_normalization'] = n_acc
# print('Overall Traditional Accuracy is %.2f' % n_acc)
print('\n---------------------------------------')
res_json['with_normalization'] = []
# print('USING PER-ANSWER NORMALIZATION\n')
types = list(set([a['question_type'] for a in overall_gt_ann]))
sum_acc = []
eps = 1e-10
for tp in types:
per_ans_stat = defaultdict(int)
for g, p in zip(res[tp + '_gt'], res[tp + '_pred']):
per_ans_stat[str(g) + '_gt'] += 1
if g == p:
per_ans_stat[str(g)] += 1
unq_acc = 0
for unq_ans in set(res[tp + '_gt']):
acc_curr_ans = per_ans_stat[str(unq_ans)] / per_ans_stat[str(unq_ans) + '_gt']
unq_acc += acc_curr_ans
denom = len(set(res[tp + '_gt']))
if denom == 0:
acc = 0
else:
acc = 100 * unq_acc / denom
sum_acc.append(acc + eps)
# print('Accuracy for %s is %.2f' % (tp, acc))
res_json['with_normalization'].append({
'type': tp,
'accuracy': acc
})
res_json['arithmetic_nmpt'] = np.mean(np.array(sum_acc))
# print('Arithmetic N-MPT Accuracy is %.2f' % (np.mean(np.array(sum_acc))))
res_json['harmonic_nmpt'] = stats.hmean(sum_acc)
# print('Harmonic N-MPT Accuracy is %.2f' % (stats.hmean(sum_acc)))
n_acc = 100 * np.mean(np.array(pred_answer_list) == np.array(gt_answer_list))
res_json['overall_with_normalization'] = n_acc
# print('Overall Traditional Accuracy is %.2f' % n_acc)
return res_json
def generate_answer_key(annotations):
answer_ix = 0
answer_key = {}
for ann in annotations['annotations']:
ans = ann['answers'][0]['answer']
if ans not in answer_key:
answer_key[ans] = answer_ix
answer_ix += 1
# %%
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gt_ann', required=True,
help='path to ground truth annotation JSON file')
parser.add_argument('--pred_ann', required=True,
help='path to the predictions JSON file')
parser.add_argument('--answer_ix_map', required=True,
help='Json file with ix_to_answer for each answer')
args = parser.parse_args()
with open(args.answer_ix_map, 'rt') as f:
answer_ix_map = json.load(f)['ix_to_answer']
answerkey = dict((ans, int(ix)) for ix, ans in zip(answer_ix_map.keys(), answer_ix_map.values()))
# answerkey_csv = csv.reader(open(args.answerkey))
# answerkey = dict((rows[0], rows[1]) for rows in answerkey_csv)
gt_ann = json.load(open(args.gt_ann))['annotations']
predictions = load_json(args.pred_ann, gt_ann, answerkey)
predictions = np.array(predictions)
mean_per_class(predictions, gt_ann, answerkey, convert_preds=False)
print('.------------------------------------------')
# %%
if __name__ == "__main__":
main()