010-61934861

举报
帖子标题
coco数据集格式计算mAP的python脚本
*举报原因
取消
首页 > 开发者论坛 > coco数据集格式计算mAP的python脚本
coco数据集格式计算mAP的python脚本
Flower 2024-07-06 13:38
举报 62 0 0

背景说明

在完成YOLOv5模型移植,运行在板端后,通常需要衡量板端运行的mAP。

一般需要两个步骤

步骤一:在板端批量运行得到目标检测结果,可保存为yolo的txt格式也可保存为json格式;

[目标检测任务中常用的数据集格式(voc、coco、yolo)](https://blog.csdn.net/weixin_45277161/article/details/130331788)

步骤二:计算预测结果 和 标注结果的mAP,本文重点介绍该步骤。

COCOeval 计算mAP

经验证该脚本不局限coco 80分类,只要满足json数据集格式,即可使用该脚本进行计算


# get_map.py
import argparse
import glob
import json
if __name__ == "__main__":
    import argparse
    import glob
    import json
    if __name__ == "__main__":
        parser = argparse.ArgumentParser(description='')
        parser.add_argument('--result-json', type=str, help='Json of inference results.')
        parser.add_argument('--benchmark-json', type=str, help='Json of labels.')
        args = parser.parse_args()
        result_json = args.result_json
        instances_train2017_json = args.benchmark_json
        with open(result_json, 'r') as r:
            result = json.load(r)
        def get_img_id(item):
            return item["image_id"]
        imgIds = set(map(get_img_id, result))
        try:
            from pycocotools.coco import COCO
            from pycocotools.cocoeval import COCOeval
            cocoGt = COCO(glob.glob(instances_train2017_json)[0])  # initialize coco ground truth api
            cocoDt = cocoGt.loadRes(result_json)  # initialize coco pred api
            cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
            cocoEval.params.imgIds = list(imgIds)  # image IDs to evaluate
            cocoEval.evaluate()
            cocoEval.accumulate()
            cocoEval.summarize()
            map, map50 = cocoEval.stats[:2]  # update results(mAP@0.5:0.95, mAP@0.5)
        except Exception as e:
            print('ERROR: pycocotools unable to run:%s' % e)


执行的命令行脚本如下

python get_map.py  --result-json yolov5s_predictions.json --benchmark-json  instances_val2017.json


输出截图如下,和[官方](https://github.com/ultralytics/yolov5?tab=readme-ov-file)的效果一致

企业微信截图_17202321026053.png


企业微信截图_17202321497572.png


- instances_val2017.json为COCO标准数据集,下载命令如下

# 下载标注文件(2017 Annotations)
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip


- yolov5s_predictions.json为yolov5 预测的数据集

执行YOLOv5源码中的验证脚本val.txt即可得到,需要注意,在运行时需要指定--save-json保存输出结果的json文件,指定--save-conf在json文件中会保存预测框置信度。

python val.py --save-json --save-conf

数据格式如下

链接: https://pan.baidu.com/s/1udt4iPGEL0glxojS3OmklQ 提取码: asdc

企业微信截图_17202324158111.png


# txt文件转换为coco json 格式

1. 训练的txt文件,数据格式如下

58 0.389578 0.416103 0.038594 0.163146
62 0.127641 0.505153 0.233313 0.2227

 对应【标签  x   y   w  h】

模型直接预测得到的txt文件,数据格式如下

46 0.0451243 0.215648 0.0848332 0.431296 0.725234 
46 0.102373 0.546547 0.198804 0.326551 0.70208

对应【标签 conf  x   y   w  h】

2. json文件中数据格式如下

 {
    "image_id": 5,
    "category_id": 0,
    "bbox": [
      280.697,
      41.816,
      218.932,
      349.688
    ],
    "score": 0.94485
  },
 

其中bbbox为映射到原始图片的值,同样需要score分数

将预测的txt文件转换为json格式

# 自定义数据集标注

1)准备图片

2)使用LableImg标注工具

 对目标进行标注

image.png

 标注结果保存为VOC格式。

 可将VOC格式转换为JSON, 参考链接:https://blog.csdn.net/a264672/article/details/123056708)



发表评论
全部评论(0)