utils
最后更新
luxonis_train.attached_modules.metrics.confusion_matrix.utils混淆矩阵的辅助函数:实例掩码的合并和马修斯相关系数。函数
compute_mcc
Python
1def compute_mcc(cm: Tensor) -> Tensor:-1 和 1 之间。当所有计数都在对角线上且至少有两个类有计数时,该值为 1。当计数总和为 0 或分母为 0 时,该函数返回 0。转置矩阵给出相同的值。示例
Pycon
1>>> import torch
2>>> round(
3... compute_mcc(torch.tensor([[5.0, 1.0], [2.0, 2.0]])).item(), 4
4... )
50.3563
6>>> compute_mcc(torch.eye(3)).item()
71.0
8>>> compute_mcc(torch.tensor([[0.0, 2.0], [2.0, 0.0]])).item()
9-1.0
10>>> compute_mcc(torch.zeros(2, 2)).item()
110.0cm(Tensor): 形状为[K, K]的计数方阵。混淆矩阵传递其计数的float32副本。
Tensor: 作为浮点张量的标量 MCC,位于cm的设备上。
preprocess_instance_masks
Python
1def preprocess_instance_masks(predicted_boundingbox: list[Tensor], predicted_instance_segmentation: list[Tensor], target_boundingbox: Tensor, target_instance_segmentation: Tensor, n_classes: int, height: int, width: int, device: torch.device) -> tuple[Tensor, Tensor]:示例
批次包含一个图像和两个类。类
1 的两个预测掩码合并为一个掩码。Pycon
1>>> import torch
2>>> boxes = [
3... torch.tensor([[0, 0, 1, 1, 0.9, 1], [1, 0, 2, 1, 0.8, 1]])
4... ]
5>>> masks = [torch.tensor([[[1, 0]], [[0, 1]]])]
6>>> target_boxes = torch.tensor([[0, 0, 0.0, 0.0, 0.5, 1.0]])
7>>> target_masks = torch.tensor([[[1, 0]]])
8>>> pred, target = preprocess_instance_masks(
9... boxes,
10... masks,
11... target_boxes,
12... target_masks,
13... n_classes=2,
14... height=1,
15... width=2,
16... device=torch.device("cpu"),
17... )
18>>> pred.tolist()
19[[[[0, 0]], [[1, 1]]]]
20>>> target.tolist()
21[[[[1, 0]], [[0, 0]]]]predicted_boundingbox(list[Tensor]): 每个图像的预测框,形状为[M_i, 6],类在第5列。列表的长度是批次大小。predicted_instance_segmentation(list[Tensor]): 每个图像的预测掩码,形状为[M_i, height, width],每个框一个。target_boundingbox(Tensor): 批次的目标框,形状为[N, C],批次索引在第0列,类在第1列。target_instance_segmentation(Tensor): 目标掩码,形状为[N, height, width],每个目标框一个。n_classes(int): 类的数量。height(int): 掩码的高度。width(int): 掩码的宽度。device(torch.device): 新掩码的设备。
tuple[Tensor, Tensor]: 预测和目标语义掩码,每个形状为[B, n_classes, height, width],其中B是predicted_boundingbox的长度。它们在对象处为1,在其他地方为0。预测掩码采用predicted_instance_segmentation[0]的 dtype。目标掩码采用target_instance_segmentation的 dtype。