调用pycocotools计算mAP

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import json
from tempfile import NamedTemporaryFile

# coco格式的json文件,原始标注数据
anno_file = '/home/j_m/Desktop/val/val.json'
coco_gt = COCO(anno_file)

# 用GT框作为预测框进行计算,目的是得到detection_res
with open(anno_file, 'r') as f:
    json_file = json.load(f)
annotations = json_file['annotations']
detection_res = []
for anno in annotations:
    detection_res.append({
        'score': 1.,
        'category_id': anno['category_id'],
        'bbox': anno['bbox'],
        'image_id': anno['image_id']
    })

with NamedTemporaryFile(suffix='.json') as tf:
    # 由于后续需要,先将detection_res转换成二进制后写入json文件
    content = json.dumps(detection_res).encode(encoding='utf-8')
    tf.write(content)
    res_path = tf.name

    # loadRes会在coco_gt的基础上生成一个新的COCO类型的instance并返回
    coco_dt = coco_gt.loadRes(res_path)

    cocoEval = COCOeval(coco_gt, coco_dt, 'bbox')
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

print(cocoEval.stats)

输出的结果如下:

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.997
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.997
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.997
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.997
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.608
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.993
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 1.000

 

 

计算各类AP的方法:

precisions = cocoEval.eval['precision']

# precision: (iou, recall, cls, area range, max dets)
assert len(self.coco.getCatIds()) == precisions.shape[2]

results_per_category = []
for idx, catId in enumerate(self.coco.getCatIds()):
    # area range index 0: all area ranges
    # max dets index -1: typically 100 per image
    nm = self.coco.loadCats(catId)[0]
    precision = precisions[:, :, idx, 0, -1]
    precision = precision[precision > -1]
    if precision.size:
        ap = np.mean(precision)
    else:
        ap = float('nan')
    results_per_category.append(
        (f'{nm["name"]}', f'{float(ap):0.3f}'))

print(f'\n{results_per_category}')

代码来源于mmdet/datasets/coco.py,搜索关键词classwise

你可能感兴趣的:(调用pycocotools计算mAP)