precision_seg_bbox_head
最后更新
luxonis_train.nodes.heads.precision_seg_bbox_headYOLOv8 实例分割头,它预测共享原型集上的掩码系数。类
PrecisionSegmentBBoxHead
输入::inputs(list[Tensor]): 每个尺度的 [B, Ci, Hi, Wi],输入节点的最后n_heads个输出
输出::- 训练:
features(list[Tensor]): 每个尺度的 [B, 4⋅regmax + nclasses, Hi, Wi],logitsprototypes(Tensor): [B, nmasks, 2H0, 2W0]mask_coefficients(Tensor): [B, nmasks, N],N = ∑iHiWi
- 评估:
features(list[Tensor]): 每个尺度的 [B, 4⋅regmax + nclasses, Hi, Wi],logitsprototypes(Tensor): [B, nmasks, 2H0, 2W0]mask_coefficients(Tensor): [B, nmasks, N]boundingbox(list[Tensor]): 每张图像的 [Mi, 6],[x1, y1, x2, y2, score, class],像素instance_segmentation(list[Tensor]): 每张图像的 [Mi, H, W],二进制detections_pre_nms(Tensor): [B, N, 5 + nclasses + nmasks],仅在请求时
- 导出:
boundingbox(list[Tensor]): 每个尺度的 [B, 5 + nclasses, Hi, Wi],DFL 解码masks(list[Tensor]): 每个尺度的 [B, nmasks, Hi, Wi],掩码系数prototypes(Tensor): [B, nmasks, 2H0, 2W0]
- 训练:
参考
- 来源:重新实现了 Real-Time Flying Object Detection with YOLOv8 和 YOLOv6: A Single-Stage Object Detection Framework for Industrial Applications。
- 许可证:Apache-2.0 (本项目)
注意
每个尺度在其 PreciseDecoupledBlock 旁边获得一个掩码分支。该分支为每个锚点预测
n_masks 个掩码系数。SegProto 从第一个输入特征图构建 n_masks 个原型,分辨率是其两倍。在评估模式下,NMS 保留每个框的系数。然后 refine_and_apply_masks 将原型与系数结合,并将掩码裁剪到框中。它还将掩码调整为模型输入图像的大小。当像素值大于 0 时,该像素在掩码中。此条件等同于 σ(x) > 0.5,其中 σ 是 sigmoid。训练模式和导出模式跳过 NMS 和 refine_and_apply_masks。导出模式的 masks 输出包含掩码系数。变体:: 无。通过params配置节点。
示例
配置中
model.nodes 部分的节点条目:Yaml
1- name: PrecisionSegmentBBoxHead
2 inputs: [RepPANNeck]兼容::- 必需标签:
boundingboxinstance_segmentation
- 使用者: InstanceSegmentationModel
- 损失: PrecisionDFLSegmentationLoss
- 指标:
- 可视化器: InstanceSegmentationVisualizer
- 导出解析器:
YOLOExtendedParser
- 必需标签:
方法
init
Python
1def __init__(n_heads: Literal[2, 3, 4] = 3, n_masks: int = 32, n_proto: int = 64, conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 300, **kwargs):3x3 ConvBlock 层,带有批归一化和 SiLU。接着是一个 1x1 卷积,输出通道数为 n_masks。每个掩码分支的隐藏宽度是 n_masks 和 in_channels[0] // 4 中较大的一个。in_channels[0] 是第一个尺度的通道数。SegProto 也读取第一个特征图。参数n_heads(Literal[2, 3, 4]): 尺度数量。头部读取输入节点的最后n_heads个输出。attach_index参数替换此选择。它必须选择一个范围或"all"。整数索引会使构造函数引发TypeError。当头部获得的输出较少时,它会记录警告并使用该数量。当attach_index选择更多输出时,构造函数会引发ValueError。默认为3。n_masks(int): 原型掩码的数量。它也是每个锚点的掩码系数数量。默认为32。n_proto(int): SegProto 原型生成器的隐藏通道数。默认为64。conf_thres(float): NMS 仅保留最大类别分数高于此值的框。该值必须在[0, 1]之间。默认为0.25。iou_thres(float): 当一个框与同类别且分数更高的框的 IoU 高于此值时,NMS 会移除该框。该值必须在[0, 1]之间。默认为0.45。max_det(int): NMS 为每张图像保留的最大框数。默认为300。**kwargs: PrecisionBBoxHead 的关键字参数,例如reg_max,以及 BaseNode 的关键字参数。它们必须包含original_in_shape、通过input_shapes或in_sizes的输入大小,以及通过n_classes或dataset_metadata的类别计数。
forward
Python
1def forward(inputs: list[Tensor]) -> Packet[Tensor]:- 导出模式:
"boundingbox"为每个尺度保存一个形状为[B, 5 + n_classes, H_i, W_i]的映射。其通道是步幅单位的距离(l, t, r, b)、最大类别分数和类别分数。当reg_max大于1时,DFL 解码距离。分数是 sigmoid 概率。"masks"为每个尺度保存形状为[B, n_masks, H_i, W_i]的掩码系数。"prototypes"保存形状为[B, n_masks, 2 * H_0, 2 * W_0]的原型。 - 训练模式:
"features"为每个尺度保存形状为[B, 4 * reg_max + n_classes, H_i, W_i]的回归和类别 logits。"prototypes"与导出模式相同。"mask_coefficients"保存所有N个锚点的系数,形状为[B, n_masks, N],其中 N = ∑iHiWi。 - 评估模式:训练模式的键和 NMS 结果。
"boundingbox"为每张图像保存一个形状为[M_i, 6]的张量。每行是[x1, y1, x2, y2, score, class],角点在模型输入图像的像素中。"instance_segmentation"保存这些框的掩码,形状为[M_i, H, W],值为0和1。H和W来自original_in_shape。对于没有框的图像,M_i为0,掩码张量的数据类型为uint8。在调用 BaseDetectionHead.request_detections_pre_nms 后,数据包还包含 NMS 输入"detections_pre_nms",形状为[B, N, 5 + n_classes + n_masks]。
示例
一个新的头部处于训练模式:对于零输入,一个新的头部会给每个类别一个低于
Pycon
1>>> import torch
2>>> from torch import Size
3>>> from luxonis_train.nodes import PrecisionSegmentBBoxHead
4>>> sizes = [Size([1, 8, 16, 16]), Size([1, 16, 8, 8])]
5>>> head = PrecisionSegmentBBoxHead(
6... n_heads=2,
7... n_masks=4,
8... n_proto=8,
9... n_classes=2,
10... input_shapes=[{"features": sizes}],
11... original_in_shape=Size([3, 64, 64]),
12... )
13>>> out = head([torch.zeros(size) for size in sizes])
14>>> out["prototypes"].shape, out["mask_coefficients"].shape
15(torch.Size([1, 4, 32, 32]), torch.Size([1, 4, 320]))conf_thres 的分数。在评估模式下,NMS 因此不保留任何框和掩码:Pycon
1>>> out = head.eval()([torch.zeros(size) for size in sizes])
2>>> boxes, masks = out["boundingbox"], out["instance_segmentation"]
3>>> boxes[0].shape, masks[0].shape
4(torch.Size([0, 6]), torch.Size([0, 64, 64]))inputs(list[Tensor]): 每个尺度的一个特征图,形状为[B, C_i, H_i, W_i]。H_0和W_0是第一个映射的高度和宽度。
Packet[Tensor]: 当前模式的数据包,包含描述中给出的键。
属性
export_output_names
2 * n_heads + 1 个输出。ONNX 导出按输出键对它们进行排序。默认名称遵循此顺序:output1_yolov8到output{n_heads}_yolov8用于"boundingbox"映射。output1_masks到output{n_heads}_masks用于"masks"系数。protos_output用于"prototypes"。
export_output_names 参数包含恰好 n_heads 个名称时,它才会替换默认名称。每次给出默认名称时,头部都会记录警告。该值从不为 None。警告: 包含 n_heads 个名称的参数的名称数量少于导出模型的输出数量。因此,ONNX 导出会记录警告并忽略这些名称。NN Archive 仍然将这 n_heads 个名称列为头部的输出。示例
一个具有两个尺度的头部给出五个默认名称。该示例关闭了日志记录器,因此不显示警告:
Pycon
1>>> from loguru import logger
2>>> from torch import Size
3>>> from luxonis_train.nodes import PrecisionSegmentBBoxHead
4>>> sizes = [Size([1, 8, 16, 16]), Size([1, 16, 8, 8])]
5>>> head = PrecisionSegmentBBoxHead(
6... n_heads=2,
7... n_classes=2,
8... input_shapes=[{"features": sizes}],
9... original_in_shape=Size([3, 64, 64]),
10... )
11>>> logger.disable("luxonis_train")
12>>> head.export_output_names
13['output1_yolov8', 'output2_yolov8', 'output1_masks',
14 'output2_masks', 'protos_output']
15>>> logger.enable("luxonis_train")parser
proto
segmentation_heads
函数
refine_and_apply_masks
Python
1def refine_and_apply_masks(mask_prototypes: Tensor, predicted_masks: Tensor, bounding_boxes: Tensor, height: int, width: int, upsample: bool = False) -> Tensor:upsample,它通过双线性插值将掩码调整为 height 乘以 width。当像素值 x > 0 时,该像素在掩码中。此条件等同于 σ(x) > 0.5,其中 σ 是 sigmoid。插值可以使掩码稍微超出其框。示例
框覆盖
8x8 图像的左上角四分之一,因此掩码保留 4x4 原型的左上角四分之一:Pycon
1>>> import torch
2>>> prototypes = torch.ones(1, 4, 4)
3>>> coefficients = torch.tensor([[1.0]])
4>>> boxes = torch.tensor([[0.0, 0.0, 4.0, 4.0]])
5>>> masks = refine_and_apply_masks(
6... prototypes, coefficients, boxes, height=8, width=8
7... )
8>>> masks[0].int().tolist()
9[[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
10>>> refine_and_apply_masks(
11... prototypes, coefficients, boxes, 8, 8, upsample=True
12... ).shape
13torch.Size([1, 8, 8])mask_prototypes(Tensor): 原型掩码,形状为[n_masks, h, w]。predicted_masks(Tensor): 每个检测的掩码系数,形状为[N, n_masks]。bounding_boxes(Tensor): 每个检测的xyxy框,在height乘以width图像的像素中,形状为[N, 4]。height(int): 图像高度,以像素为单位。width(int): 图像宽度,以像素为单位。upsample(bool): 是否将掩码调整为height乘以width。默认为False。
Tensor: 值为0和1的浮点掩码张量。使用upsample时,其形状为[N, height, width],不使用时为[N, h, w]。当predicted_masks或bounding_boxes没有行时,函数返回形状为[0, height, width]的uint8张量。