unet
最后更新
luxonis_train.nodes.blocks.unetU-Net 的编码器和解码器块,带或不带跳跃连接。类
BaseDecoder
1 并反向遍历列表。每对相邻条目提供一个解码器步骤。该步骤将大小加倍,并将第一个条目的通道映射到第二个条目的通道。对于 [1, 2],步骤将 2 * base_width 通道映射到 base_width,然后将 base_width 映射到 base_width。然后 3x3 卷积将 base_width 通道映射到 out_channels。每个步骤都使用 align_corners=True 的 "simple_upsample",并在其 UpBlock 中使用批归一化。该类未定义 forward。SimpleDecoder 和 UNetDecoder 添加了它。方法
init
Python
1def __init__(base_width: int, out_channels: int, encoder_width_multipliers: list[float], n_convolutions: int, block: type[SimpleDecoderBlock | UNetDecoderBlock]):base_width(int): 编码器的base_hidden_channels。out_channels(int): 输出通道数。encoder_width_multipliers(list[float]): 编码器的width_multipliers。n_convolutions(int): 每个步骤堆栈中3x3ConvBlock 层的数量。block(type[SimpleDecoderBlock | UNetDecoderBlock]): 解码器步骤的类。
属性
blocks
final_conv
3x3 输出卷积。BaseDecoderBlock
forward。SimpleDecoderBlock 和 UNetDecoderBlock 添加了它。方法
init
Python
1def __init__(in_channels: int, out_channels: int, conv_in_channels: int, kernel_size: int, use_norm: bool, align_corners: bool, upsample_mode: Literal['simple_upsample', 'conv_upsample', 'conv_transpose'], n_repeats: int):in_channels(int): 输入通道数。out_channels(int): 上采样块和堆栈的输出通道数。conv_in_channels(int): 堆栈的输入通道数。kernel_size(int): UpBlock 的核大小。只有"conv_transpose"使用它。use_norm(bool): UpBlock 的3x3ConvBlock 是否有批归一化。堆栈始终有批归一化。align_corners(bool): UpBlock 中插值的align_corners选项。upsample_mode(Literal['simple_upsample', 'conv_upsample', 'conv_transpose']): UpBlock 的上采样方法。因子为2。n_repeats(int): 堆栈中3x3ConvBlock 层的数量。
属性
conv
up
EncoderBlock
2x2 最大池化和一个 ConvStack。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import EncoderBlock
3>>> x = torch.zeros(1, 3, 16, 16)
4>>> EncoderBlock(3, 8, n_repeats=2)(x).shape
5torch.Size([1, 8, 8, 8])
6>>> EncoderBlock(3, 8, n_repeats=2, max_pool=False)(x).shape
7torch.Size([1, 8, 16, 16])方法
init
Python
1def __init__(in_channels: int, out_channels: int, n_repeats: int, *, max_pool: bool = True):in_channels(int): 输入通道数。out_channels(int): 输出通道数。n_repeats(int): 堆栈中3x3ConvBlock 层的数量。max_pool(bool): 在堆栈之前,是否通过步长为2的2x2最大池化将大小减半。否则,该步骤保持大小不变。
SimpleDecoder
base_hidden_channels 等于 base_width 且乘数相同。当编码器的输入大小可被 2n 整除时,其中 n 是乘数数量,它会恢复编码器的输入大小。SimpleEncoder 的示例展示了这对。方法
init
Python
1def __init__(base_width: int, out_channels: int, encoder_width_multipliers: list[float], n_convolutions: int = 2):base_width(int): 编码器的base_hidden_channels。out_channels(int): 输出通道数。encoder_width_multipliers(list[float]): 编码器的width_multipliers。n_convolutions(int): 每个步骤堆栈中3x3ConvBlock 层的数量。block: 解码器步骤的类。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): SimpleEncoder 的输出,形状为[B, C, h, w],其中 C = ⌊m⋅wb⌋。这里 m 是最后一个乘数,wb 是base_width。
Tensor: 形状为[B, out_channels, h * 2^n, w * 2^n]的输出,其中n是乘数数量。
SimpleDecoderBlock
2 倍,并在结果上运行 ConvStack。方法
init
Python
1def __init__(in_channels: int, out_channels: int, kernel_size: int, use_norm: bool, align_corners: bool, upsample_mode: Literal['simple_upsample', 'conv_upsample', 'conv_transpose'], n_repeats: int):out_channels 通道映射到 out_channels。参数in_channels(int): 输入通道数。out_channels(int): 输出通道数。kernel_size(int): UpBlock 的核大小。只有"conv_transpose"使用它。use_norm(bool): UpBlock 的3x3ConvBlock 是否有批归一化。align_corners(bool): UpBlock 中插值的align_corners选项。upsample_mode(Literal['simple_upsample', 'conv_upsample', 'conv_transpose']): UpBlock 的上采样方法。n_repeats(int): 堆栈中3x3ConvBlock 层的数量。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 对于插值模式,输出形状为[B, out_channels, 2H, 2W]。UpBlock 为"conv_transpose"提供大小。
SimpleEncoder
width_multipliers 的每个条目设置一个步骤,并为最后一个条目再设置一个步骤。条目 m 的步骤具有 ⌊m⋅c⌋ 个输出通道,其中 c 是 base_hidden_channels。第一步保持大小不变。后面的每个步骤都将其减半并向下取整。对于 n 个条目,输出比输入小 2n 倍。RecSubNet 将编码器与 SimpleDecoder 配对。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import (
3... SimpleDecoder,
4... SimpleEncoder,
5... )
6>>> encoder = SimpleEncoder(3, 8, [1, 2])
7>>> len(encoder)
83
9>>> features = encoder(torch.zeros(1, 3, 16, 16))
10>>> features.shape
11torch.Size([1, 16, 4, 4])
12>>> SimpleDecoder(8, 3, [1, 2])(features).shape
13torch.Size([1, 3, 16, 16])方法
init
Python
1def __init__(in_channels: int, base_hidden_channels: int, width_multipliers: list[float], n_convolutions: int = 2):in_channels(int): 输入通道数。base_hidden_channels(int): 基础宽度。每个步骤都将其乘以width_multipliers的对应条目。width_multipliers(list[float]): 每个步骤的宽度因子。空列表将导致构造函数引发IndexError。n_convolutions(int): 每个步骤中3x3ConvBlock 层的数量。
UNetDecoder
base_hidden_channels 等于 base_width 且乘数相同。forward 从列表中移除最后一个图,如示例所示。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import UNetDecoder, UNetEncoder
3>>> encoder = UNetEncoder(3, 8, [1, 2])
4>>> decoder = UNetDecoder(8, 2, [1, 2])
5>>> features = encoder(torch.zeros(1, 3, 16, 16))
6>>> decoder(features).shape
7torch.Size([1, 2, 16, 16])
8>>> len(features)
92方法
init
Python
1def __init__(base_width: int, out_channels: int, encoder_width_multipliers: list[float], n_convolutions: int = 2):base_width(int): 编码器的base_hidden_channels。out_channels(int): 输出通道数。encoder_width_multipliers(list[float]): 编码器的width_multipliers。n_convolutions(int): 每个步骤堆栈中3x3ConvBlock 层的数量。block: 解码器步骤的类。
forward
Python
1def forward(inputs: list[Tensor]) -> Tensor:inputs 中弹出最小的图并从它开始。然后每个步骤将下一个剩余的图作为其跳跃连接,从小到大。警告: 该方法会移除 inputs 的最后一个元素。参数inputs(list[Tensor]): UNetEncoder 的特征图,从大到小。列表中的图数量必须比解码器步骤多一个。否则zip会引发ValueError。
Tensor: 形状为[B, out_channels, H, W]的输出,其中H和W是第一个图的大小。
UNetDecoderBlock
2 倍,并沿通道轴连接跳跃特征图。然后它在结果上运行 ConvStack。跳跃特征图必须具有 in_channels 通道和上采样输入的大小。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import UNetDecoderBlock
3>>> block = UNetDecoderBlock(
4... in_channels=8,
5... out_channels=4,
6... kernel_size=3,
7... use_norm=True,
8... align_corners=True,
9... upsample_mode="simple_upsample",
10... n_repeats=1,
11... )
12>>> x, skip_x = torch.zeros(1, 8, 4, 4), torch.zeros(1, 8, 8, 8)
13>>> block(x, skip_x).shape
14torch.Size([1, 4, 8, 8])方法
init
Python
1def __init__(in_channels: int, out_channels: int, kernel_size: int, use_norm: bool, align_corners: bool, upsample_mode: Literal['simple_upsample', 'conv_upsample', 'conv_transpose'], n_repeats: int):in_channels + out_channels 通道映射到 out_channels。参数in_channels(int): 输入和跳跃特征图的通道数。out_channels(int): 输出通道数。kernel_size(int): UpBlock 的核大小。只有"conv_transpose"使用它。use_norm(bool): UpBlock 的3x3ConvBlock 是否有批归一化。align_corners(bool): UpBlock 中插值的align_corners选项。upsample_mode(Literal['simple_upsample', 'conv_upsample', 'conv_transpose']): UpBlock 的上采样方法。n_repeats(int): 堆栈中3x3ConvBlock 层的数量。
forward
Python
1def forward(x: Tensor, skip_x: Tensor) -> Tensor:x,连接 skip_x,并运行堆栈。参数x(Tensor): 形状为[B, in_channels, H, W]的输入。skip_x(Tensor): 形状为[B, in_channels, 2H, 2W]的编码器特征图。
Tensor: 形状为[B, out_channels, 2H, 2W]的输出。
UNetEncoder
示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import UNetEncoder
3>>> encoder = UNetEncoder(3, 8, [1, 2])
4>>> features = encoder(torch.zeros(1, 3, 16, 16))
5>>> [tuple(feature.shape) for feature in features]
6[(1, 8, 16, 16), (1, 16, 8, 8), (1, 16, 4, 4)]方法
forward
Python
1def forward(x: Tensor) -> list[Tensor]:x(Tensor): 形状为[B, in_channels, H, W]的输入。
list[Tensor]: 每个步骤的一个特征图,从大到小。步骤i的图的高度和宽度是x的 2i 倍。
UpBlock
stride 上采样,然后是一个 3x3 ConvBlock。upsample_mode 选择上采样方式:"conv_transpose": 一个torch.nn.ConvTranspose2d,从in_channels到out_channels,带有kernel_size、stride且无填充。输出大小为 (H − 1)⋅s + k,其中步长为 s,核大小为 k。"simple_upsample": 一个torch.nn.Upsample,通过stride上采样。然后 ConvBlock 将in_channels映射到out_channels。"conv_upsample": 相同的torch.nn.Upsample,然后是一个1x1torch.nn.Conv2d,从in_channels到out_channels。
out_channels 通道。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import UpBlock
3>>> x = torch.zeros(1, 8, 4, 4)
4>>> options = {
5... "kernel_size": 2,
6... "use_norm": True,
7... "align_corners": False,
8... }
9>>> UpBlock(8, 4, "conv_transpose", **options)(x).shape
10torch.Size([1, 4, 8, 8])
11>>> UpBlock(8, 4, "conv_upsample", **options)(x).shape
12torch.Size([1, 4, 8, 8])方法
init
Python
1def __init__(in_channels: int, out_channels: int, upsample_mode: Literal['simple_upsample', 'conv_upsample', 'conv_transpose'], kernel_size: int, use_norm: bool, align_corners: bool, stride: int = 2, activation: nn.Module | bool | None = True, interpolation_mode: Literal['nearest', 'linear', 'bilinear', 'bicubic', 'trilinear'] = 'bilinear'):in_channels(int): 输入通道数。out_channels(int): 输出通道数。upsample_mode(Literal['simple_upsample', 'conv_upsample', 'conv_transpose']): 上采样方法。类描述列出了每种方法的层。kernel_size(int): 转置卷积的核大小。其他方法忽略它。use_norm(bool): ConvBlock 是否有批归一化。align_corners(bool):torch.nn.Upsample的align_corners选项。"conv_transpose"忽略它。stride(int): 上采样因子。activation(nn.Module | bool | None): ConvBlock 的激活函数。True选择torch.nn.ReLU。False或None选择torch.nn.Identity。interpolation_mode(Literal['nearest', 'linear', 'bilinear', 'bicubic', 'trilinear']):torch.nn.Upsample的模式。"conv_transpose"忽略它。警告: 只有"bilinear"和"bicubic"适用于 4D 输入。"nearest"拒绝任何align_corners值。"linear"需要 3D 输入,"trilinear"需要 5D 输入。