-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_captions.py
executable file
·59 lines (51 loc) · 1.71 KB
/
evaluate_captions.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
#!/usr/bin/env python
#
# File Name : evaluate_captions.py
#
# Description :
#
# Usage :
#
# Creation Date : 06-01-2015
# Last Modified : Tue Jan 6 21:47:15 2015
# Author : Hao Fang
import os
path_to_tokenized_ref_json = 'data/tokenized_ref.json'
path_to_raw_hypo_json = 'data/hypo.json'
from caption_eval.evals import PTBTokenizer, Bleu, Rouge, Meteor, Cider
# =================================================
# Load references
# =================================================
import json
tokenized_ref_for_image = json.load(open(path_to_tokenized_ref_json))
# =================================================
# Tokenize hypotheses
# =================================================
print 'tokenizing hypothese...'
raw_hypo_for_image = json.load(open(path_to_raw_hypo_json))
tokenizer = PTBTokenizer()
tokenized_hypo_for_image= tokenizer.tokenize(raw_hypo_for_image)
keys1 = tokenized_hypo_for_image.keys()
keys1.sort()
keys2 = tokenized_ref_for_image.keys()
keys2.sort()
assert(keys1 == keys2)
print 'all hypothese have been tokenized'
# =================================================
# Compute Bleu (up to 4-gram)
# - using 'shortest' reference length as brevity penalty
# Also provides n-gram precision w/o brevity penalty (This is NOT Bleu)
# =================================================
scorer = Bleu()
score, bleu_info = scorer.compute_score(tokenized_hypo_for_image, \
tokenized_ref_for_image)
print 'Bleu: ', score
print 'Bleu info: ', bleu_info
# =================================================
# Compute Meteor
# =================================================
scorer = Meteor()
score = scorer.compute_score(tokenized_hypo_for_image, \
tokenized_ref_for_image)
print 'Meteor: ', score
#...