blocks
最后更新
luxonis_train.nodes.backbones.ghostfacenet.blocksGhostFaceNet 的 Ghost 模块,它通过廉价操作而不是完全卷积来生成部分特征图。类
AttentionGhostModuleV2
1x5 和一个 5x1 深度可分离卷积,一个步幅为 2 的 2x2 平均池化,以及一个 sigmoid。分支的卷积具有批量归一化,没有激活。GhostNetV2 将此分支称为 DFC 注意力。最近邻插值将门控调整为输出大小,模块将 Ghost 特征乘以门控。示例
Pycon
1>>> import torch
2>>> module = AttentionGhostModuleV2(4, 8)
3>>> module(torch.zeros(1, 4, 7, 7)).shape
4torch.Size([1, 8, 7, 7])方法
init
Python
1def __init__(in_channels: int, out_channels: int, kernel_size: int = 1, ratio: int = 2, dw_size: int = 3, stride: int = 1, use_prelu: bool = True):in_channels(int): 输入通道数。out_channels(int): 输出通道数。kernel_size(int): 主核和注意力分支的第一个核的大小。填充为kernel_size // 2。ratio(int):out_channels与主卷积通道数之比。dw_size(int): 廉价深度可分离核的大小。填充为dw_size // 2。stride(int): 主卷积和注意力分支的第一个卷积的步幅。use_prelu(bool): 主卷积和廉价卷积是否以torch.nn.PReLU结束。注意力分支在 sigmoid 之前从不激活。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 形状为[B, out_channels, H', W']的输出。主卷积的步幅设置H'和W'。每个值都是 Ghost 特征乘以0到1之间的门控。
属性
short_conv
GhostBottleneckLayer
width_multiplier。然后将每个计数四舍五入到最接近的 4 的倍数,最小值为 4。如果计数四舍五入后低于其值的 90%,则增加 4。示例
Pycon
1>>> import torch
2>>> layer = GhostBottleneckLayer(
3... width_multiplier=1,
4... input_channel=16,
5... kernel_sizes=[3, 3],
6... expand_sizes=[48, 72],
7... output_channels=[24, 24],
8... se_ratios=[0.0, 0.25],
9... strides=[2, 1],
10... mode="attention",
11... )
12>>> len(layer), layer.output_channel
13(2, 24)
14>>> layer(torch.zeros(1, 16, 8, 8)).shape
15torch.Size([1, 24, 4, 4])方法
init
Python
1def __init__(width_multiplier: int, input_channel: int, kernel_sizes: list[int], expand_sizes: list[int], output_channels: list[int], se_ratios: list[float], strides: list[int], mode: Literal['original', 'attention']):width_multiplier(int):expand_sizes和output_channels的比例。input_channel(int): 第一个块的输入通道数。该层不缩放它。kernel_sizes(list[int]): 每个块的kernel_size。expand_sizes(list[int]): 每个块的hidden_channels,在width_multiplier之前。output_channels(list[int]): 每个块的out_channels,在width_multiplier之前。se_ratios(list[float]): 每个块的se_ratio。strides(list[int]): 每个块的stride。mode(Literal['original', 'attention']): 所有块的mode。
ValueError: 当五个列表长度不同时。
属性
output_channel
input_channel。GhostBottleneckV2
- 一个带有
torch.nn.PReLU的 Ghost 模块将in_channels扩展到hidden_channels。 - 当
stride大于1时,一个带有批量归一化且无激活的深度可分离卷积会减小空间大小。 - 当
se_ratio大于0时,一个带有硬 sigmoid 和torch.nn.PReLU的 SqueezeExciteBlock 会缩放通道。 - 一个没有激活的 OriginalGhostModuleV2 投影到
out_channels。
in_channels 等于 out_channels 且 stride 为 1 时,快捷连接是 torch.nn.Identity。否则,它是一个带有 kernel_size 和 stride 的深度可分离卷积,然后是一个 1x1 卷积,每个都带有批量归一化。该块将快捷连接添加到主路径。示例
Pycon
1>>> import torch
2>>> block = GhostBottleneckV2(
3... 8, 16, 8, stride=2, se_ratio=0.25, mode="attention"
4... )
5>>> block(torch.zeros(1, 8, 8, 8)).shape
6torch.Size([1, 8, 4, 4])
7>>> GhostBottleneckV2(8, 16, 8, mode="original").shortcut
8Identity()方法
init
Python
1def __init__(in_channels: int, hidden_channels: int, out_channels: int, kernel_size: int = 3, stride: int = 1, se_ratio: float = 0.0, *, mode: Literal['original', 'attention']):in_channels(int): 输入通道数。hidden_channels(int): 扩展后的通道数。out_channels(int): 输出通道数。kernel_size(int): 主路径和快捷连接的深度可分离核的大小。填充为(kernel_size - 1) // 2。stride(int): 两个深度可分离卷积的步幅。se_ratio(float): 挤压-激励通道与hidden_channels的比率。该块将结果四舍五入到 4 的倍数。0或更小表示不添加 SqueezeExciteBlock。mode(Literal['original', 'attention']): 扩展的 Ghost 模块。"original"选择 OriginalGhostModuleV2,"attention"选择 AttentionGhostModuleV2。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 形状为[B, out_channels, H', W']的输出。对于奇数kernel_size,H'和W'是H和W除以stride并向上取整。
属性
bn_dw
conv_dw
ghost1
ghost2
se
shortcut
OriginalGhostModuleV2
ceil(out_channels / ratio) 个通道。深度可分离卷积(廉价操作)从每个通道生成 ratio - 1 个通道。该模块将两个结果连接起来,并保留前 out_channels 个通道。两个卷积都带有批量归一化。示例
Pycon
1>>> import torch
2>>> module = OriginalGhostModuleV2(4, 7, ratio=3)
3>>> module.primary_conv.out_channels
43
5>>> module.cheap_operation.out_channels
66
7>>> module(torch.zeros(1, 4, 8, 8)).shape
8torch.Size([1, 7, 8, 8])方法
init
Python
1def __init__(in_channels: int, out_channels: int, kernel_size: int = 1, ratio: int = 2, dw_size: int = 3, stride: int = 1, use_prelu: bool = True):in_channels(int): 输入通道数。out_channels(int): 输出通道数。kernel_size(int): 主核的大小。填充为kernel_size // 2。ratio(int):out_channels与主卷积通道数之比。dw_size(int): 深度可分离核的大小。填充为dw_size // 2。stride(int): 主卷积的步幅。use_prelu(bool): 两个卷积是否都以torch.nn.PReLU结束。当为False时,它们没有激活。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 形状为[B, out_channels, H', W']的输出。主卷积的步幅设置H'和W'。