ITi*_*ger 7 python object-detection confusion-matrix
我正在尝试为我的对象检测模型计算一个混淆矩阵。但是我似乎偶然发现了一些陷阱。我目前的方法是将每个预测框与每个地面真实框进行比较。如果它们的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
Run Code Online (Sandbox Code Playgroud)
行规范化矩阵如下所示:
[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]
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法来为对象检测系统生成混淆矩阵?还是其他更合适的指标?
这是一个脚本,用于根据 TensorFlow 对象检测 API 生成的 detections.record 文件计算混淆矩阵。这是解释此脚本如何工作的文章。
总而言之,这里是文章中算法的大纲:
对于每个检测记录,算法从输入文件中提取真实框和类,以及检测到的框、类和分数。
仅考虑得分大于或等于 0.5 的检测。低于此值的任何内容都将被丢弃。
对于每个ground-truth box,算法为每个检测到的box生成IoU(Intersection over Union)。如果两个框的 IoU 大于或等于 0.5,则找到匹配。
匹配列表被修剪以删除重复项(与多个检测框匹配的真实框,反之亦然)。如果有重复,则始终选择最佳匹配(更大的 IoU)。
更新混淆矩阵以反映真实情况和检测结果之间的匹配结果。
属于地面实况但未被检测到的对象计入矩阵的最后一列(在与地面实况类相对应的行中)。检测到但不属于混淆矩阵的对象计入矩阵的最后一行(在与检测到的类别对应的列中)。
您还可以查看脚本以获取更多信息。