-
Notifications
You must be signed in to change notification settings - Fork 24
/
SCN_evaluation.py
executable file
·66 lines (50 loc) · 1.83 KB
/
SCN_evaluation.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
"""
Semantic Compositional Network https://arxiv.org/pdf/1611.08002.pdf
Developed by Zhe Gan, [email protected], July, 12, 2016
Computes the BLEU, ROUGE, METEOR, and CIDER
using the COCO metrics scripts
"""
# this requires the coco-caption package, https://github.com/tylin/coco-caption
from pycocoevalcap.bleu.bleu import Bleu
from pycocoevalcap.rouge.rouge import Rouge
from pycocoevalcap.cider.cider import Cider
from pycocoevalcap.meteor.meteor import Meteor
import json
from collections import defaultdict
def score(ref, hypo):
"""
ref, dictionary of reference sentences (id, sentence)
hypo, dictionary of hypothesis sentences (id, sentence)
score, dictionary of scores
"""
scorers = [
(Bleu(4), ["Bleu_1", "Bleu_2", "Bleu_3", "Bleu_4"]),
(Meteor(),"METEOR"),
(Rouge(), "ROUGE_L"),
(Cider(), "CIDEr")
]
final_scores = {}
for scorer, method in scorers:
score, scores = scorer.compute_score(ref, hypo)
if type(score) == list:
for m, s in zip(method, score):
final_scores[m] = s
else:
final_scores[method] = score
return final_scores
if __name__ == '__main__':
# this is the generated captions
hypo = {idx: [lines.strip()] for (idx, lines) in enumerate(open('./coco_scn_5k_test.txt', 'rb') )}
# this is the ground truth captions
dataset = json.load(open('./data/coco/dataset.json', 'r'))
split = defaultdict(list)
for img in dataset['images']:
split[img['split']].append(img)
del dataset
refs = []
for img in split['test']:
references = [' '.join(tmp['tokens']) for tmp in img['sentences']]
refs.append(references)
del split
refs = {idx: ref for (idx, ref) in enumerate(refs)}
print score(refs, hypo)