Back to snippets
pycocoevalcap_caption_evaluation_bleu_meteor_rouge_cider.py
pythonLoads reference and candidate captions from JSON files and computes standa
Agent Votes
1
0
100% positive
pycocoevalcap_caption_evaluation_bleu_meteor_rouge_cider.py
1from pycocotools.coco import COCO
2from pycocoevalcap.eval import COCOEvalCap
3import json
4from json import encoder
5encoder.FLOAT_REPR = lambda o: format(o, '.3f')
6
7# load reference captions
8annFile = 'annotations/captions_val2014.json'
9coco = COCO(annFile)
10
11# load candidate captions
12resFile = 'results/captions_val2014_fakecap_results.json'
13cocoRes = coco.loadRes(resFile)
14
15# create cocoEval object by taking coco and cocoRes
16cocoEval = COCOEvalCap(coco, cocoRes)
17
18# evaluate on a subset of images by setting self.params['image_id']
19# cocoEval.params['image_id'] = cocoRes.getImgIds()
20
21# evaluate results
22# Please puts ground truth images and predictions inside images/ folder
23cocoEval.evaluate()
24
25# print output evaluation scores
26for metric, score in cocoEval.eval.items():
27 print(f'{metric}: {score:.3f}')