blocks
最后更新
luxonis_train.nodes.backbones.ddrnet.blocksDDRNet 骨干网络的块。BasicDDRBackbone 包含主干和 layer1 到 layer4 阶段。 DAPPM 以多个尺度池化最深层的特征。 make_layer 将残差块堆叠成一个阶段。参考
类
BasicDDRBackbone
3x3 ConvBlock 层。每个层都有步长 2、偏置、批归一化和 ReLU。 make_layer 从 block 构建阶段。此列表给出了每个部分的输出通道和输出高度,对于高度为 H 的输入。宽度遵循相同的比例。stem:stem_channels通道,在H / 4。layer1:stem_channels通道,在H / 4。layer2:2 * stem_channels通道,在H / 8。layer3:一个torch.nn.ModuleList,包含max(layer3_repeats, 1)个阶段,在H / 16处有4 * stem_channels个通道。只有第一个阶段的步长为2。layer4:8 * stem_channels通道,在H / 32。
forward,因此调用模块会引发 NotImplementedError。 DDRNet 逐一调用主干和阶段,并将其高分辨率分支放在 layer3 和 layer4 旁边。方法
init
Python
1def __init__(block: type[nn.Module], stem_channels: int, layers: list[int], in_channels: int, layer3_repeats: int = 1):block(type[nn.Module]):每个阶段的块类,例如 ResNetBlock。 make_layer 列出了该类必须接受的参数。stem_channels(int):主干和layer1的输出通道数。layer2、layer3和layer4的通道数分别是其 2、4 和 8 倍。layers(list[int]):layer1、layer2、每个layer3阶段和layer4中的块数。骨干网络读取前四个条目。in_channels(int):输入通道数。layer3_repeats(int):layer3阶段的数量。小于1的值仍会构建一个阶段。默认为1。
get_backbone_output_number_of_channels
Python
1def get_backbone_output_number_of_channels(self) -> dict[str, int]:[1, in_channels, 320, 320] 的随机 CPU 张量,因此骨干网络必须在 CPU 上。它读取 layer2 之后、最后一个 layer3 阶段之后和 layer4 之后的通道维度。运行从 PyTorch 的全局随机生成器中抽取。在训练状态下,运行还会更新批归一化的运行统计信息。示例
Pycon
1>>> from luxonis_train.nodes.blocks import ResNetBlock
2>>> backbone = BasicDDRBackbone(
3... ResNetBlock,
4... stem_channels=8,
5... layers=[1, 1, 1, 1],
6... in_channels=3,
7... )
8>>> backbone.get_backbone_output_number_of_channels()
9{'layer2': 16, 'layer3': 32, 'layer4': 64}dict[str, int]:键为"layer2"、"layer3"和"layer4"的通道计数。
属性
layer1
layer2
layer3
layer4
stem
DAPPM
1x1 卷积将其压缩到 out_channels。批归一化、ReLU 和 1x1 卷积的快捷方式将输入映射到 out_channels。输出是压缩映射和快捷方式的总和。示例
Pycon
1>>> import torch
2>>> spp = DAPPM(
3... in_channels=8,
4... branch_channels=4,
5... out_channels=16,
6... kernel_sizes=[1, 5, 9, 17, 0],
7... strides=[1, 2, 4, 8, 0],
8... )
9>>> len(spp.branches)
104
11>>> spp(torch.zeros(1, 8, 16, 16)).shape
12torch.Size([1, 16, 16, 16])方法
init
Python
1def __init__(in_channels: int, branch_channels: int, out_channels: int, kernel_sizes: list[int], strides: list[int], interpolation_mode: str = 'bilinear'):in_channels(int):输入通道数。branch_channels(int):每个分支的输出通道数。out_channels(int):输出通道数。kernel_sizes(list[int]):每个分支的核大小。第一个条目配置 DAPPMBranch。其他每个条目配置一个 MergeDAPPMBranch。strides(list[int]):每个分支的步长,按kernel_sizes的顺序。 DAPPMBranch 解释了值0、1和更大的值。interpolation_mode(str):torch.nn.functional.interpolate在每个分支中用于调整大小的模式。默认为"bilinear"。
IndexError:当kernel_sizes或strides为空时。ValueError:当kernel_sizes和strides长度不同时。
forward
Python
1def forward(x: Tensor) -> Tensor:x 并融合结果。参数x(Tensor):形状为[B, in_channels, H, W]的输入。
Tensor:形状为[B, out_channels, H, W]的输出。
属性
branches
compression
shortcut
start_branch
DAPPMBranch
1x1 卷积到 branch_channels。然后 UpscaleOnline 将结果调整为输入的高度和宽度。stride 选择下采样:0:全局平均池化到1x1。1:无下采样。- 大于
1:使用核大小kernel_size、步长stride和填充stride的深度可分离卷积。
示例
Pycon
1>>> import torch
2>>> branch = DAPPMBranch(8, kernel_size=5, stride=2, branch_channels=4)
3>>> branch(torch.zeros(1, 8, 16, 16)).shape
4torch.Size([1, 4, 16, 16])方法
init
Python
1def __init__(in_channels: int, kernel_size: int, stride: int, branch_channels: int, interpolation_mode: str = 'bilinear'):in_channels(int):输入通道数。kernel_size(int):深度可分离卷积的核大小。仅当stride大于1时才使用。stride(int):选择下采样。0选择全局平均池化,1选择无下采样,更大的值选择具有此步长的深度可分离卷积。branch_channels(int):输出通道数。interpolation_mode(str):torch.nn.functional.interpolate用于调整到输入大小的模式。默认为"bilinear"。
forward
Python
1def forward(x: Tensor) -> Tensor:x 上运行分支并将输出调整为 [H, W]。参数x(Tensor):形状为[B, in_channels, H, W]的输入。
Tensor:形状为[B, branch_channels, H, W]的输出。
属性
down_scale
up_scale
MergeDAPPMBranch
3x3 卷积处理总和。卷积保持 branch_channels。示例
Pycon
1>>> import torch
2>>> branch = MergeDAPPMBranch(
3... 8, kernel_size=5, stride=2, branch_channels=4
4... )
5>>> previous = torch.zeros(1, 4, 16, 16)
6>>> branch(torch.zeros(1, 8, 16, 16), previous).shape
7torch.Size([1, 4, 16, 16])方法
init
Python
1def __init__(in_channels: int, kernel_size: int, stride: int, branch_channels: int, interpolation_mode: str = 'bilinear'):in_channels(int):输入通道数。kernel_size(int):深度可分离卷积的核大小。仅当stride大于1时才使用。stride(int):选择下采样。0选择全局平均池化,1选择无下采样,更大的值选择具有此步长的深度可分离卷积。branch_channels(int):输出通道数。前一个分支的输出必须具有相同的数量。interpolation_mode(str):torch.nn.functional.interpolate用于调整到输入大小的模式。默认为"bilinear"。
forward
Python
1def forward(x: Tensor, skip_x: Tensor) -> Tensor:x 上运行分支并合并前一个分支的输出。参数x(Tensor):DAPPM 块的输入,形状为[B, in_channels, H, W]。skip_x(Tensor):前一个分支的输出,形状为[B, branch_channels, H, W]。
Tensor:形状为[B, branch_channels, H, W]的输出。
属性
process
函数
make_layer
Python
1def make_layer(block: type[nn.Module], in_channels: int, channels: int, n_blocks: int, stride: int = 1, expansion: int = 1) -> nn.Sequential:n_blocks 个残差块堆叠成一个阶段。该函数为每个块调用 block 一次。每次调用都将输入通道和 channels 作为位置参数传递,并将 stride、final_relu 和 expansion 作为关键字参数传递。第一个块获取 in_channels 和 stride。其他块获取 channels * expansion 输入通道和步长 1。最后一个块获取 final_relu=False,其他每个块获取 final_relu=True。因此,阶段输出没有最终的 ReLU。小于 1 的 n_blocks 值仍会构建一个块。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import ResNetBlock
3>>> stage = make_layer(ResNetBlock, 8, 16, n_blocks=3, stride=2)
4>>> [type(block.final_relu).__name__ for block in stage]
5['ReLU', 'ReLU', 'Identity']
6>>> stage(torch.zeros(1, 8, 8, 8)).shape
7torch.Size([1, 16, 4, 4])block(type[nn.Module]):块类,例如 ResNetBlock 或 ResNetBottleneck。它必须接受上述参数并输出channels * expansion通道。in_channels(int):第一个块的输入通道数。channels(int):每个块的隐藏通道数。n_blocks(int):块数。stride(int):第一个块的步长。默认为1。expansion(int):每个块的扩展因子。默认为1。
nn.Sequential:按顺序排列的块。该阶段输出channels * expansion通道。