voc_eval.py地址为https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/datasets/voc_eval.py
mAP计算过程
输入:
1、各类检测到的目标框txt文件
txt文件内容如下,第一列是图像名字(不带后缀),第二列是置信度,剩下依次是xmin、ymin、xmax、ymax
2、Annotations文件
3、验证图像名字列表
4、compute_mAP.py + voc_eval.py (代码见下)
输出:
计算结果
compute_mAP.py代码及解析
#-*- coding: utf-8 -*-
import os
import numpy as np
from voc_eval import voc_eval # 注意将voc_eval.py和compute_mAP.py放在同一级目录下
detpath = 'Path to dets txt' # 各类txt文件路径
detfiles = os.listdir(detpath)
classes = ('__background__', # always index 0 数据集类别
'class1', 'class2', 'class3', 'class4', 'class5', 'class6')
aps = [] # 保存各类ap
recs = [] # 保存recall
precs = [] # 保存精度
annopath = 'Path to Annotations' + '{:s}.xml' # annotations的路径,{:s}.xml方便后面根据图像名字读取对应的xml文件
imagesetfile = 'Path to VOC2007/ImageSets/Main/test.txt' # 读取图像名字列表文件
cachedir = 'Path to annotations_cache/'
for i, cls in enumerate(classes):
if cls == '__background__':
continue
for f in detfiles: # 读取cls类对应的txt文件
if f.find(cls) != -1:
filename = detpath + f
rec, prec, ap = voc_eval( # 调用voc_eval.py计算cls类的recall precision ap
filename, annopath, imagesetfile, cls, cachedir, ovthresh=0,
use_07_metric=False)
aps += [ap]
print('AP for {} = {:.4f}'.format(cls, ap))
print('recall for {} = {:.4f}'.format(cls, rec[-1]))
print('precision for {} = {:.4f}'.format(cls, prec[-1]))
print('Mea