我正在尝试为我的对象检测模型计算混淆矩阵。然而,我似乎偶然发现了一些陷阱。我目前的方法是将每个预测的框与每个groundtruth框进行比较。如果他们有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]
有没有更好的方法来为对象检测系统生成混淆矩阵?或者更合适的其他指标?
答案 0 :(得分:3)
Here is a script,以从TensorFlow对象检测API生成的detections.record文件计算混淆矩阵。 Here is the article解释该脚本的工作原理。
总而言之,这是文章中算法的概述:
对于每个检测记录,算法从输入文件中提取地面真相框和类以及检测到的 框,类和分数。
仅考虑得分大于或等于0.5的检测。低于此值的所有内容都将被丢弃。
对于每个地面真框,该算法会为每个检测到的框生成IoU(联合交集)。如果找到匹配项 两个方框的IoU均大于或等于0.5。
修剪匹配项列表以删除重复项(与多个检测框匹配的地面真相框,反之亦然)。如果 有重复项,则始终选择最佳匹配项(更大的IoU)。
更新了混淆矩阵,以反映地面真相与检测结果之间的匹配结果。
属于地面真相但未被检测到的对象计入矩阵的最后一列(在对应于 真相课)。检测到但不属于的对象 混淆矩阵在矩阵的最后一行(在 列对应于检测到的类别)。
您也可以查看at the script了解更多信息。