ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 

连接:

目标检测评价标准mAP - 知乎 (zhihu.com)

1.混淆矩阵

                       真实值(true)    真实值(false)

预测值(true)TP   (True Positive)     FP(False Positive)

预测值(false)FN(False Negative)    TN(True Negative)

recall = TP/(FN+TP)  「查全率」:所有的正样本是不是都被检测出来了。比如在肿瘤预测场景中,要求模型有更高的recall,不能放过每一个肿瘤

precision = TP/(TP+FP)「查准率] :在所有检测出的正样本中是不是实际都为正样本。比如在垃圾邮件判断等场景中,要求有更高的precision,确保放到回收站的都是垃圾邮件

ImageNet的评价标准。但具体到单个类别,如果recall比较高,但precision较低,比如大部分的汽车都被识别出来了,但把很多卡车也误识别为了汽车,这时候对应一个原因。如果recall较低,precision较高,比如检测出的飞机结果很准确,但是有很多的飞机没有被识别出来,这时候又有一个原因

在mAP计算中

1.干活的时候confidence选择比较高,一般来说为0.5,0.3

2.测量mAP的是时候比较低一般为0.001,0.01之类的非常小(统一标准,保留多一点框)

手写mAP

第一步match_table

match_table格式为

[confidence(置信度), matched_iou(类间iou), matched_groundtruth_index(最匹配真是图id), image_id(图片id)]
代码如下
 
def nms_as_class(bboxes, threshold, class_index=-1, confidence_index=-2):
    boxasclass = {}
    for box in bboxes:
        classes = box[class_index]
        if classes not in boxasclass:
            boxasclass[classes] = []
        boxasclass[classes].append(box)

    output = []
    for key in boxasclass:
        result = nms(boxasclass[key], threshold, confidence_index)
        output.extend(result)
    return output
View Code

 

# 5.计算AP@0.5的precision、recall曲线
* 0.5指IoU的阈值是0.5,如果需要计算AP@0.75,直接指定阈值0.75即可
* 当某一个detection的最优匹配的IoU > threshold时,被认为检测成功,
* 对应的groundtruth如果第一次出现,则该detection被认为是true_positive,否则是false_positive
* 当选中一个true_positive时,其对应的groundtruth设置为见过(seen),以后任何detection见到他都不能够匹配为true_positive
* Recall的计算公式为:
 Recall={TP}/{TP + FN} = {TP}/ground truths}
* Precision的计算公式为:
Precision={TP}/{TP + FP} = {TP}/{\#detections}
* 对于检测任务而言,真实值只有Positive,没有Negative。预测值只有Positive,没有Negative
- 也因此,咱们只有FN、FP、TP没有TN
- 所以,TP + FN,就是ground truths的数量
- 所以,TP + FP,就是detections的数量

 

 代码如下

# 计算AP@0.5 代表的含义是iou阈值为0.5的时候Precision,Recall值
# 如果有出现的框我们只匹配一次
iou_threshold = 0.5
num_detection = len(matched_table)
true_positive = np.zeros((num_detection,))
# 因为不是全局唯一matched_index,每个图片对应一些match_index
groundtruth_seen_map = {item[3]: set() for item in matched_table}
for detection_index in range(num_detection):
    confidence, iou_value, match_index, image_id = matched_table[0]
    if iou_value >= iou_threshold:
        groundtruth_seen_map_set = groundtruth_seen_map[image_id]
        if match_index not in groundtruth_seen_map_set:
            groundtruth_seen_map_set.add(match_index)
            true_positive[detection_index] = 1
num_predicts = np.arange(1, len(true_positive) + 1)
accumulate_true_positive = np.cumsum(true_positive)
precision = accumulate_true_positive / num_predicts
recall = accumulate_true_positive / sum_groundtruth
View Code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

------------恢复内容结束------------

posted on 2022-12-08 14:27  哦哟这个怎么搞  阅读(20)  评论(0编辑  收藏  举报