mAP计算代码分析与解释

本文详细解析了在深度学习中,平均精度均值(mAP)的计算过程,包括其在目标检测任务中的作用,以及如何通过代码实现mAP的评估。通过对不同IoU阈值的处理和精度-召回曲线的绘制,深入理解mAP作为衡量检测性能的关键指标。
摘要由CSDN通过智能技术生成
from __future__ import print_function
import argparse
import xml.etree.ElementTree as ET
import os,sys
import pickle
import numpy as np
def parse_args(): # 命令行加载结果文件
    """Parse input arguments."""
    parser = argparse.ArgumentParser(description='mAP Calculation')

    parser.add_argument('--path', dest='path', help='The data path', type=str)
    args = parser.parse_args()

    return args
    
def parse_rec(filename):# decode xml file#读取标注的xml文件转化成list
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {
   }
        obj_struct['name'] = obj.find('name').text
        #obj_struct['pose'] = obj.find('pose').text
        #obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('x_left_top').text),
                              int(bbox.find('y_left_top').text),
                              int(bbox.find('x_left_top').text)+int(bbox.find('width').text),
                              int(bbox.find('y_left_top').text)+int(bbox.find('height').text)]
        objects.append(obj_struct)

    return objects

def voc_ap(rec, prec, use_07_metric=False):#单个测量AP的函数。
    """ ap = voc_ap(rec, prec, [use_07_metric])
    Compute VOC AP given precision and recall.
    If use_07_metric is true, uses the
    VOC 07 11 point method (default:False).
    """
    #计算AP值,若use_07_metric=true,则用11个点采样的方法,将rec从0-1分成11个点,这些点prec值求平均近似表示AP
    #若use_07_metric=false,则采用更为精确的逐点积分方法
    if use_07_metric:
        # 11 point metric
        ap = 0.
        for t in np.arange(0., 1.1, 0.1):
            if np.sum(rec >= t) == 0:
                p = 0
            else:
                p = np.max(prec[rec >= t])
            ap = ap + p / 11.
    else:
        # correct AP calculation
        # first append sentinel values at the end
        mrec = np.concatenate(([0.], rec, [1.]))
        mpre 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值