resnet
最后更新
luxonis_train.nodes.blocks.resnetResNet 的残差块和瓶颈块。类
GenericResidualBlock
stride 为 1 且 in_channels 等于 expansion * hidden_channels 时,快捷连接是 torch.nn.Identity。否则,它是一个 1x1 ConvBlock,带有 stride、批归一化且无激活。此投影将输入映射到 expansion * hidden_channels 通道。ResNetBlock 和 ResNetBottleneck 基于此类构建。方法
init
Python
1def __init__(in_channels: int, hidden_channels: int, stride: int, expansion: int, final_relu: bool, block: nn.Module):in_channels(int): 输入通道数。hidden_channels(int): 基本宽度。输出有expansion * hidden_channels个通道。stride(int): 快捷连接投影的步幅。分支必须以相同的因子减小尺寸。expansion(int): 从hidden_channels到输出通道数的因子。final_relu(bool): 求和后是否跟随 ReLU。block(nn.Module): 残差分支。其输出必须具有快捷连接输出的形状。
forward
Python
1def forward(x: Tensor) -> Tensor:block 的输出上就地运行。参数x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 求和block(x) + shortcut(x),当构造函数获得final_relu=True时,在 ReLU 之后。形状为[B, expansion * hidden_channels, H', W'],其中stride设置H'和W'。
属性
block
final_relu
torch.nn.ReLU 或 torch.nn.Identity。shortcut
1x1 投影。ResNetBlock
3x3 卷积的基本 ResNet 块。残差分支是一个带有 stride 的 3x3 卷积、一个批归一化、一个 ReLU、一个 3x3 卷积、一个批归一化和 DropPath。卷积没有偏置。快捷连接遵循 GenericResidualBlock 的规则。示例
Pycon
1>>> import torch
2>>> from torch import nn
3>>> from luxonis_train.nodes.blocks import ResNetBlock
4>>> isinstance(ResNetBlock(8, 8).shortcut, nn.Identity)
5True
6>>> block = ResNetBlock(8, 16, stride=2)
7>>> block(torch.zeros(1, 8, 8, 8)).shape
8torch.Size([1, 16, 4, 4])方法
init
Python
1def __init__(in_channels: int, hidden_channels: int, stride: int = 1, expansion: int = 1, final_relu: bool = True, droppath_prob: float = 0.0):in_channels(int): 输入通道数。hidden_channels(int): 输出通道数。stride(int): 第一个卷积和快捷连接的步幅。expansion(int): 快捷连接通道的因子。分支总是给出hidden_channels个通道,因此只有1有效。当值大于1时,forward会引发RuntimeError。final_relu(bool): 求和后是否跟随 ReLU。droppath_prob(float): 分支末尾 DropPath 的丢弃概率。
ResNetBottleneck
1x1 卷积将输入减少到 hidden_channels。接着是一个带有 stride 的 3x3 卷积。最后一个 1x1 卷积将结果扩展到 expansion * hidden_channels 个通道。每个卷积都有批归一化且无偏置。前两个批归一化后跟随 ReLU,DropPath 结束分支。快捷连接遵循 GenericResidualBlock 的规则。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes.blocks import ResNetBottleneck
3>>> block = ResNetBottleneck(16, 8, stride=2)
4>>> block(torch.zeros(1, 16, 8, 8)).shape
5torch.Size([1, 32, 4, 4])方法
init
Python
1def __init__(in_channels: int, hidden_channels: int, stride: int = 1, expansion: int = 4, final_relu: bool = True, droppath_prob: float = 0.0):in_channels(int): 输入通道数。hidden_channels(int):1x1缩减和3x3卷积的通道数。stride(int):3x3卷积和快捷连接的步幅。expansion(int): 输出有expansion * hidden_channels个通道。final_relu(bool): 求和后是否跟随 ReLU。droppath_prob(float): 分支末尾 DropPath 的丢弃概率。