目标检测是一个分类和回归都有的一个任务。
通过混淆矩阵(TP, TN, FP, FN),可以计算出 Precision ( P ), Recall ( R ), Accuracy, F1-Score;
IOU 预测的 bbox 和 GT box的交并比.
P-R曲线: P和R越高越好,但一般是矛盾的,PR曲线下方的面积AUC(Area Under Curve) 越大说明越好。目标检测中的P-R通过IOU的阈值判断TP, TN, FP, FN,进而去计算。
AP(Average Precision): 针对单一类别的,P-R曲线下面的面积(需要对PR曲线平滑处理,即取每个Recall值的时候,选择对应点的Precision的右侧最大的Precision值)。
A P I o U = . 50 AP^{IoU=.50} APIoU=.50 : Pascal VOC,计算混淆矩阵、P-R的 IOU 阈值使用的是0.5
A P I o U = . 75 AP^{IoU=.75} APIoU=.75 : Pascal VOC,计算混淆矩阵、P-R的 IOU 阈值使用的是0.75,比较严格的评价指标
AP: I O U = . 50 : . 05 : . 95 IOU=.50:.05:.95 IOU=.50:.05:.95,IOU阈值从0.5—0.95,按照间隔为0.05取10个IOU阈值的值求得的AP的均值。目前使用比较广泛的一种。
AP(根据像素尺寸大小分别计算AP): 小目标: 3 2 2 32^2 322 ; 中目标: 3 2 2 − 9 6 2 32^2-96^2 322−962;大目标: 9 6 2 96^2 962
AP(根据每张图设定的检测目标个数分别计算AP): 最大目标个数:1,10,100等
mAP: 针对所有类别,即 (所有类别的AP和) / (类别总数)
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=()):
"""计算每个类别的ap
tp:根据iou阈值计算的true positive, ndarray, [n, 10],
10表示range[0.5, 0.95],间隔0.05取一个iou阈值,预测与标签超过这个iou阈值才为tp
conf:置信度,ndarray, [n, 1]
pred_cls:预测类别,ndarray, [n, 1]
plot:是否画[email protected]的PR曲线
"""
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
# Arguments
tp: True positives (nparray, nx1 or nx10).
conf: Objectness value from 0-1 (nparray).
pred_cls: Predicted object classes (nparray).
target_cls: True object classes (nparray).
plot: Plot precision-recall curve at [email protected]
save_dir: Plot save directory
# Returns
The average precision as computed in py-faster-rcnn.
"""
# Sort by objectness
# 将tp,conf,pred_cls按照置信度从大到小排序
i = np.argsort(-conf)
tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
# Find unique classes
# 将target_cls去重,获得类别
unique_classes = np.unique(target_cls)
# 获得类别数
nc = unique_classes.shape[0] # number of classes, number of detections
# Create Precision-Recall curve and compute AP for each class
# 初始化坐标x,y
px, py = np.linspace(0, 1, 1000), [] # for plotting
# 初始化指标,ap,precision,recall
ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
# 对每个类别处理
for ci, c in enumerate(unique_classes):
# 选取类别为c的索引
i = pred_cls == c
# c类别标签的数量
n_l = (target_cls == c).sum() # number of labels
# c类别预测的数量
n_p = i.sum() # number of predictions
if n_p == 0 or n_l == 0:
continue
else:
# Accumulate FPs and TPs
# 累计计算fp,tp
fpc = (1 - tp[i]).cumsum(0)
tpc = tp[i].cumsum(0)
# Recall
# 计算recall
recall = tpc / (n_l + 1e-16) # recall curve
# 插值,方便绘制基于iou_thres=0.5的召回曲线
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases
# Precision
# 计算precision
precision = tpc / (tpc + fpc) # precision curve
# 插值,方便绘制基于iou_thres=0.5的准确率曲线
p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score
# AP from recall-precision curve
# 根据precision与recall计算ap
for j in range(tp.shape[1]):
ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
if plot and j == 0:
py.append(np.interp(px, mrec, mpre)) # precision at [email protected]
# Compute F1 (harmonic mean of precision and recall)
# 根据precision与recall计算f1值
f1 = 2 * p * r / (p + r + 1e-16)
# 画PR曲线,F1曲线,Precision, recall曲线(后三个的横坐标x为置信度)
if plot:
plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names)
plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')
plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
i = f1.mean(0).argmax() # max F1 index
return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')
def compute_ap(recall, precision):
"""根据precision与recall计算ap, 计算PR曲线下的面积"""
""" Compute the average precision, given the recall and precision curves
# Arguments
recall: The recall curve (list)
precision: The precision curve (list)
# Returns
Average precision, precision curve, recall curve
"""
# Append sentinel values to beginning and end
mrec = np.concatenate(([0.], recall, [recall[-1] + 0.01]))
mpre = np.concatenate(([1.], precision, [0.]))
# Compute the precision envelope
# np.maximum.accumulate 计算数组的累计最大值
mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
# Integrate area under curve
method = 'interp' # methods: 'continuous', 'interp'
if method == 'interp':
x = np.linspace(0, 1, 101) # 101-point interp (COCO)
# np.trapz求积分, 求得PR曲线下的面积
ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
else: # 'continuous'
i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
return ap, mpre, mrec
ps:本博客仅供自己复习理解,不具其他人可参考,本博客参考了大量的优质资源,侵删。