ddrnet_segmentation_head
最后更新
luxonis_train.nodes.heads.ddrnet_segmentation_headDDRNet 的分割头。类
DDRNetSegmentationHead
输入::inputs(Tensor): [B, C, H ⁄ s, W ⁄ s]
输出::- 训练、评估:
segmentation(Tensor): [B, nclasses, H, W] logits
- 导出:
segmentation(Tensor): [B, H, W]int32类索引,或当n_classes == 1时为 [B, 1, H, W],值为0和1
- 训练、评估:
参考
- 来源: 改编自 Deci-AI/super-gradients (Apache-2.0)。
- 许可证: Apache-2.0
注意事项
H 和 W 是模型输入的高度和宽度。比例 s 是一个 2 的幂,头部根据输入大小和模型输入大小计算得出。DDRNet 主干网络给出 s = 8。头部应用批归一化和 ReLU,一个 3x3 卷积,批归一化和 ReLU,以及一个 1x1 卷积到 nclasses 通道。然后,一个上采样层将结果按 s 缩放。在导出模式下,头部将 logits 转换为类索引。警告: 当
inter_mode="pixel_shuffle" 时,1x1 卷积保留 inter_channels 通道。输出通道数是 inter_channels 除以 s2,而不是 nclasses。变体:: 无。通过params配置节点。
示例
配置中
model.nodes 部分的节点条目:Yaml
1- name: DDRNetSegmentationHead
2 inputs: [DDRNet]兼容::- 附加索引:
-1,输入节点的最后一个输出 - 所需标签:
segmentation - 使用者: SegmentationModel
- 损失:
- 指标:
- 可视化工具: SegmentationVisualizer
- 导出解析器:
SegmentationParser - 预训练权重: 可通过
weights: download获取
- 附加索引:
方法
init
Python
1def __init__(inter_channels: int = 64, inter_mode: Literal['nearest', 'linear', 'bilinear', 'bicubic', 'trilinear', 'area', 'pixel_shuffle'] = 'bilinear', **kwargs):ValueError。参数inter_channels(int): 3x3 卷积的输出通道数。对于"pixel_shuffle",它必须是 s2 的倍数。inter_mode(Literal['nearest', 'linear', 'bilinear', 'bicubic', 'trilinear', 'area', 'pixel_shuffle']): 上采样方法。"pixel_shuffle"使用torch.nn.PixelShuffle。其他值是torch.nn.Upsample的mode。对于"linear"或"trilinear",forward 会引发NotImplementedError,因为这些模式需要 3D 或 5D 输入。**kwargs: BaseNode 的关键字参数。它们必须包含original_in_shape、input_shapes或in_sizes,以及通过n_classes或dataset_metadata给出的类计数。
ValueError: 当inter_mode为"pixel_shuffle"且inter_channels不是 s2 的倍数时。
forward
Python
1def forward(inputs: Tensor) -> Tensor:示例
Pycon
1>>> import torch
2>>> from torch import Size
3>>> from luxonis_train.nodes import DDRNetSegmentationHead
4>>> head = DDRNetSegmentationHead(
5... n_classes=3,
6... input_shapes=[{"features": [Size([1, 8, 4, 4])]}],
7... original_in_shape=Size([3, 32, 32]),
8... )
9>>> head.eval()(torch.zeros(1, 8, 4, 4)).shape
10torch.Size([1, 3, 32, 32])
11>>> head.export = True
12>>> out = head(torch.zeros(1, 8, 4, 4))
13>>> out.shape, out.dtype
14(torch.Size([1, 32, 32]), torch.int32)inputs(Tensor): 形状为[B, C, H / s, W / s]的特征图。
Tensor: 在非导出模式下,形状为[B, n_classes, H, W]的 logits。在导出模式下,一个int32张量。它包含每个像素具有最高 logit 的类索引,形状为[B, H, W]。当n_classes == 1时,它包含 logit 大于0的位置的1,形状为[B, 1, H, W]。BaseNode.run 将结果放在"segmentation"键下。
get_custom_head_config
Python
1def get_custom_head_config(self) -> Params:"metadata" 字典中。基本实现返回一个空字典。返回Params: 附加的元数据键及其值。
get_weights_url
Python
1def get_weights_url(self) -> str:23-slim 主干网络使用 slim 检查点;256 通道的 23 主干网络使用完整检查点。引发NotImplementedError: 如果输入既不是 128 也不是 256 通道。
load_checkpoint
Python
1def load_checkpoint(path: str | None = None, strict: bool = False):path 作为 ckpt 传递给 BaseNode.load_checkpoint。重写将第一个参数重命名并将 strict 的默认值设置为 False。警告: 重写没有 ckpt 参数。构造后,节点会为 weights URL 调用 load_checkpoint(ckpt=...)。因此,诸如 DDRNetSegmentationHead(weights="https://...") 之类的调用会引发 TypeError。对于 weights="download",节点在没有参数的情况下调用该方法,因此该值不会以这种方式失败。参数path(str | None):.ckpt文件的本地路径或 URL。LuxonisLightningModule 也提供状态字典,基本方法直接加载它。None或""从 get_weights_url 获取 URL。strict(bool): 检查点的键是否必须与头部的键完全匹配。
属性
bn1
bn2
conv1
conv2
in_channels
RuntimeError: 当 in_sizes 无法找到输入大小时。ValueError: 当 attach_index 不适合大小时。
in_height
RuntimeError: 当 in_sizes 无法找到输入大小时。ValueError: 当 attach_index 不适合大小时。
in_width
RuntimeError: 当 in_sizes 无法找到输入大小时。ValueError: 当 attach_index 不适合大小时。