MobileNet V2

## [](#overview)Overview

The MobileNet model was proposed in [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/abs/1801.04381) by Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen.

The abstract from the paper is the following:

_In this paper we describe a new mobile architecture, MobileNetV2, that improves the state of the art performance of mobile models on multiple tasks and benchmarks as well as across a spectrum of different model sizes. We also describe efficient ways of applying these mobile models to object detection in a novel framework we call SSDLite. Additionally, we demonstrate how to build mobile semantic segmentation models through a reduced form of DeepLabv3 which we call Mobile DeepLabv3._

_The MobileNetV2 architecture is based on an inverted residual structure where the input and output of the residual block are thin bottleneck layers opposite to traditional residual models which use expanded representations in the input an MobileNetV2 uses lightweight depthwise convolutions to filter features in the intermediate expansion layer. Additionally, we find that it is important to remove non-linearities in the narrow layers in order to maintain representational power. We demonstrate that this improves performance and provide an intuition that led to this design. Finally, our approach allows decoupling of the input/output domains from the expressiveness of the transformation, which provides a convenient framework for further analysis. We measure our performance on Imagenet classification, COCO object detection, VOC image segmentation. We evaluate the trade-offs between accuracy, and number of operations measured by multiply-adds (MAdd), as well as the number of parameters._

Tips:

-   The checkpoints are named **mobilenet\_v2\__depth_\__size_**, for example **mobilenet\_v2\_1.0\_224**, where **1.0** is the depth multiplier (sometimes also referred to as “alpha” or the width multiplier) and **224** is the resolution of the input images the model was trained on.
    
-   Even though the checkpoint is trained on images of specific size, the model will work on images of any size. The smallest supported image size is 32x32.
    
-   One can use [MobileNetV2ImageProcessor](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ImageProcessor) to prepare images for the model.
    
