File size: 3,180 Bytes
44ddffd |
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 |
import json
import string
import regex
#Normalization from SQuAD evaluation script https://worksheets.codalab.org/rest/bundles/0x6b567e1cf2e041ec80d7098f031c5c9e/contents/blob/
def normalize_answer(s):
def remove_articles(text):
return regex.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
def cal_acc_multi(ground_truth, preds, return_id = False):
all_num = len(ground_truth)
acc_num = 0
ids = []
temp = []
for i, answer_id in enumerate(ground_truth):
pred = preds[i]
cnt = 0
for aid in answer_id:
if pred == aid:
cnt += 1
if cnt ==1:
acc_num += 1/3
elif cnt == 2:
acc_num += 2/3
elif cnt > 2:
acc_num += 1
if return_id:
return acc_num / all_num, ids
else:
return acc_num, all_num
def ensemble(a):
return max(a[::-1], key = a.count)
# Ground Truth Answers
f=open("/root/okvqa/data/okvqa_val.json", "r")
answer_dict=json.load(f)
f.close()
for k in answer_dict.keys():
for a_ind, a in enumerate(answer_dict[k]['multi_answers']):
answer_dict[k]['multi_answers'][a_ind] = normalize_answer(answer_dict[k]['multi_answers'][a_ind])
# Load Predictions (for example, ensemble of three models' predictions)
f1=open("/mnt/bn/qingyi-hl/finetunedModelonOKVQA/1e-41e-5FTwiki25-From-1e-41e-5PretrainWiki25Epo0/FTwiki25FromPretrainWiki25Epo0-1e41e5/predictions.json", "r")
predict0_dict=json.load(f1)
for p in predict0_dict.keys():
predict0_dict[p]=normalize_answer(predict0_dict[p])
f1.close()
f2=open("/mnt/bn/qingyi-hl/finetunedModelonOKVQA/1e-41e-5FTwiki25-From-1e-41e-5PretrainWiki25Epo1/predictions.json", "r")
predict1_dict=json.load(f2)
for p in predict1_dict.keys():
predict1_dict[p]=normalize_answer(predict1_dict[p])
f2.close()
f3=open("/mnt/bn/qingyi-hl/finetunedModelonOKVQA/1e-41e-5FTwiki25-From-1e-41e-5PretrainWiki25Epo2/predictions.json", "r")
predict2_dict=json.load(f3)
for p in predict2_dict.keys():
predict2_dict[p]=normalize_answer(predict2_dict[p])
f3.close()
answer_list=[]
predict0_list=[]
predict1_list=[]
predict2_list=[]
emsemble_predict=[]
for k in answer_dict.keys():
answer_list.append( answer_dict[k]['multi_answers'])
predict0_list.append( predict0_dict[k])
predict1_list.append( predict1_dict[k])
predict2_list.append( predict2_dict[k])
emsemble_predict.append(ensemble([predict0_dict[k], predict1_dict[k], predict2_dict[k])
acc_n0,all_n0=cal_acc_multi(answer_list,predict0_list)
acc_n1,all_n1=cal_acc_multi(answer_list,predict1_list)
acc_n2,all_n2=cal_acc_multi(answer_list,predict2_list)
acc_ens,all_ens=cal_acc_multi(answer_list,emsemble_predict)
print("0-accuracy",acc_n0/all_n0)
print("1-accuracy",acc_n1/all_n1)
print("2-accuracy",acc_n2/all_n2)
print("ensemble-accuracy",acc_ens/all_ens) |