mobilenetv2
最后更新
luxonis_train.nodes.backbones.mobilenetv2来自 torchvision 的 MobileNetV2 骨干网络。类
MobileNetV2
1.0 封装了 torchvision 模型。它运行 features 的 19 个模块。它返回 out_indices 选择的每个模块的输出。输入::inputs(Tensor): [B, 3, H, W]
输出::features(list[Tensor]): 每个out_indices一个;默认步长为 4、8、16、32,通道数为 24、32、96、1280
参考
- 来源:封装了 torchvision.models.mobilenet_v2 (BSD-3-Clause)。论文:MobileNetV2: Inverted Residuals and Linear Bottlenecks。
- 许可证:Apache-2.0(本项目)
注意事项
输入必须有 3 个通道。该节点保留了
torchvision 模型中未使用的 classifier。变体:: 无。通过params配置节点。
示例
配置中
model.nodes 部分的节点条目:Yaml
1- name: MobileNetV2兼容::- 附加索引:
-1,输入节点的最后一个输出
- 附加索引:
方法
init
Python
1def __init__(out_indices: list[int] | None = None, weights: Literal['download', 'none'] | None = None, **kwargs):torchvision 模型并存储 out_indices。features 的模块具有以下输出通道和步长:- 模块
0,主干卷积:32 个通道,步长 2。 - 模块
1:16 个通道,步长 2。 - 模块
2和3:24 个通道,步长 4。 - 模块
4到6:32 个通道,步长 8。 - 模块
7到10:64 个通道,步长 16。 - 模块
11到13:96 个通道,步长 16。 - 模块
14到16:160 个通道,步长 32。 - 模块
17:320 个通道,步长 32。 - 模块
18,最终的1x1卷积:1280 个通道,步长 32。
1 到 17 是倒置残差块。参数out_indices(list[int] | None): forward 返回的features模块的索引,范围从0到18。超出此范围的索引不会添加输出。None或空列表选择[3, 6, 13, 18]。weights(Literal['download', 'none'] | None): 值"download"加载DEFAULTtorchvision权重IMAGENET1K_V2。任何其他值都保持随机初始化。该值不会到达 BaseNode,因此检查点 URL 或"yolo"无效。**kwargs: 转发到 BaseNode 的关键字参数。
forward
Python
1def forward(inputs: Tensor) -> list[Tensor]:features 模块。示例
Pycon
1>>> import torch
2>>> from luxonis_train.nodes import MobileNetV2
3>>> node = MobileNetV2()
4>>> [tuple(t.shape) for t in node(torch.zeros(1, 3, 64, 64))]
5[(1, 24, 16, 16), (1, 32, 8, 8), (1, 96, 4, 4), (1, 1280, 2, 2)]Pycon
1>>> node = MobileNetV2(out_indices=[18, 1])
2>>> [tuple(t.shape) for t in node(torch.zeros(1, 3, 64, 64))]
3[(1, 16, 32, 32), (1, 1280, 2, 2)]inputs(Tensor): 形状为[B, 3, H, W]的图像批次。
list[Tensor]: 其索引在out_indices中的每个模块的输出,按模块顺序。默认索引在步长 4、8、16 和 32 处分别给出 24、32、96 和 1280 个通道。