import numpy as np
import matplotlib.pyplot as plt
def voc_ap(rec, prec, 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
print (rec)
mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [0.]))
print ('...................')
# compute the precision envelope
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
# to calculate area under PR curve, look for points
# where X axis (recall) changes value
i = np.where(mrec[1:] != mrec[:-1])[0]
# and sum (\Delta recall) * prec
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
def compute_ap(scores, gt_label, class_id = 0, t = 0.5):
sorted_ind = np.argsort(scores)[::-1]
socres = [scores[i] for i in sorted_ind]
gt_label = [gt_label[i] for i in sorted_ind]
nd = len(scores)
npos = np.sum(gt_label)
print ('npos:', npos)
tp = np.zeros(nd)
fp = np.zeros(nd)
for i in range(nd):
score = scores[i]
label = gt_label[i]
if score > t and label == class_id:
tp[i] = 1
else:
fp[i] = 1
print ('tp:', tp)
fp = np.cumsum(fp)
tp = np.cumsum(tp)
# recall
rec = tp / float(npos)
# precision
prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
ap = voc_ap(rec, prec, False)
# ap = voc_ap(rec, prec, True)
return rec, prec, ap
scores = [0.23, 0.76, 0.01, 0.91, 0.13, 0.45, 0.12, 0.03, 0.38,
0.11, 0.03, 0.09, 0.65, 0.07, 0.12, 0.24, 0.1, 0.23, 0.46, 0.08]
gt_label = [0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
# arange_t = np.arange(0, 1.1, 0.1)
# for t in arange_t:
compute_ap(scores, gt_label, 1, 0.1)
# mAP就是所有AP的平均值
精度/召回/AP/mAP
最新推荐文章于 2023-06-02 16:16:09 发布
2291

被折叠的 条评论
为什么被折叠?



