forked from salaniz/pycocoevalcap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
46 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env python | ||
# | ||
# | ||
# File Name : bleu.py | ||
# | ||
# Description : Wrapper for BLEU scorer. | ||
|
@@ -8,7 +8,7 @@ | |
# Last Modified : Thu 19 Mar 2015 09:13:28 PM PDT | ||
# Authors : Hao Fang <[email protected]> and Tsung-Yi Lin <[email protected]> | ||
|
||
from bleu_scorer import BleuScorer | ||
from .bleu_scorer import BleuScorer | ||
|
||
|
||
class Bleu: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
# reserved. Do not redistribute without permission from the | ||
# author. Not for commercial use. | ||
|
||
# Modified by: | ||
# Modified by: | ||
# Hao Fang <[email protected]> | ||
# Tsung-Yi Lin <[email protected]> | ||
|
||
|
@@ -52,21 +52,22 @@ def cook_refs(refs, eff=None, n=4): ## lhuang: oracle will call with "average" | |
reflen = float(sum(reflen))/len(reflen) | ||
|
||
## lhuang: N.B.: leave reflen computaiton to the very end!! | ||
|
||
## lhuang: N.B.: in case of "closest", keep a list of reflens!! (bad design) | ||
|
||
return (reflen, maxcounts) | ||
|
||
def cook_test(test, (reflen, refmaxcounts), eff=None, n=4): | ||
def cook_test(test, refs, eff=None, n=4): | ||
'''Takes a test sentence and returns an object that | ||
encapsulates everything that BLEU needs to know about it.''' | ||
|
||
reflen, refmaxcounts = refs | ||
testlen, counts = precook(test, n, True) | ||
|
||
result = {} | ||
|
||
# Calculate effective reference sentence length. | ||
|
||
if eff == "closest": | ||
result["reflen"] = min((abs(l-testlen), l) for l in reflen)[1] | ||
else: ## i.e., "average" or "shortest" or None | ||
|
@@ -108,7 +109,7 @@ def __init__(self, test=None, refs=None, n=4, special_reflen=None): | |
|
||
def cook_append(self, test, refs): | ||
'''called by constructor and __iadd__ to avoid creating new instances.''' | ||
|
||
if refs is not None: | ||
self.crefs.append(cook_refs(refs)) | ||
if test is not None: | ||
|
@@ -136,7 +137,7 @@ def reflen(self, option=None): | |
|
||
def testlen(self, option=None): | ||
self.compute_score(option=option) | ||
return self._testlen | ||
return self._testlen | ||
|
||
def retest(self, new_test): | ||
if type(new_test) is str: | ||
|
@@ -151,7 +152,7 @@ def retest(self, new_test): | |
|
||
def rescore(self, new_test): | ||
''' replace test(s) with new test(s), and returns the new score.''' | ||
|
||
return self.retest(new_test).compute_score() | ||
|
||
def size(self): | ||
|
@@ -170,7 +171,7 @@ def __iadd__(self, other): | |
self.crefs.extend(other.crefs) | ||
self._score = None ## need to recompute | ||
|
||
return self | ||
return self | ||
|
||
def compatible(self, other): | ||
return isinstance(other, BleuScorer) and self.n == other.n | ||
|
@@ -179,7 +180,7 @@ def single_reflen(self, option="average"): | |
return self._single_reflen(self.crefs[0][0], option) | ||
|
||
def _single_reflen(self, reflens, option=None, testlen=None): | ||
|
||
if option == "shortest": | ||
reflen = min(reflens) | ||
elif option == "average": | ||
|
@@ -194,7 +195,7 @@ def _single_reflen(self, reflens, option=None, testlen=None): | |
def recompute_score(self, option=None, verbose=0): | ||
self._score = None | ||
return self.compute_score(option, verbose) | ||
|
||
def compute_score(self, option=None, verbose=0): | ||
n = self.n | ||
small = 1e-9 | ||
|
@@ -212,7 +213,7 @@ def compute_score(self, option=None, verbose=0): | |
totalcomps = {'testlen':0, 'reflen':0, 'guess':[0]*n, 'correct':[0]*n} | ||
|
||
# for each sentence | ||
for comps in self.ctest: | ||
for comps in self.ctest: | ||
testlen = comps['testlen'] | ||
self._testlen += testlen | ||
|
||
|
@@ -222,7 +223,7 @@ def compute_score(self, option=None, verbose=0): | |
reflen = self.special_reflen | ||
|
||
self._reflen += reflen | ||
|
||
for key in ['guess','correct']: | ||
for k in xrange(n): | ||
totalcomps[key][k] += comps[key][k] | ||
|
@@ -231,15 +232,15 @@ def compute_score(self, option=None, verbose=0): | |
bleu = 1. | ||
for k in xrange(n): | ||
bleu *= (float(comps['correct'][k]) + tiny) \ | ||
/(float(comps['guess'][k]) + small) | ||
/(float(comps['guess'][k]) + small) | ||
bleu_list[k].append(bleu ** (1./(k+1))) | ||
ratio = (testlen + tiny) / (reflen + small) ## N.B.: avoid zero division | ||
if ratio < 1: | ||
for k in xrange(n): | ||
bleu_list[k][-1] *= math.exp(1 - 1/ratio) | ||
|
||
if verbose > 1: | ||
print comps, reflen | ||
print(comps, reflen) | ||
|
||
totalcomps['reflen'] = self._reflen | ||
totalcomps['testlen'] = self._testlen | ||
|
@@ -256,8 +257,8 @@ def compute_score(self, option=None, verbose=0): | |
bleus[k] *= math.exp(1 - 1/ratio) | ||
|
||
if verbose > 0: | ||
print totalcomps | ||
print "ratio:", ratio | ||
print(totalcomps) | ||
print("ratio:", ratio) | ||
|
||
self._score = bleus | ||
return self._score, bleu_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
# Filename: cider.py | ||
# | ||
# Description: Describes the class to compute the CIDEr (Consensus-Based Image Description Evaluation) Metric | ||
# Description: Describes the class to compute the CIDEr (Consensus-Based Image Description Evaluation) Metric | ||
# by Vedantam, Zitnick, and Parikh (http://arxiv.org/abs/1411.5726) | ||
# | ||
# Creation Date: Sun Feb 8 14:16:54 2015 | ||
# | ||
# Authors: Ramakrishna Vedantam <[email protected]> and Tsung-Yi Lin <[email protected]> | ||
|
||
from cider_scorer import CiderScorer | ||
from .cider_scorer import CiderScorer | ||
import pdb | ||
|
||
class Cider: | ||
""" | ||
Main Class to compute the CIDEr metric | ||
Main Class to compute the CIDEr metric | ||
""" | ||
def __init__(self, test=None, refs=None, n=4, sigma=6.0): | ||
|
@@ -26,7 +26,7 @@ def compute_score(self, gts, res): | |
Main function to compute CIDEr score | ||
:param hypo_for_image (dict) : dictionary with key <image> and value <tokenized hypothesis / candidate sentence> | ||
ref_for_image (dict) : dictionary with key <image> and value <tokenized reference sentence> | ||
:return: cider (float) : computed CIDEr score for the corpus | ||
:return: cider (float) : computed CIDEr score for the corpus | ||
""" | ||
|
||
assert(gts.keys() == res.keys()) | ||
|
@@ -51,4 +51,4 @@ def compute_score(self, gts, res): | |
return score, scores | ||
|
||
def method(self): | ||
return "CIDEr" | ||
return "CIDEr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters