010-61934861

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

背景

在完成yolov5模型移植,运行在板端后,通常需要验证板端运行的mAP,以衡量模型最终的业务的精度。

要完成以上目标通常需要以下两个步骤

步骤一:在板端批量跑出目标检测结果,保存为yolo的txt 或者json格式。数据格式资料参考博客

步骤二:计算 预测的目标检查结果(json)和数据标注结果的mAP,本文重点介绍该步骤的脚步内容。


计算mAP的Python脚本

一般可使用COCOeval计算mAP,脚本如下

#get_map.py
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



发表评论
全部评论(0)