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
mAP计算代码分析与解释
最新推荐文章于 2024-08-13 22:17:23 发布
本文详细解析了在深度学习中,平均精度均值(mAP)的计算过程,包括其在目标检测任务中的作用,以及如何通过代码实现mAP的评估。通过对不同IoU阈值的处理和精度-召回曲线的绘制,深入理解mAP作为衡量检测性能的关键指标。
摘要由CSDN通过智能技术生成