介绍
在目标检测中,通常采用mAP指标来表达检测模型的性能表现,而mAP指标的计算具体包含几个步骤,其中重要的一步就是TP(真正,即命中)与TP(假正)的计算,具体根据mmdetection中的评估代码mean_ap.py进行分析。
def tpfp_func(det_bboxes,gt_bboxes,iou_thr=0.5):
"""Check if detected bboxes are true positive or false positive.
Args:
det_bbox (ndarray): Detected bboxes of this image, of shape (m, 5).
gt_bboxes (ndarray): GT bboxes of this image, of shape (n, 4).
iou_thr (float): IoU threshold to be considered as matched.
Default: 0.5.
Returns:
tuple[np.ndarray]: (tp, fp) whose elements are 0 and 1. The shape of
each array is (num_scales, m).
"""
num_dets = det_bboxes.shape[0]
num_gts = gt_bboxes.shape[0]
tp = np.zeros((num_dets), dtype=np.float32)
fp = np.zeros((num_dets), dtype=np.float32)
# if there is no gt bboxes in this image, then all det bboxes are false positives
if gt_bboxes.shape[0] == 0:
fp[...] = 1
return tp, fp
ious = bbox_overlaps(det_bboxes, gt_bboxes)
# for each det, the max iou with all gts
ious_max = ious.max(axis=1)
# for each det, which gt overlaps most with it
ious_argmax = ious.argmax(axis=1)
# sort all dets in descending order by scores
sort_inds = np.argsort(-det_bboxes[:, -1])
gt_covered = np.zeros(num_gts, dtype=bool)
for i in sort_inds:
if ious_max[i] >= iou_thr:
matched_gt = ious_argmax[i]
if not gt_covered[matched_gt]:
gt_covered[matched_gt] = True
tp[i] = 1
else:
fp[i] = 1
else:
fp[i] = 1
return tp, fp
上述代码为简化版本,去除了面积约束条件以不予考虑的真实框条件(gt_bboxes_ignore),基础约束条件是预测框与标注框具有相同的类别,具体步骤分析如下:
(1)输入:单张图像单个类别的预测框与标注框
(2)初始化tp,fp为0,分别用于预测框真假情况统计
(3)计算预测框与标注框的最大匹配iou值以及对应的标注框索引
(4)按照预测分数排序预测框,方便优先使用置信度更高的预测框
(5)初始化每个真实标注框匹配情况统计量
(6)按照预测分数从高到底,逐一验证其应当归属于tp与fp中的哪个
(7)阈值约束,若不满足阈值要求,则将此种低质量预测归属为fp,若满足阈值要求,且是真实标注框的初次最佳匹配,则为tp,否则表明预测框为具有较低分数的重复预测框,归于fp。
思考分析:
基于tp与fp,我们只能计算出检测模型的准确率(tp/(tp+fp)),还无法获知召回率,通过结合真实标注框的数量gt_nums,我们可以计算出召回率(tp/gt_nums)。
从召回情况,我们可以分析出此该图像某一类别的漏检情况(与标注框的iou极低,甚至为0)。