blocks
最后更新
luxonis_train.nodes.backbones.pplcnet_v3.blocksPPLCNetV3 主干网络的块。类
AffineActivation
Hardswish 后跟一个可学习的仿射映射。输出是 scale * hardswish(x) + bias。一个 AffineBlock 包含 scale 和 bias。它们从 1.0 和 0.0 开始,因此该模块最初是一个普通的 torch.nn.Hardswish。示例
Pycon
1>>> import torch
2>>> act = AffineActivation()
3>>> act(torch.tensor([-4.0, 0.0, 4.0])).tolist()
4[0.0, 0.0, 4.0]方法
init
Python
1def __init__(self):forward
Python
1def forward(x: Tensor) -> Tensor:Hardswish,然后是仿射映射。参数x(Tensor): 任意形状的张量。
Tensor:scale * hardswish(x) + bias,与x形状相同。
属性
activation
affine
AffineBlock
scale * x + bias,带标量参数。scale 和 bias 是形状为 [1] 的 torch.nn.Parameter 对象。相同的两个值适用于输入的所有元素。示例
Pycon
1>>> import torch
2>>> block = AffineBlock(scale_value=2.0, bias_value=1.0)
3>>> block(torch.tensor([1.0, 2.0])).tolist()
4[3.0, 5.0]方法
init
Python
1def __init__(scale_value: float = 1.0, bias_value: float = 0.0):scale_value(float):scale的初始值。提供float类型。int类型可以通过类型检查,但它会生成一个整数张量。然后构造函数会引发RuntimeError。bias_value(float):bias的初始值。规则相同。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 任意形状的张量。
Tensor:scale * x + bias,与x形状相同。
属性
bias
scale
LCNetV3Block
- 一个 k×k 深度可分离 GeneralReparameterizableBlock。它有
n_branches个密集分支,一个 1×1 比例分支,当stride为1时还有一个恒等分支。 - 一个可选的 SqueezeExciteBlock,带有
in_channels // 4个隐藏通道,一个ReLU和一个硬 sigmoid 门。 - 一个 1×1 点式 GeneralReparameterizableBlock。它有
n_branches个密集分支,没有比例分支,当in_channels等于out_channels时还有一个恒等分支。
stride 为 2 时,深度卷积没有激活。示例
Pycon
1>>> import torch
2>>> block = LCNetV3Block(8, 16, kernel_size=3, stride=2, use_se=True)
3>>> block(torch.zeros(1, 8, 32, 32)).shape
4torch.Size([1, 16, 16, 16])方法
init
Python
1def __init__(in_channels: int, out_channels: int, kernel_size: int, stride: int, use_se: bool = False, n_branches: int = 4):in_channels(int): 输入通道数。深度卷积保持此数量。out_channels(int): 点式卷积的输出通道数。kernel_size(int): 深度卷积的核大小。填充为(kernel_size - 1) // 2。使用奇数值。stride(int): 深度卷积的步幅。2还会移除其后的激活。use_se(bool): 是否在两个卷积之间添加 SqueezeExciteBlock。n_branches(int): 每个卷积的密集分支数。
forward
Python
1def forward(x: Tensor) -> Tensor:x(Tensor): 形状为[B, in_channels, H, W]的输入。
Tensor: 形状为[B, out_channels, ceil(H / stride), ceil(W / stride)]的输出,对于奇数kernel_size。
属性
dw_conv
pw_conv
se
LCNetV3Layer
out_channels 的每个值按 scale 进行缩放。每个块都接收其前一个块的输出通道。torch.nn.Sequential 的 forward 按顺序运行这些块。示例
Pycon
1>>> import torch
2>>> layer = LCNetV3Layer(
3... 16, [64, 64], [3, 3], [2, 1], [False, False], scale=0.95
4... )
5>>> layer.out_channels, len(layer)
6(64, 2)
7>>> layer(torch.zeros(1, 16, 8, 8)).shape
8torch.Size([1, 64, 4, 4])方法
init
Python
1def __init__(in_channels: int, out_channels: list[int], kernel_sizes: list[int], strides: list[int], use_se: list[bool], n_branches: int = 4, scale: float = 1.0):in_channels(int): 第一个块的输入通道数。out_channels(list[int]): 每个块的输出通道数,在 scale_up 缩放它们之前。kernel_sizes(list[int]): 每个块的深度核大小。strides(list[int]): 每个块的深度步幅。use_se(list[bool]): 每个块是否具有 SqueezeExciteBlock。n_branches(int): 每个块的每个卷积的密集分支数。scale(float): scale_up 应用于out_channels的宽度乘数。
ValueError: 当四个列表长度不一致时。
属性
out_channels
scale_up(out_channels[-1], scale)。QuantizedAffineBlock
aimet_torch 时存在。装饰器 QuantizationMixin.implements 为 AffineBlock 注册它。该块有一个输入量化器和一个输出量化器。方法
forward
Python
1def forward(x: Tensor) -> Tensor:x。它使用量化参数运行 AffineBlock.forward。然后它使用输出量化器量化结果。它会跳过为 None 的量化器。参数x(Tensor): 任意形状的张量。
Tensor:scale * x + bias,与x形状相同。
属性
input_quantizers
output_quantizers
函数
scale_up
Python
1def scale_up(v: float, scale: float, divisor: int = 16, min_value: int | None = None) -> int:divisor 的倍数。该函数将 v 乘以 scale。它将乘积四舍五入到最接近 divisor 的倍数,介于两个倍数之间的乘积向上取整。结果至少为 min_value。当结果低于乘积的 90% 时,函数会添加一个 divisor。示例
Pycon
1>>> scale_up(512, 0.95)
248010 四舍五入为 8,低于 10 的 90%。因此函数添加 8:Pycon
1>>> scale_up(10, 1.0, divisor=8)
216v(float): 要缩放的通道计数。scale(float): 乘数。divisor(int): 要四舍五入到的倍数。min_value(int | None): 最小结果。None选择divisor。
int: 缩放并四舍五入后的通道计数。