forked from yunjey/show-attend-and-tell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbleu.py
71 lines (48 loc) · 1.76 KB
/
bleu.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
import cPickle as pickle
import os
import sys
sys.path.append('../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
def score(ref, hypo):
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
def evaluate(data_path='./data', split='val', get_scores=False):
reference_path = os.path.join(data_path, "%s/%s.references.pkl" %(split, split))
candidate_path = os.path.join(data_path, "%s/%s.candidate.captions.pkl" %(split, split))
# load caption data
with open(reference_path, 'rb') as f:
ref = pickle.load(f)
with open(candidate_path, 'rb') as f:
cand = pickle.load(f)
# make dictionary
hypo = {}
for i, caption in enumerate(cand):
hypo[i] = [caption]
# compute bleu score
final_scores = score(ref, hypo)
# print out scores
print 'Bleu_1:\t',final_scores['Bleu_1']
print 'Bleu_2:\t',final_scores['Bleu_2']
print 'Bleu_3:\t',final_scores['Bleu_3']
print 'Bleu_4:\t',final_scores['Bleu_4']
print 'METEOR:\t',final_scores['METEOR']
print 'ROUGE_L:',final_scores['ROUGE_L']
print 'CIDEr:\t',final_scores['CIDEr']
if get_scores:
return final_scores