# mobilenetv2

Python API: `luxonis_train.nodes.backbones.mobilenetv2`

来自 `torchvision` 的 MobileNetV2 骨干网络。

## 类

### MobileNetV2

返回中间特征图的 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](https://docs.pytorch.org/vision/stable/models/mobilenetv2.html) (BSD-3-Clause)。论文：[MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/abs/1801.04381)。
 * 许可证：Apache-2.0（本项目）

> **注意事项**
> 输入必须有 3 个通道。该节点保留了 `torchvision` 模型中未使用的 `classifier`。

 * `变体:`: 无。通过 `params` 配置节点。

> **示例**
> 配置中 `model.nodes` 部分的节点条目：

```yaml
- name: MobileNetV2
```

 * `兼容:`: * 附加索引：`-1`，输入节点的最后一个输出

#### 方法

##### init

```python
def __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](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/mobilenetv2.md)
   返回的 `features` 模块的索引，范围从 `0` 到 `18`。超出此范围的索引不会添加输出。`None` 或空列表选择 `[3, 6, 13, 18]`。
 * `weights` (`Literal['download', 'none'] | None`): 值 `"download"` 加载 `DEFAULT` `torchvision` 权重
   `IMAGENET1K_V2`。任何其他值都保持随机初始化。该值不会到达
   [BaseNode](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md)，因此检查点
   URL 或 `"yolo"` 无效。
 * `**kwargs`: 转发到
   [BaseNode](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md)
   的关键字参数。

##### forward

```python
def forward(inputs: Tensor) -> list[Tensor]:
```

按顺序运行 `features` 模块。

> **示例**
> ```pycon
>>> import torch
>>> from luxonis_train.nodes import MobileNetV2
>>> node = MobileNetV2()
>>> [tuple(t.shape) for t in node(torch.zeros(1, 3, 64, 64))]
[(1, 24, 16, 16), (1, 32, 8, 8), (1, 96, 4, 4), (1, 1280, 2, 2)]
```

输出保持模块顺序：

```pycon
>>> node = MobileNetV2(out_indices=[18, 1])
>>> [tuple(t.shape) for t in node(torch.zeros(1, 3, 64, 64))]
[(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 个通道。

#### 属性

##### backbone
