mAP计算工具使用|快速计算mAP

jupiter
2022-03-25 / 0 评论 / 572 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年03月26日,已超过788天没有更新,若内容或图片失效,请留言反馈。
计算mAP的工具:https://github.com/Cartucho/mAP

1.使用步骤

  • clone代码

    git clone https://github.com/Cartucho/mAP
  • Create the ground-truth files(将标签文件转为对应的格式,,工具参考2.1)

    • format
    <class_name> <left> <top> <right> <bottom> [<difficult>]
    • E.g. "image_1.txt"
    tvmonitor 2 10 173 238
    book 439 157 556 241
    book 437 246 518 351 difficult
    pottedplant 272 190 316 259
  • Copy the ground-truth files into the folder input/ground-truth/
  • Create the detection-results files
    • format
    <class_name> <confidence> <left> <top> <right> <bottom>
    • E.g. "image_1.txt"
    tvmonitor 0.471781 0 13 174 244
    cup 0.414941 274 226 301 265
    book 0.460851 429 219 528 247
    chair 0.292345 0 199 88 436
    book 0.269833 433 260 506 336
  • Copy the detection-results files into the folder input/detection-results/
  • Run the code

    python main.py

2.工具补充

2.1 VOC_2_gt.py

设置好xml_dir并执行即可得到对应的gt_txt文件

import os
import xmltodict
import shutil
from tqdm import tqdm


# TODO:需要修改的内容
xml_dir = "/data/jupiter/project/dataset/帯広空港_per_frame/xml/"

gt_dir = "./input/ground-truth/"
shutil.rmtree(gt_dir)
os.mkdir(gt_dir)

"""
将voc xml 的数据转为对应的gt_str
"""
def voc_2_gt_str(xml_dict):
    objects = xml_dict["annotation"]["object"]
    obj_list = []
    if isinstance(objects,list): # xml文件中包含多个object
        for obj in objects:
            obj_list.append(obj)
    else: # xml文件中包含1个object
        obj_list.append(objects)
    
    # 获取gt格式的数据信息 
    gt_str = ""
    for obj in obj_list:
        left = int(obj['bndbox']['xmin'])
        top = int(obj['bndbox']['ymin'])
        right = int(obj['bndbox']['xmax'])
        bottom = int(obj['bndbox']['ymax'])
        obj_name = obj['name']
        gt_str += "%s %s %s %s %s\n" % (obj_name, left, top, right, bottom)
    
    return gt_str


xml_list = os.listdir(xml_dir)
pbar = tqdm(total=len(xml_list)) # 加入进度条支持
pbar.set_description("VOC2GT") # 设置前缀
for tmp_file in xml_list:
    xml_path = os.path.join(xml_dir,tmp_file)
    gt_txt_path = os.path.join(gt_dir,tmp_file.replace(".xml", ".txt"))
    
    
    # 读取xml文件+转为字典
    with open(xml_path,'r',encoding="utf8") as f:
        xml_str = f.read()
    xml_dict = xmltodict.parse(xml_str)
    
    # 提取对应的数据
    gt_str = voc_2_gt_str(xml_dict)
    
    # 写入对应的gt_txt文件
    with open(gt_txt_path, "w") as f:
        f.write(gt_str)
    
    pbar.update(1)
pbar.close()
VOC2GT:  27%|██████████████████████████████████████████████████████████████▌                                                                                                                                                                         | 24013/89029 [03:25<09:54, 109.31it/s]

2.2 YOLOv5_2_dr.py

# YOLOv5_2_dr
import os
import xmltodict
import shutil
from tqdm import tqdm

# TODO:需要修改的内容
yolov5_detect_txt_dir = "/data/jupiter/project/目标检测对比实验/yolov5/runs/detect/exp3/labels"
cls_list = ['conveyor', 'refueller', 'aircraft', 'lounge', 'dining car', 'front of baggage car', 'tractor']
img_width = 1632
img_height = 1080


def txt_convert(txt_src,img_width,img_height,cls_list):
    txt_dst = ""
    for line in txt_src.split("\n"):
        if(len(line)==0):continue
        cls_id,dx,dy,dw,dh,conf = line.split(" ")
        cls_name = cls_list[int(cls_id)].replace(" ","")
        x_center = int(float(dx)*img_width)
        y_center = int(float(dy)*img_height)
        w = int(float(dw)*img_width)
        h = int(float(dh)*img_height)
        
        x1 = x_center - int(w/2)
        y1 = y_center - int(h/2)
        x2 = x1 + w
        y2 = y1 + h
        
        txt_dst += "{} {} {} {} {} {}\n".format(cls_name,conf,x1,y1,x2,y2)
    return txt_dst


dr_dir = "./input/detection-results/"
txt_list = os.listdir(yolov5_detect_txt_dir)

pbar = tqdm(total=len(txt_list)) # 加入进度条支持
pbar.set_description("YOLOv5_2_dr") # 设置前缀

for file in txt_list:
    txt_path_src = os.path.join(yolov5_detect_txt_dir,file)
    txt_path_dst = os.path.join(dr_dir,"{:>05d}.txt".format(int(file.split("_")[1][:-4])))
    
    # 读取原文件
    with open(txt_path_src) as f:
        txt_src = f.read()
    # 转为目标格式
    txt_dst = txt_convert(txt_src,img_width,img_height,cls_list)
    # 写入对应的dr_txt文件
    with open(txt_path_dst,"w") as f:
        f.write(txt_dst)

    pbar.update(1)
pbar.close()

参考资料

  1. https://github.com/Cartucho/mAP.git
  2. 目标检测中的mAP及代码实现
0

评论 (0)

打卡
取消