Python 什么';计算目标检测混淆矩阵的正确方法是什么?

Python 什么';计算目标检测混淆矩阵的正确方法是什么?,python,object-detection,confusion-matrix,Python,Object Detection,Confusion Matrix,我试图为我的目标检测模型计算一个混淆矩阵。然而,我似乎偶然发现了一些陷阱。我目前的方法是将每个预测框与每个地面真相框进行比较。如果他们有一个IoU>某个阈值,我会将预测插入混淆矩阵。插入后,我删除预测列表中的元素,并继续下一个元素 因为我还希望将错误分类的建议插入混淆矩阵,所以我将IoU低于阈值的元素视为与背景混淆。我当前的实现如下所示: def insert_into_conf_m(true_labels, predicted_labels, true_boxes, predicted_box

我试图为我的目标检测模型计算一个混淆矩阵。然而,我似乎偶然发现了一些陷阱。我目前的方法是将每个预测框与每个地面真相框进行比较。如果他们有一个IoU>某个阈值,我会将预测插入混淆矩阵。插入后,我删除预测列表中的元素,并继续下一个元素

因为我还希望将错误分类的建议插入混淆矩阵,所以我将IoU低于阈值的元素视为与背景混淆。我当前的实现如下所示:

def insert_into_conf_m(true_labels, predicted_labels, true_boxes, predicted_boxes):
    matched_gts = []
    for i in range(len(true_labels)):
        j = 0
        while len(predicted_labels) != 0:
            if j >= len(predicted_boxes):
                break
            if bb_intersection_over_union(true_boxes[i], predicted_boxes[j]) >= 0.7:
                conf_m[true_labels[i]][predicted_labels[j]] += 1
                del predicted_boxes[j]
                del predicted_labels[j]
            else:
                j += 1
        matched_gts.append(true_labels[i])
        if len(predicted_labels) == 0:
            break
    # if there are groundtruth boxes that are not matched by any proposal
    # they are treated as if the model classified them as background
    if len(true_labels) > len(matched_gts):
        true_labels = [i for i in true_labels if not i in matched_gts or matched_gts.remove(i)]
        for i in range(len(true_labels)):
            conf_m[true_labels[i]][0] += 1

    # all detections that have no IoU with any groundtruth box are treated
    # as if the groundtruth label for this region was Background (0)
    if len(predicted_labels) != 0:
        for j in range(len(predicted_labels)):
            conf_m[0][predicted_labels[j]] += 1
[0.0, 0.36, 0.34, 0.30]
[0.0, 0.29, 0.30, 0.41]
[0.0, 0.20, 0.47, 0.33]
[0.0, 0.23, 0.19, 0.58]
行规格化矩阵如下所示:

def insert_into_conf_m(true_labels, predicted_labels, true_boxes, predicted_boxes):
    matched_gts = []
    for i in range(len(true_labels)):
        j = 0
        while len(predicted_labels) != 0:
            if j >= len(predicted_boxes):
                break
            if bb_intersection_over_union(true_boxes[i], predicted_boxes[j]) >= 0.7:
                conf_m[true_labels[i]][predicted_labels[j]] += 1
                del predicted_boxes[j]
                del predicted_labels[j]
            else:
                j += 1
        matched_gts.append(true_labels[i])
        if len(predicted_labels) == 0:
            break
    # if there are groundtruth boxes that are not matched by any proposal
    # they are treated as if the model classified them as background
    if len(true_labels) > len(matched_gts):
        true_labels = [i for i in true_labels if not i in matched_gts or matched_gts.remove(i)]
        for i in range(len(true_labels)):
            conf_m[true_labels[i]][0] += 1

    # all detections that have no IoU with any groundtruth box are treated
    # as if the groundtruth label for this region was Background (0)
    if len(predicted_labels) != 0:
        for j in range(len(predicted_labels)):
            conf_m[0][predicted_labels[j]] += 1
[0.0, 0.36, 0.34, 0.30]
[0.0, 0.29, 0.30, 0.41]
[0.0, 0.20, 0.47, 0.33]
[0.0, 0.23, 0.19, 0.58]
有没有更好的方法来生成目标检测系统的混淆矩阵?或任何其他更适合的度量?

根据TensorFlow对象检测API生成的detections.record文件计算混淆矩阵。解释这个脚本是如何工作的

总之,以下是文章中的算法概述:

  • 对于每个检测记录,算法从输入文件中提取地面真值框和类,以及检测到的数据 盒子、班级和分数

  • 仅考虑分数大于或等于0.5的检测。低于此值的任何内容都将被丢弃

  • 对于每个地面真值框,该算法使用每个检测到的框生成IoU(联合上的交集)。如果出现以下情况,则会找到匹配项: 两个框的IoU都大于或等于0.5

  • 将修剪匹配列表以删除重复项(与多个检测框匹配的地面真相框,反之亦然)。如果 如果存在重复项,则始终选择最佳匹配项(较大IoU)

  • 将更新混淆矩阵,以反映地面真相和探测结果之间的匹配

  • 属于基本真相一部分但未被检测到的对象将在矩阵的最后一列(对应于 地面真理课)。已检测到但不属于的对象 混淆矩阵计算在矩阵的最后一行(在 与检测到的类相对应的列)


  • 您还可以查看更多信息。

    您能否解释如何使用提供的脚本计算每个类的真阳性、假阳性、假阴性和真阴性的数量