-   The available image classification checkpoints are pre-trained on [ImageNet-1k](https://huggingface.co/datasets/imagenet-1k) (also referred to as ILSVRC 2012, a collection of 1.3 million images and 1,000 classes). However, the model predicts 1001 classes: the 1000 classes from ImageNet plus an extra “background” class (index 0).
    
-   The segmentation model uses a [DeepLabV3+](https://arxiv.org/abs/1802.02611) head. The available semantic segmentation checkpoints are pre-trained on [PASCAL VOC](http://host.robots.ox.ac.uk/pascal/VOC/).
    
-   The original TensorFlow checkpoints use different padding rules than PyTorch, requiring the model to determine the padding amount at inference time, since this depends on the input image size. To use native PyTorch padding behavior, create a [MobileNetV2Config](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Config) with `tf_padding = False`.
    

Unsupported features:

-   The [MobileNetV2Model](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Model) outputs a globally pooled version of the last hidden state. In the original model it is possible to use an average pooling layer with a fixed 7x7 window and stride 1 instead of global pooling. For inputs that are larger than the recommended image size, this gives a pooled output that is larger than 1x1. The Hugging Face implementation does not support this.
    
-   The original TensorFlow checkpoints include quantized models. We do not support these models as they include additional “FakeQuantization” operations to unquantize the weights.
    
-   It’s common to extract the output from the expansion layers at indices 10 and 13, as well as the output from the final 1x1 convolution layer, for downstream purposes. Using `output_hidden_states=True` returns the output from all intermediate layers. There is currently no way to limit this to specific layers.
    
-   The DeepLabV3+ segmentation head does not use the final convolution layer from the backbone, but this layer gets computed anyway. There is currently no way to tell [MobileNetV2Model](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Model) up to which layer it should run.
    

This model was contributed by [matthijs](https://huggingface.co/Matthijs). The original code and weights can be found [here for the main model](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet) and [here for DeepLabV3+](https://github.com/tensorflow/models/tree/master/research/deeplab).

## [](#resources)Resources

A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with MobileNetV2.

-   [MobileNetV2ForImageClassification](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForImageClassification) is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb).
-   See also: [Image classification task guide](../tasks/image_classification)

**Semantic segmentation**

-   [Semantic segmentation task guide](../tasks/semantic_segmentation)

If you’re interested in submitting a resource to be included here, please feel free to open a Pull Request and we’ll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource.

## [](#transformers.MobileNetV2Config)MobileNetV2Config

### class transformers.MobileNetV2Config

[](#transformers.MobileNetV2Config)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/configuration_mobilenet_v2.py#L38)

( num\_channels = 3image\_size = 224depth\_multiplier = 1.0depth\_divisible\_by = 8min\_depth = 8expand\_ratio = 6output\_stride = 32first\_layer\_is\_expansion = Truefinegrained\_output = Truehidden\_act = 'relu6'tf\_padding = Trueclassifier\_dropout\_prob = 0.8initializer\_range = 0.02layer\_norm\_eps = 0.001semantic\_loss\_ignore\_index = 255\*\*kwargs )

This is the configuration class to store the configuration of a [MobileNetV2Model](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Model). It is used to instantiate a MobileNetV2 model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the MobileNetV2 [google/mobilenet\_v2\_1.0\_224](https://huggingface.co/google/mobilenet_v2_1.0_224) architecture.

Configuration objects inherit from [PretrainedConfig](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig) and can be used to control the model outputs. Read the documentation from [PretrainedConfig](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig) for more information.

Example:

```
>>> from transformers import MobileNetV2Config, MobileNetV2Model

>>> 
>>> configuration = MobileNetV2Config()

>>> 
>>> model = MobileNetV2Model(configuration)

>>> 
>>> configuration = model.config```

## [](#transformers.MobileNetV2FeatureExtractor)MobileNetV2FeatureExtractor

( images: typing.Union\[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List\[ForwardRef('PIL.Image.Image')\], typing.List\[numpy.ndarray\], typing.List\[ForwardRef('torch.Tensor')\]\]do\_resize: typing.Optional\[bool\] = Nonesize: typing.Dict\[str, int\] = Noneresample: Resampling = Nonedo\_center\_crop: bool = Nonecrop\_size: typing.Dict\[str, int\] = Nonedo\_rescale: typing.Optional\[bool\] = Nonerescale\_factor: typing.Optional\[float\] = Nonedo\_normalize: typing.Optional\[bool\] = Noneimage\_mean: typing.Union\[float, typing.List\[float\], NoneType\] = Noneimage\_std: typing.Union\[float, typing.List\[float\], NoneType\] = Nonereturn\_tensors: typing.Union\[str, transformers.utils.generic.TensorType, NoneType\] = Nonedata\_format: typing.Union\[str, transformers.image\_utils.ChannelDimension\] = <ChannelDimension.FIRST: 'channels\_first'>\*\*kwargs )

Preprocess an image or batch of images.

( outputstarget\_sizes: typing.List\[typing.Tuple\] = None ) → `List[torch.Tensor]`

Parameters

-   [](#transformers.MobileNetV2FeatureExtractor.post_process_semantic_segmentation.outputs)**outputs** ([MobileNetV2ForSemanticSegmentation](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForSemanticSegmentation)) — Raw outputs of the model.
-   [](#transformers.MobileNetV2FeatureExtractor.post_process_semantic_segmentation.target_sizes)**target\_sizes** (`List[Tuple]`, _optional_) — A list of length `batch_size`, where each item is a `Tuple[int, int]` corresponding to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized.

A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target\_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id.

Converts the output of [MobileNetV2ForSemanticSegmentation](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForSemanticSegmentation) into semantic segmentation maps. Only supports PyTorch.

## [](#transformers.MobileNetV2ImageProcessor)MobileNetV2ImageProcessor

### class transformers.MobileNetV2ImageProcessor

[](#transformers.MobileNetV2ImageProcessor)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py#L50)

( do\_resize: bool = Truesize: typing.Union\[typing.Dict\[str, int\], NoneType\] = Noneresample: Resampling = <Resampling.BILINEAR: 2>do\_center\_crop: bool = Truecrop\_size: typing.Dict\[str, int\] = Nonedo\_rescale: bool = Truerescale\_factor: typing.Union\[int, float\] = 0.00392156862745098do\_normalize: bool = Trueimage\_mean: typing.Union\[float, typing.List\[float\], NoneType\] = Noneimage\_std: typing.Union\[float, typing.List\[float\], NoneType\] = None\*\*kwargs )

Constructs a MobileNetV2 image processor.

#### preprocess

[](#transformers.MobileNetV2ImageProcessor.preprocess)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py#L224)

( images: typing.Union\[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List\[ForwardRef('PIL.Image.Image')\], typing.List\[numpy.ndarray\], typing.List\[ForwardRef('torch.Tensor')\]\]do\_resize: typing.Optional\[bool\] = Nonesize: typing.Dict\[str, int\] = Noneresample: Resampling = Nonedo\_center\_crop: bool = Nonecrop\_size: typing.Dict\[str, int\] = Nonedo\_rescale: typing.Optional\[bool\] = Nonerescale\_factor: typing.Optional\[float\] = Nonedo\_normalize: typing.Optional\[bool\] = Noneimage\_mean: typing.Union\[float, typing.List\[float\], NoneType\] = Noneimage\_std: typing.Union\[float, typing.List\[float\], NoneType\] = Nonereturn\_tensors: typing.Union\[str, transformers.utils.generic.TensorType, NoneType\] = Nonedata\_format: typing.Union\[str, transformers.image\_utils.ChannelDimension\] = <ChannelDimension.FIRST: 'channels\_first'>\*\*kwargs )

Preprocess an image or batch of images.

#### post\_process\_semantic\_segmentation

[](#transformers.MobileNetV2ImageProcessor.post_process_semantic_segmentation)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py#L335)

( outputstarget\_sizes: typing.List\[typing.Tuple\] = None ) → `List[torch.Tensor]`

Parameters

-   [](#transformers.MobileNetV2ImageProcessor.post_process_semantic_segmentation.outputs)**outputs** ([MobileNetV2ForSemanticSegmentation](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForSemanticSegmentation)) — Raw outputs of the model.
-   [](#transformers.MobileNetV2ImageProcessor.post_process_semantic_segmentation.target_sizes)**target\_sizes** (`List[Tuple]`, _optional_) — A list of length `batch_size`, where each item is a `Tuple[int, int]` corresponding to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized.

Returns

`List[torch.Tensor]`

A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target\_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id.

Converts the output of [MobileNetV2ForSemanticSegmentation](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForSemanticSegmentation) into semantic segmentation maps. Only supports PyTorch.

## [](#transformers.MobileNetV2Model)MobileNetV2Model

### class transformers.MobileNetV2Model

[](#transformers.MobileNetV2Model)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/modeling_mobilenet_v2.py#L502)

( config: MobileNetV2Configadd\_pooling\_layer: bool = True )

Parameters

-   [](#transformers.MobileNetV2Model.config)**config** ([MobileNetV2Config](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Config)) — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.

The bare MobileNetV2 model outputting raw hidden-states without any specific head on top. This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.

#### forward

[](#transformers.MobileNetV2Model.forward)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/modeling_mobilenet_v2.py#L566)

( pixel\_values: typing.Optional\[torch.Tensor\] = Noneoutput\_hidden\_states: typing.Optional\[bool\] = Nonereturn\_dict: typing.Optional\[bool\] = None ) → `transformers.modeling_outputs.BaseModelOutputWithPoolingAndNoAttention` or `tuple(torch.FloatTensor)`

The [MobileNetV2Model](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Model) forward method, overrides the `__call__` special method.

Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.

Example:

```
>>> from transformers import AutoImageProcessor, MobileNetV2Model
>>> import torch
>>> from datasets import load_dataset

>>> dataset = load_dataset("huggingface/cats-image")
>>> image = dataset["test"]["image"][0]

>>> image_processor = AutoImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224")
>>> model = MobileNetV2Model.from_pretrained("google/mobilenet_v2_1.0_224")

>>> inputs = image_processor(image, return_tensors="pt")

>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state
>>> list(last_hidden_states.shape)
[1, 1280, 7, 7]```

## [](#transformers.MobileNetV2ForImageClassification)MobileNetV2ForImageClassification

### class transformers.MobileNetV2ForImageClassification

[](#transformers.MobileNetV2ForImageClassification)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/modeling_mobilenet_v2.py#L622)

( config: MobileNetV2Config )

Parameters

-   [](#transformers.MobileNetV2ForImageClassification.config)**config** ([MobileNetV2Config](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Config)) — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.

MobileNetV2 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet.

This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.

The [MobileNetV2ForImageClassification](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForImageClassification) forward method, overrides the `__call__` special method.

Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.

Example:

```
>>> from transformers import AutoImageProcessor, MobileNetV2ForImageClassification
>>> import torch
>>> from datasets import load_dataset

>>> dataset = load_dataset("huggingface/cats-image")
>>> image = dataset["test"]["image"][0]

>>> image_processor = AutoImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224")
>>> model = MobileNetV2ForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224")

>>> inputs = image_processor(image, return_tensors="pt")

>>> with torch.no_grad():
...     logits = model(**inputs).logits

>>> 
>>> predicted_label = logits.argmax(-1).item()
>>> print(model.config.id2label[predicted_label])
tabby, tabby cat```

## [](#transformers.MobileNetV2ForSemanticSegmentation)MobileNetV2ForSemanticSegmentation

### class transformers.MobileNetV2ForSemanticSegmentation

[](#transformers.MobileNetV2ForSemanticSegmentation)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/mobilenet_v2/modeling_mobilenet_v2.py#L781)

( config: MobileNetV2Config )

Parameters

-   [](#transformers.MobileNetV2ForSemanticSegmentation.config)**config** ([MobileNetV2Config](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2Config)) — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.

MobileNetV2 model with a semantic segmentation head on top, e.g. for Pascal VOC.

This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.

The [MobileNetV2ForSemanticSegmentation](/docs/transformers/v4.30.0/en/model_doc/mobilenet_v2#transformers.MobileNetV2ForSemanticSegmentation) forward method, overrides the `__call__` special method.

Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.

Examples:

```
>>> from transformers import AutoImageProcessor, MobileNetV2ForSemanticSegmentation
>>> from PIL import Image
>>> import requests

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> image_processor = AutoImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513")
>>> model = MobileNetV2ForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513")

>>> inputs = image_processor(images=image, return_tensors="pt")

>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> 
>>> logits = outputs.logits```