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
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
from random import randint
|
| 7 |
+
from os import listdir
|
| 8 |
+
import numpy as np
|
| 9 |
+
from sklearn.metrics import average_precision_score, f1_score,recall_score
|
| 10 |
+
#delete cv2
|
| 11 |
+
index_list = [728, 542, 519, 1357, 1007, 1380, 783, 733, 1327, 760, 684, 795, 1075, 917, 638, 1020, 1180, 1225, 914, 1236, 871, 1231, 1330, 602, 870, 639, 1187, 934, 626, 1255, 1064, 1281, 689, 1078, 1043, 735, 1159, 1189, 1140, 579, 554, 752, 1095, 574, 841, 1054, 622, 547, 908, 1267, 767, 1215, 739, 664, 1365, 984, 1243, 1165, 634, 972, 1329, 804, 855, 1249, 311, 415, 448, 319, 14, 472, 356, 407, 265, 241, 309, 176, 105, 383, 32, 349, 149, 436, 343, 21, 147, 31, 240, 314, 9, 100, 190, 11, 218, 20, 346, 89, 25, 112, 243, 357]
|
| 12 |
+
#
|
| 13 |
+
current_dir_path = os.path.dirname(os.path.realpath(__file__))
|
| 14 |
+
|
| 15 |
+
def All_scores():
|
| 16 |
+
All_scores = F1_score(All_gt_binary,All_predict_binary)
|
| 17 |
+
VQA_scores = F1_score(VQA_gt_binary,VQA_predict_binary)
|
| 18 |
+
VizWiz_scores = F1_score(VizWiz_gt_binary,VizWiz_predict_binary)
|
| 19 |
+
return All_scores, VizWiz_scores, VQA_scores
|
| 20 |
+
|
| 21 |
+
def F1_score(gt_binary,predict_binary):
|
| 22 |
+
# predict_binary/gt_binary is np.array
|
| 23 |
+
length_diff = len(gt_binary)-len(predict_binary)
|
| 24 |
+
assert len(gt_binary) == len(predict_binary)
|
| 25 |
+
acc = 1- np.mean(abs(gt_binary - predict_binary))
|
| 26 |
+
print("accuracy: " + str(acc) + "\n")
|
| 27 |
+
|
| 28 |
+
# consider positive is diff, negative is same
|
| 29 |
+
print("consider positive is diff, negative is same")
|
| 30 |
+
relevant_set = [gt_binary[i] for i in range(len(gt_binary)) if predict_binary[i]==1]
|
| 31 |
+
precision = np.mean(relevant_set)
|
| 32 |
+
print("precision: " + str(precision))
|
| 33 |
+
|
| 34 |
+
relevant_element = [predict_binary[i] for i in range(len(gt_binary)) if gt_binary[i]==1]
|
| 35 |
+
recall = np.mean(relevant_element)
|
| 36 |
+
print("recall: " + str(recall))
|
| 37 |
+
|
| 38 |
+
scores={}
|
| 39 |
+
scores["Overall_f1_score"] = 2*precision*recall/(precision+recall)
|
| 40 |
+
scores["Oveall_precision"]=precision
|
| 41 |
+
scores["Overall_recall"] = recall
|
| 42 |
+
print("scores",scores)
|
| 43 |
+
# scores["VizWiz_f1_score"] =
|
| 44 |
+
# scores["VizWiz_precision"]=
|
| 45 |
+
# scores["VizWiz_recall"] =
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# scores["VQA_f1_score"] =
|
| 49 |
+
# scores["VQA_precision"]=
|
| 50 |
+
# scores["VQA_recall"] =
|
| 51 |
+
return scores
|
| 52 |
+
|
| 53 |
+
def evaluate_unanswerability(self, imgs=None):
|
| 54 |
+
if imgs == None:
|
| 55 |
+
imgs = [img for img in self.params['images']]
|
| 56 |
+
pred = []
|
| 57 |
+
gt_labels = []
|
| 58 |
+
for img in imgs:
|
| 59 |
+
gt_labels.append(self.vqa.imgToQA[img]['answerable'])
|
| 60 |
+
pred.append(self.vqaRes.imgToQA[img]['answerable'])
|
| 61 |
+
gt_labels = np.array(gt_labels)
|
| 62 |
+
pred = np.array(pred)
|
| 63 |
+
|
| 64 |
+
gt_labels_n = 1 - gt_labels
|
| 65 |
+
pred_n = 1.0 - pred
|
| 66 |
+
average_precision = average_precision_score(gt_labels_n, pred_n)
|
| 67 |
+
one_f1_score = f1_score(gt_labels_n, pred_n > 0.5)
|
| 68 |
+
|
| 69 |
+
self.unanswerability['average_precision'] = round(100*average_precision, self.n)
|
| 70 |
+
self.unanswerability['f1_score'] = round(100*one_f1_score, self.n)
|
| 71 |
+
|
| 72 |
+
def get_overlap_index(list1, list2):
|
| 73 |
+
index_list=[]
|
| 74 |
+
for i in range(len(list1)):
|
| 75 |
+
if list1[i] in list2:
|
| 76 |
+
index_list.append(list2.index(list1[i]))
|
| 77 |
+
return index_list
|
| 78 |
+
def GroundingDifference(annFile, resFile):
|
| 79 |
+
Anns_vqa = []
|
| 80 |
+
Anns_vizwiz = []
|
| 81 |
+
Ress_vqa=[]
|
| 82 |
+
Ress_vizwiz=[]
|
| 83 |
+
|
| 84 |
+
with open(annFile,'r') as annF:
|
| 85 |
+
with open(resFile,'r') as resF:
|
| 86 |
+
anns = json.load(annF)
|
| 87 |
+
test_ress = json.load(resF)
|
| 88 |
+
|
| 89 |
+
if len(anns) == 100:
|
| 90 |
+
# print(get_overlap_index(anns,test_ress))
|
| 91 |
+
ress = [test_ress[i] for i in index_list]
|
| 92 |
+
else:
|
| 93 |
+
ress = test_ress
|
| 94 |
+
|
| 95 |
+
Anns_labels = np.array([ann["single_grounding"] for ann in anns])
|
| 96 |
+
Res_labels = np.array([res["single_grounding"] for res in ress])
|
| 97 |
+
for ann in anns:
|
| 98 |
+
if ann["question_id"].startswith("Viz"):
|
| 99 |
+
Anns_vizwiz.append(ann)
|
| 100 |
+
else:
|
| 101 |
+
Anns_vqa.append(ann)
|
| 102 |
+
for res in ress:
|
| 103 |
+
if res["question_id"].startswith("Viz"):
|
| 104 |
+
Ress_vizwiz.append(res)
|
| 105 |
+
else:
|
| 106 |
+
Ress_vqa.append(res)
|
| 107 |
+
# All_Ress = Ress_vizwiz+Ress_vqa
|
| 108 |
+
|
| 109 |
+
Anns_vizwiz_labels = np.array([ann["single_grounding"] for ann in Anns_vizwiz])
|
| 110 |
+
Anns_vqa_labels = np.array([ann["single_grounding"] for ann in Anns_vqa])
|
| 111 |
+
|
| 112 |
+
Ress_vizwiz_labels = np.array([res["single_grounding"] for res in Ress_vizwiz])
|
| 113 |
+
Ress_vqa_labels = np.array([res["single_grounding"] for res in Ress_vqa])
|
| 114 |
+
if len(Anns_labels) !=len(Res_labels):
|
| 115 |
+
print("unsucessful submission! The number of files you generated is not equal to the number of ground-truth files.")
|
| 116 |
+
else:
|
| 117 |
+
results ={}
|
| 118 |
+
# F1_score(Anns_labels, Res_labels)
|
| 119 |
+
# F1_score(Anns_vizwiz_labels, Ress_vizwiz_labels)
|
| 120 |
+
# F1_score(Anns_vqa_labels, Ress_vqa_labels)
|
| 121 |
+
results["overall_f1"] = round(100*f1_score(Anns_labels,Res_labels>0.5),2)
|
| 122 |
+
results['overall_precision'] = round(100*average_precision_score(Anns_labels,Res_labels>0.5), 2)
|
| 123 |
+
results['overall_recall'] = round(100*recall_score(Anns_labels,Res_labels>0.5), 2)
|
| 124 |
+
results['vqav2_f1'] = round(100*f1_score(Anns_vqa_labels,Ress_vqa_labels>0.5),2)
|
| 125 |
+
results['vqa_precision'] = round(100*average_precision_score(Anns_vqa_labels,Ress_vqa_labels>0.5), 2)
|
| 126 |
+
results['vqa_recall'] = round(100*recall_score(Anns_vqa_labels,Ress_vqa_labels>0.5), 2)
|
| 127 |
+
results['vizwiz_f1'] = round(100*f1_score(Anns_vizwiz_labels,Ress_vizwiz_labels>0.5),2)
|
| 128 |
+
results['vizwiz_precision'] = round(100*average_precision_score(Anns_vizwiz_labels,Ress_vizwiz_labels>0.5), 2)
|
| 129 |
+
results['vizwiz_recall'] = round(100*recall_score(Anns_vizwiz_labels,Ress_vizwiz_labels>0.5), 2)
|
| 130 |
+
return results
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
phase_splits = {
|
| 134 |
+
"test-dev2025": ["test-dev"],
|
| 135 |
+
"test-standard2025": ["test"],
|
| 136 |
+
"test-challenge2025": ["test"]
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def evaluate(test_annotation_file, resFile, phase_codename, **kwargs):
|
| 141 |
+
result = []
|
| 142 |
+
splits = phase_splits[phase_codename]
|
| 143 |
+
for split in splits:
|
| 144 |
+
annFile = os.path.join(current_dir_path, "Annotations", split + ".json")
|
| 145 |
+
# if "dev" in phase_codename:
|
| 146 |
+
print(phase_codename)
|
| 147 |
+
result.append({split: GroundingDifference(annFile,resFile)}) # return a dict from groundingDifference()
|
| 148 |
+
output = {"result": result}
|
| 149 |
+
output["submission_result"] = result
|
| 150 |
+
# output["submission_result"] = output["result"][0]
|
| 151 |
+
print(result)
|
| 152 |
+
print("Completed evaluation for Test Phase")
|
| 153 |
+
return output
|
| 154 |
+
|
| 155 |
+
if __name__=="__main__":
|
| 156 |
+
evaluate("t","Results/test.json","test-dev2025")
|