File size: 1,269 Bytes
daf0288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from torchmetrics.detection import MeanAveragePrecision
from pprint import pprint


def compute_coco_map(file):
    coco_pred = list()
    coco_gt = list()
    for _, obj in file.items():
        tmp_pred = {
            "boxes": torch.tensor(obj["pred"], device=0),
            "labels": torch.tensor([0] * len(obj["pred"]), device=0),
            "scores": torch.tensor([0.999] * len(obj["pred"]), device=0),
        }

        tmp_gt = {
            "boxes": torch.tensor(obj["gt"], device=0),
            "labels": torch.tensor([0] * len(obj["gt"]), device=0),
        }

        coco_pred.append(tmp_pred)
        coco_gt.append(tmp_gt)

    metric = MeanAveragePrecision(
        iou_type="bbox",
        max_detection_thresholds=[1, 10, 1000],
        backend="faster_coco_eval",
    )
    metric.update(coco_pred, coco_gt)
    pprint(metric.compute())


if __name__ == "__main__":
    import json
    import argparse

    parser = argparse.ArgumentParser(description="mAP Computation")

    parser.add_argument("-f", "--file", help="path to html table results in json file")
    args = parser.parse_args()


    results_file = args.file
    with open(results_file, "r") as f:
        results_json = json.load(f)

    compute_coco_map(results_json)