怎么做网站的301,张家港网站推广,怎样做网站二维码,百度推广怎么才能效果好目录 背景说明COCOeval 计算mAPtxt文件转换为coco json 格式自定义数据集标注 背景说明
在完成YOLOv5模型移植#xff0c;运行在板端后#xff0c;通常需要衡量板端运行的mAP。
一般需要两个步骤 步骤一#xff1a;在板端批量运行得到目标检测结果#xff0c;可保存为yol… 目录 背景说明COCOeval 计算mAPtxt文件转换为coco json 格式自定义数据集标注 背景说明
在完成YOLOv5模型移植运行在板端后通常需要衡量板端运行的mAP。
一般需要两个步骤 步骤一在板端批量运行得到目标检测结果可保存为yolo的txt格式也可保存为json格式 目标检测任务中常用的数据集格式(voc、coco、yolo) 步骤二计算预测结果 和 标注结果的mAP本文重点介绍该步骤。
探索历程可略过如果想基于预测的txt计算mAP推荐 Cartucho/mAP, 由于开发时间有限最终还是决定基于json 格式进行计算。
COCOeval 计算mAP
经验证该脚本不局限coco 80分类只要满足json数据集格式即可使用该脚本进行计算
# get_map.py
import argparse
import glob
import jsonif __name__ __main__:import argparseimport globimport jsonif __name__ __main__:parser argparse.ArgumentParser(description)parser.add_argument(--result-json, typestr, helpJson of inference results.)parser.add_argument(--benchmark-json, typestr, helpJson of labels.)args parser.parse_args()result_json args.result_jsoninstances_train2017_json args.benchmark_jsonwith 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 COCOfrom pycocotools.cocoeval import COCOevalcocoGt COCO(glob.glob(instances_train2017_json)[0]) # initialize coco ground truth apicocoDt cocoGt.loadRes(result_json) # initialize coco pred apicocoEval COCOeval(cocoGt, cocoDt, bbox)cocoEval.params.imgIds list(imgIds) # image IDs to evaluatecocoEval.evaluate()cocoEval.accumulate()cocoEval.summarize()map, map50 cocoEval.stats[:2] # update results(mAP0.5:0.95, mAP0.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输出截图如下和官方的效果一致
instances_val2017.json为COCO标准数据集下载命令如下
# 下载标注文件2017 Annotations
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zipyolov5s_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
txt文件转换为coco json 格式
训练的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】
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标注工具 对目标进行标注 标注结果保存为VOC格式。 可将VOC格式转换为JSON