File size: 1,793 Bytes
fc0ff8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pycocoevalcap.cider.cider import Cider
from pycocoevalcap.eval import COCOEvalCap
from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer
from pycocotools.coco import COCO


def compute_cider(
    result_path,
    annotations_path,
):
    # create coco object and coco_result object
    coco = COCO(annotations_path)
    coco_result = coco.loadRes(result_path)

    # create coco_eval object by taking coco and coco_result
    coco_eval = COCOEvalCap(coco, coco_result)
    coco_eval.params["image_id"] = coco_result.getImgIds()
    coco_eval.evaluate()

    return coco_eval.eval

def compute_cider_all_scores(
    result_path,
    annotations_path,
    return_img_ids=False,
):
    # create coco object and coco_result object
    coco = COCO(annotations_path)
    coco_result = coco.loadRes(result_path)

    cider_scorer = Cider()
    imgIds = coco_result.getImgIds()
    gts = {}
    res = {}
    for imgId in imgIds:
        gts[imgId] = coco.imgToAnns[imgId]
        res[imgId] = coco_result.imgToAnns[imgId]
    tokenizer = PTBTokenizer()
    gts = tokenizer.tokenize(gts)
    res = tokenizer.tokenize(res)
    score, scores = cider_scorer.compute_score(gts, res)
    scores *= 100
    if return_img_ids:
        return scores, imgIds
    else:
        return scores

def postprocess_captioning_generation(predictions):
    return predictions.split("Output", 1)[0]

if __name__ == '__main__':
    result_path = "/mnt/cschlarmann37/project_multimodal/llava-evals/captions-json/cocoresults_38eb6f53-71e4-469e-a864-cb64b1fdbbf4.json"
    annotations_path = "/mnt/datasets/coco/annotations/captions_val2014.json"
    print(f"\nresult_path: {result_path}\n")
    metrics = compute_cider(result_path, annotations_path)
    print(metrics)
    print(f"CIDER: {metrics['CIDEr']*100}")