本文实现了mAP函数,其中计算了精确率(Precision),召回率(Recall)
首先需要了解mAP的概念和计算方式.
具体原理可以查看:https://www.cnblogs.com/lixiunan/articles/9566627.html
以下为参考上文的代码实现,最好配合上文一起理解.
1.recall-precious
def calc_detection_voc_prec_rec(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels,
gt_difficults=None, iou_thresh=0.5):
'''
输入为pred_bboxes(total_image,N,4)
gt_bboxes(total_image,M,4)
'''
# 存放每一类真值标签个数,用于计算recall
gt_number = np.zeros(opt.classes)
# 用于存储预测窗口是否正确TP
TP_FP = defaultdict(list)
# 用于存储预测窗口的置信度
pred_score_all = defaultdict(list)
for i in range(len(gt_labels)):
# 取出每一个图像中的预测和真值
pred_bbox, pred_label, pred_score, gt_bbox, gt_label = pred_bboxes[
i], pred_labels[i], pred_scores[i], gt_bboxes[i], gt_labels[i]
pred_bbox = np.array(pred_bbox)
pred_label = np.array(pred_label)
pred_score = np.array(pred_score)
gt_difficult = np.zeros(gt_bbox.shape[0], dtype=bool)
# 用于计算每一类中预测窗口的TP
for l in np.unique(np.concatenate((pred_label, gt_label)).astype(int)):
# 选出该图像中的l类预测和真值
gt_bbox_l = gt_bbox[gt_label == int(l)]
gt_label_l = gt_label[gt_label == int(l)]
if len(pred_bbox) == 0:
continue
pred_bbox_l = pred_bbox[pred_label == int(l)]
pred_score_l = pred_score[pred_label == int(l)]
pred_label_l = pred_label[pred_label == int(l)]
pred_score_all[l].extend(pred_score_l)
if len(pred_bbox_l) == 0:
continue
if len(gt_bbox_l) == 0:
TP_FP[l].extend(np.zeros(len(pred_label_l)))
continue
# 计算每一类真值标签个数
gt_number[l] += len(gt_bbox_l)
# 计算iou(N,M) N为预测bbox大小,M为真值bbox大小
iou = bbox_iou2(pred_bbox_l, gt_bbox_l)
# pred_label_l.shape == pre_index.shape
# 找到预测标签与所有真值标签中iou最大值
pre_index = iou.argmax(axi