utils
最后更新
luxonis_train.attached_modules.metrics.utils关键点指标的辅助函数。merge_bbox_kpt_targets 将边界框标签和关键点标签合并到一个张量中。fix_empty_tensor 将空的单维张量重塑为 [1, 0] 的形状。函数
fix_empty_tensor
Python
1def fix_empty_tensor(tensor: Tensor) -> Tensor:[1, 0] 的形状。关键点指标在将其附加到其列表状态的张量上调用此函数。形状为 [0] 的空张量可能会在 DDP 模式下导致问题。该函数返回所有其他张量不变。示例
Pycon
1>>> import torch
2>>> fix_empty_tensor(torch.zeros(0)).shape
3torch.Size([1, 0])
4>>> fix_empty_tensor(torch.zeros(0, 3)).shape
5torch.Size([0, 3])tensor(Tensor): 要检查的张量。
Tensor: 当tensor为空且为一维时,形状为[1, 0]的视图,否则为tensor本身。
merge_bbox_kpt_targets
Python
1def merge_bbox_kpt_targets(target_boundingbox: Tensor, target_keypoints: Tensor, *, device: torch.device | None = None) -> Tensor:xywh(其中 x 和 y 是左上角)转换为 xyxy。它保留了归一化坐标。结果的第 i 行合并了两个标签的第 i 行。示例
Pycon
1>>> import torch
2>>> boxes = torch.tensor([[0.0, 3.0, 0.25, 0.25, 0.5, 0.25]])
3>>> keypoints = torch.tensor([[0.0, 0.5, 0.375, 2.0]])
4>>> merge_bbox_kpt_targets(boxes, keypoints).tolist()
5[[0.0, 3.0, 0.25, 0.25, 0.75, 0.5, 0.5, 0.375, 2.0]]target_boundingbox(Tensor): 框标签,形状为[N, 6],行格式为[batch_index, class, x, y, w, h]。target_keypoints(Tensor): 关键点标签,形状为[N, 1 + 3K],行格式为[batch_index, x_1, y_1, v_1, ..., x_K, y_K, v_K],与target_boundingbox的行顺序相同。device(torch.device | None): 结果的设备。None选择torch的默认设备。
Tensor: 形状为[N, 6 + 3K]的新浮点张量,行格式为[batch_index, class, x1, y1, x2, y2, x_1, y_1, v_1, ...]。