detection_confusion_matrix
最后更新
luxonis_train.attached_modules.metrics.confusion_matrix.detection_confusion_matrix通过 IoU 将预测与标签匹配的边界框混淆矩阵。类
DetectionConfusionMatrix
输入::boundingbox(list[Tensor]): 每张图像 [Mi, 6],[x1, y1, x2, y2, conf, class],像素target_boundingbox(Tensor): [N, 6],[batch, class, x, y, w, h],xywh归一化
输出::mcc(Tensor): 整个矩阵的标量 MCC,请参阅 compute_mccconfusion_matrix(Tensor): [nclasses + 1, nclasses + 1] 计数,行是目标,最后一行和列是背景
公式:: 该指标匹配每张图像的框。使用iou_threshold中的 IoU 阈值 t,当 IoU > t 时,目标框和预测匹配。预测的类别不影响匹配。每个目标框按行顺序选择它匹配的第一个预测,而不是 IoU 最高的预测。几个目标框可以选择相同的预测。然后:- 匹配的目标框在其预测类别的列中,为其类别行添加
1。 - 未匹配的目标框在其类别行中,为背景列添加
1。 - 没有目标框选择的预测在其类别列中,为背景行添加
1。 - 没有目标框且没有预测的图像在单元格
[n_classes, n_classes]中添加1。 在没有目标框、没有预测或没有匹配的图像中,每个类别最多计数一次。 例如,在没有目标框的图像中,三个类别0的预测添加1,而不是3,到背景行。
- 匹配的目标框在其预测类别的列中,为其类别行添加
参考
- 来源:本项目。
- 许可证:Apache-2.0 (本项目)
注意
该指标忽略置信度列,因此节点的每个预测都计数。FomoConfusionMatrix 和 InstanceSegmentationConfusionMatrix 扩展了此类别。
示例
附加到
model.nodes 中的 EfficientBBoxHead:Yaml
1- name: EfficientBBoxHead
2 inputs: [RepPANNeck]
3 metrics:
4 - name: DetectionConfusionMatrix方法
init
Python
1def __init__(iou_threshold: float = 0.45, **kwargs):confusion_matrix 状态。状态是一个形状为 [n_classes + 1, n_classes + 1] 的零 int64 张量。分布式运行会添加所有进程的状态。类别数量来自节点,因此没有节点的指标会引发 RuntimeError。参数iou_threshold(float): 目标框和预测的 IoU 必须超过此值才能匹配。**kwargs: 转发给 BaseMetric 的关键字参数,例如node。
compute
Python
1def compute(self) -> dict[str, Tensor]:示例
批次包含一张图像,其中有一个类别
0 的目标框。第一个预测覆盖它。第二个类别 1 的预测不匹配任何目标框。SimpleNamespace 代表节点。Pycon
1>>> import torch
2>>> from types import SimpleNamespace
3>>> node = SimpleNamespace(
4... task=None,
5... n_classes=2,
6... original_in_shape=torch.Size([3, 10, 10]),
7... )
8>>> metric = DetectionConfusionMatrix(node=node)
9>>> target = torch.tensor([[0, 0, 0.0, 0.0, 0.5, 0.5]])
10>>> boxes = [
11... torch.tensor([[0, 0, 5, 5, 0.9, 0], [6, 6, 9, 9, 0.8, 1]])
12... ]
13>>> metric.update(boxes, target)
14>>> metric.compute()["confusion_matrix"].tolist()
15[[1, 0, 0], [0, 0, 0], [0, 1, 0]]dict[str, Tensor]: 包含两个键的字典。"mcc"包含整个矩阵的标量 MCC,包括背景,请参阅 compute_mcc。"confusion_matrix"包含int64计数,形状为[n_classes + 1, n_classes + 1]。行是目标类别,列是预测类别,索引n_classes是背景。
update
Python
1def update(boundingbox: list[Tensor], target_boundingbox: Tensor):xyxy 像素,其高度和宽度为 BaseAttachedModule.original_in_shape。它将转换后的框写入 target_boundingbox。BaseMetric.run_update 传递一个克隆,因此批次的标签不会改变。然后,该方法匹配每张图像的框,如类文档字符串所述。参数boundingbox(list[Tensor]): 每张图像的预测框,形状为[M_i, 6],以[x1, y1, x2, y2, conf, class]像素表示。列表的长度是批次大小。target_boundingbox(Tensor): 批次的boundingbox标签,形状为[N, 6],以[batch_index, class, x, y, w, h]表示。值已归一化,x和y是左上角。