直接放代码,从官方那边抠出来的AP计算代码,代码内部分为voc2007之前的11点计算方法和voc2007之后的方法,目前都已经采用else:内的计算方法
import numpy as np
def voc_ap(rec, prec, use_07_metric=False):
""" 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).
"""
# 针对2007年VOC,使用的11个点计算AP,现在不使用
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.])) #[0. 0.0666, 0.1333, 0.4 , 0.4666, 1.]
mpre = np.concatenate(([0.], prec, [0.])) #[0. 1., 0.6666, 0.4285, 0.3043, 0.]
# compute the precision envelope
# 计算出precision的各个断点(折线点)
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) #[1. 1. 0.6666 0.4285 0.3043 0. ]
# to calculate area under PR curve, look for points
# where X axis (recall) changes value
i = np.where(mrec[1:] != mrec[:-1])[0] #precision前后两个值不一样的点
print(mrec[1:], mrec[:-1])
print(i) #[0, 1, 3, 4, 5]
# AP= AP1 + AP2+ AP3+ AP4
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
recall = [0.3,0.6,0.7,0.8,0.9,0.92]
precision = [1.0,0.95,0.91,0.89,0.79,0.4]
ap = voc_ap(recall, precision)
print(ap)