# mobilenetv2

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

The MobileNetV2 backbone from `torchvision`.

## Classes

| Name | Description |
| --- | --- |
| [`MobileNetV2`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/mobilenetv2.md#mobilenetv2-Classes-MobileNetV2) | MobileNetV2 backbone that returns intermediate feature maps. |

### class `MobileNetV2`

```python
class MobileNetV2(BaseNode)
```

MobileNetV2 backbone that returns intermediate feature maps.

MobileNetV2 stacks inverted residual blocks with linear bottlenecks and depthwise convolutions. The node wraps the `torchvision`
model with the width multiplier `1.0`. It runs the 19 modules of `features`. It returns the output of each module that
`out_indices` selects.

 * `Inputs:`: * `inputs` (`Tensor`): [B, 3, H, W]
 * `Outputs:`: * `features` (`list[Tensor]`): one per `out_indices`; by default strides 4, 8, 16, 32 with 24, 32, 96, 1280
   channels

> **References**
> * Source: Wraps [torchvision.models.mobilenet_v2](https://docs.pytorch.org/vision/stable/models/mobilenetv2.html) (BSD-3-Clause). Paper: [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/abs/1801.04381).
 * License: Apache-2.0 (this project)

> **Notes**
> The input must have 3 channels. The node keeps the unused `classifier` of the `torchvision` model.

 * `Variants:`: None. Configure the node through `params`.

> **Example**
> A node entry in the `model.nodes` section of a config:

```yaml
- name: MobileNetV2
```

 * `Compatible with:`: * Attach index: `-1`, the last output of the input node

Attributes

| Name | Description |
| --- | --- |
| `backbone` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/mobilenetv2.md#mobilenetv2-Classes-MobileNetV2-__init__) | Build the `torchvision` model and store `out_indices`. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/mobilenetv2.md#mobilenetv2-Classes-MobileNetV2-forward) | Run the `features` modules in order. |

#### method `__init__`

```python
def __init__(
    out_indices: list[int] | None = None,
    weights: Literal['download', 'none'] | None = None,
    **kwargs,
)
```

Build the `torchvision` model and store `out_indices`.

The modules of `features` have these output channels and strides:

 * Module `0`, the stem convolution: 32 channels, stride 2.
 * Module `1`: 16 channels, stride 2.
 * Modules `2` and `3`: 24 channels, stride 4.
 * Modules `4` to `6`: 32 channels, stride 8.
 * Modules `7` to `10`: 64 channels, stride 16.
 * Modules `11` to `13`: 96 channels, stride 16.
 * Modules `14` to `16`: 160 channels, stride 32.
 * Module `17`: 320 channels, stride 32.
 * Module `18`, the final `1x1` convolution: 1280 channels, stride 32.

The modules `1` to `17` are the inverted residual blocks.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `out_indices` | `list[int] | None` | Indices of the `features` modules that [forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/mobilenetv2.md#mobilenetv2-Classes-MobileNetV2-forward) returns, from `0` to `18`. An index outside that range adds no output. `None` or an empty list selects `[3, 6, 13, 18]`. | `None` |
| `weights` | `Literal['download', 'none'] | None` | The value `"download"` loads the `DEFAULT` `torchvision` weights, `IMAGENET1K_V2`. Any other value keeps the random initialization. The value does not reach [BaseNode](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md#base_node-Classes-BaseNode), so a checkpoint URL or `"yolo"` has no effect. | `None` |
| `**kwargs` |  | Keyword arguments forwarded to [BaseNode](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md#base_node-Classes-BaseNode). |  |

#### method `forward`

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

Run the `features` modules in order.

> **Example**
> ```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)]
```

The outputs keep the module order:

```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)]
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputs` | `Tensor` | Image batch of shape `[B, 3, H, W]`. | required |

Returns

| Type | Description |
| --- | --- |
| `list[Tensor]` | The output of each module whose index is in `out_indices`, in module order. The default indices give 24, 32, 96, and 1280 channels at the strides 4, 8, 16, and 32. |
