COCOAPI计算mAP

cal_mAP.py 

import re
import os
import json
import cv2
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from collections import OrderedDict
import argparse
from pathlib import Path


class COCOResults(object):
    METRICS = {
        "bbox": ["AP", "AP50", "AP75", "APs", "APm", "APl"],
        "segm": ["AP", "AP50", "AP75", "APs", "APm", "APl"],
        "box_proposal": [
            "AR@100",
            "ARs@100",
            "ARm@100",
            "ARl@100",
            "AR@1000",
            "ARs@1000",
            "ARm@1000",
            "ARl@1000",
        ],
        "keypoints": ["AP", "AP50", "AP75", "APm", "APl"],
    }

    def __init__(self, *iou_types):
        allowed_types = ("box_proposal", "bbox", "segm", "keypoints")
        assert all(iou_type in allowed_types for iou_type in iou_types)
        results = OrderedDict()
        for iou_type in iou_types:
            results[iou_type] = OrderedDict(
                [(metric, -1) for metric in COCOResults.METRICS[iou_type]]
            )
        self.results = results

    def update(self, coco_eval):
        if coco_eval is None:
            return
        from pycocotools.cocoeval import COCOeval

        assert isinstance(coco_eval, COCOeval)
        s = coco_eval.stats
        iou_type = coco_eval.params.iouType
        res = self.results[iou_type]
        metrics = COCOResults.METRICS[iou_type]
        for idx, metric in enumerate(metrics):
            res[metric] = s[idx]

    def __repr__(self):
        results = '\n'
        for task, metrics in self.results.items():
            results += 'Task: {}\n'.format(task)
            metric_names = metrics.keys()
            metric_vals = ['{:.4f}'.format(v) for v in metrics.values()]
            results += (', '.join(metric_names) + '\n')
            results += (', '.join(metric_vals) + '\n')
        return results


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--log')
    parser.add_argument('--anno')
    parser.add_argument('--image-dir')
    args = parser.parse_args()
    coco = COCO(args.anno)
    results = COCOResults('bbox')
    coco_dt = coco.loadRes(args.log)
    coco_eval = COCOeval(coco, coco_dt, 'bbox')
    imagelist = os.listdir(args.image_dir)
    coco_eval.params.imgIds = [int(Path(x).stem) for x in imagelist if x.endswith('jpg')]
    coco_eval.evaluate()
    coco_eval.accumulate()
    coco_eval.summarize()
    results.update(coco_eval)
    print(results)
    

 执行:

python cal_mAP.py --anno ./instances_val2017.json --log ./result.json --image-dir ../../coco2017val/images/val2017

上述命令中--anno是coco格式的标注文件, --image-dir是测试集图片路径,--log是预测的结果文件为json格式,可参考如下伪代码生成:

def coco80_to_coco91_class():  # converts 80-index (val2014) to 91-index (paper)
    # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34,
         35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
         64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
    return x 

preds = []
for img in js['images']:
    img_p = img['file_name']
    if not os.path.isfile('/'.join((imgdir, img_p))):
       continue
    
    pred = inference(img_p) # 推理接口, 获取预测结果
    coco91class = coco80_to_coco91_class() # 获取coco类别字典
    for pp in pred:
      if pp is None:
        continue
      for p in pp:
        bbox = p[:4]
        prob = float(p[4])
        clse = int(p[5])
        preds.append(dict(
          image_id=img['id'], # 图片id
          category_id=coco91class[clse], # 类别id
          bbox=[float(bbox[0]), float(bbox[1]), float(bbox[2]-bbox[0]), float(bbox[3]-bbox[1])], # 检测框
          score=prob)) # 预测得分

  with open('result.json', 'w') as f: # 保存为json文件
      json.dump(preds, f)

result.json示例格式如下:

[
    {"image_id": 397133, "category_id": 1, "bbox": [382.21466064453125, 61.73094177246094, 123.6776123046875, 290.18048095703125], "score": 0.8199472427368164}, 
    {"image_id": 397133, "category_id": 1, "bbox": [0.8529815673828125, 262.3185729980469, 62.58330535888672, 47.36285400390625], "score": 0.5744290351867676}, 
    {"image_id": 397134, "category_id": 51, "bbox": [60.40350341796875, 286.3122863769531, 81.48675537109375, 41.669189453125], "score": 0.28584015369415283}, 
    {"image_id": 397134, "category_id": 64, "bbox": [-3.7143173217773438, -2.5591201782226562, 70.6357421875, 155.88150024414062], "score": 0.20149467885494232}, 
    {"image_id": 397135, "category_id": 51, "bbox": [28.550830841064453, 344.74737548828125, 70.10545349121094, 40.75140380859375], "score": 0.11809217184782028}
]

mAP计算结果如下:

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Yolact是一种基于Mask-RCNN框架的目标检测模型,具有实时分割的能力。COCO评价指标是用来衡量目标检测和分割模型性能的指标之一,常用于比较和评估不同模型的表现。 在Yolact代码中,计算COCO评价指标的主要步骤如下: 1. 首先,需要加载训练好的Yolact模型并设置好相应的参数,包括类别数量、阈值等。 2. 接下来,需要准备COCO数据集的标注文件和预测结果文件。标注文件包含每个图像中的目标类别和边界框信息,预测结果文件则包括模型对图像的预测结果,包括类别、边界框和分割掩码等。 3. 使用COCOAPI工具包中的函数,可以将标注文件和预测结果文件转换成COCO数据结构。这样就可以方便地使用COCO评价指标进行计算。 4. 在计算评价指标之前,通常会对预测结果进行后处理,以去除一些低置信度或重叠度较大的检测框。常见的后处理方法包括非极大值抑制(NMS)和置信度阈值等。 5. 最后,使用COCOAPI提供的函数,可以根据标注文件和预测结果文件计算一系列COCO评价指标,包括检测精度(AP)、平均精度(mAP)和平均交并比(mIoU)等。 总之,Yolact代码计算COCO评价指标的过程包括准备数据、后处理和调用COCOAPI函数进行计算。通过评价指标的计算,可以了解Yolact模型在目标检测和分割任务上的表现,并与其他模型进行比较和评估。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洪流之源

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值