-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_code.py
95 lines (79 loc) · 3.05 KB
/
test_code.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
from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer
from pycocoevalcap.bleu.bleu import Bleu
from pycocoevalcap.meteor.meteor import Meteor
from pycocoevalcap.rouge.rouge import Rouge
from pycocoevalcap.cider.cider import Cider
class COCOEvalCap:
def __init__(self,images,gts,res):
self.evalImgs = []
self.eval = {}
self.imgToEval = {}
self.params = {'image_id': images}
self.gts = gts
self.res = res
def evaluate(self):
imgIds = self.params['image_id']
gts = self.gts
res = self.res
# =================================================
# Set up scorers
# =================================================
print('tokenization...')
tokenizer = PTBTokenizer()
gts = tokenizer.tokenize(gts)
res = tokenizer.tokenize(res)
# =================================================
# Set up scorers
# =================================================
print('setting up scorers...')
scorers = [
(Bleu(4), ["Bleu_1", "Bleu_2", "Bleu_3", "Bleu_4"]),
(Meteor(),"METEOR"),
(Rouge(), "ROUGE_L"),
(Cider(), "CIDEr")
]
# =================================================
# Compute scores
# =================================================
eval = {}
for scorer, method in scorers:
print('computing %s score...'%(scorer.method()))
score, scores = scorer.compute_score(gts, res)
if type(method) == list:
for sc, scs, m in zip(score, scores, method):
self.setEval(sc, m)
self.setImgToEvalImgs(scs, imgIds, m)
print("%s: %0.3f"%(m, sc))
else:
self.setEval(score, method)
self.setImgToEvalImgs(scores, imgIds, method)
print("%s: %0.3f"%(method, score))
self.setEvalImgs()
def setEval(self, score, method):
self.eval[method] = score
def setImgToEvalImgs(self, scores, imgIds, method):
for imgId, score in zip(imgIds, scores):
if not imgId in self.imgToEval:
self.imgToEval[imgId] = {}
self.imgToEval[imgId]["image_id"] = imgId
self.imgToEval[imgId][method] = score
def setEvalImgs(self):
self.evalImgs = [eval for imgId, eval in self.imgToEval.items()]
def calculate_metrics(rng,datasetGTS,datasetRES):
imgIds = rng
gts = {}
res = {}
imgToAnnsGTS = {ann['image_id']: [] for ann in datasetGTS}
for ann in datasetGTS:
imgToAnnsGTS[ann['image_id']] += [ann]
imgToAnnsRES = {ann['image_id']: [] for ann in datasetRES}
for ann in datasetRES:
imgToAnnsRES[ann['image_id']] += [ann]
for imgId in imgIds:
imgId = int(imgId)
gts[imgId] = imgToAnnsGTS[imgId]
res[imgId] = imgToAnnsRES[imgId]
#print(gts, res)
evalObj = COCOEvalCap(imgIds,gts,res)
evalObj.evaluate()
return evalObj.eval