python - 计算用于对象检测的混淆矩阵的正确方法是什么?

标签 python object-detection confusion-matrix

我正在尝试为我的对象检测模型计算一个混淆矩阵。但是,我似乎偶然发现了一些陷阱。我目前的方法是将每个预测框与每个地面实况框进行比较。如果他们的 IoU > 某个阈值,我将预测插入混淆矩阵。插入后,我删除预测列表中的元素并移至下一个元素。

因为我也想将误分类的proposals插入到混淆矩阵中,所以我将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 ground-truth 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]

有没有更好的方法来为目标检测系统生成混淆矩阵?或者其他更合适的指标?

最佳答案

Here is a script从 TensorFlow 对象检测 API 生成的 detections.record 文件计算混淆矩阵。 Here is the article解释这个脚本是如何工作的。

总而言之,这里是文章中算法的概要:

  1. For each detection record, the algorithm extracts from the input file the ground-truth boxes and classes, along with the detected boxes, classes, and scores.

  2. Only detections with a score greater or equal than 0.5 are considered. Anything that’s under this value is discarded.

  3. For each ground-truth box, the algorithm generates the IoU (Intersection over Union) with every detected box. A match is found if both boxes have an IoU greater or equal than 0.5.

  4. The list of matches is pruned to remove duplicates (ground-truth boxes that match with more than one detection box or vice versa). If there are duplicates, the best match (greater IoU) is always selected.

  5. The confusion matrix is updated to reflect the resulting matches between ground-truth and detections.

  6. Objects that are part of the ground-truth but weren’t detected are counted in the last column of the matrix (in the row corresponding to the ground-truth class). Objects that were detected but aren’t part of the confusion matrix are counted in the last row of the matrix (in the column corresponding to the detected class).

你也可以看看at the script获取更多信息。

关于python - 计算用于对象检测的混淆矩阵的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110545/

相关文章:

python - 如何以张量形式从 tensorflow 或 Keras 中的混淆矩阵获得准确性?

keras - 使用 ImageDataGenerator 时如何获取预测类

python - 极坐标 DataFrame 行之间的分割值

python - 串联两个 numpy 数组的每一行组合

python - 通过使用其质心之间的欧式距离来提高检测精度

tensorflow - 使用 tensorflow 对象检测减少误报的方法有哪些?

python - 对多行使用正则表达式

某些文本中的日期的python正则表达式,由两个关键字括起来

tensorflow - 如何为以 encoded_image_string_tensor 作为输入的 tensorflow 模型编码输入

r - 对象中的缺失值 - R 中的随机森林混淆矩阵