EfficientNet

## [](#overview)Overview

The EfficientNet model was proposed in [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946) by Mingxing Tan and Quoc V. Le. EfficientNets are a family of image classification models, which achieve state-of-the-art accuracy, yet being an order-of-magnitude smaller and faster than previous models.

The abstract from the paper is the following:

_Convolutional Neural Networks (ConvNets) are commonly developed at a fixed resource budget, and then scaled up for better accuracy if more resources are available. In this paper, we systematically study model scaling and identify that carefully balancing network depth, width, and resolution can lead to better performance. Based on this observation, we propose a new scaling method that uniformly scales all dimensions of depth/width/resolution using a simple yet highly effective compound coefficient. We demonstrate the effectiveness of this method on scaling up MobileNets and ResNet. To go even further, we use neural architecture search to design a new baseline network and scale it up to obtain a family of models, called EfficientNets, which achieve much better accuracy and efficiency than previous ConvNets. In particular, our EfficientNet-B7 achieves state-of-the-art 84.3% top-1 accuracy on ImageNet, while being 8.4x smaller and 6.1x faster on inference than the best existing ConvNet. Our EfficientNets also transfer well and achieve state-of-the-art accuracy on CIFAR-100 (91.7%), Flowers (98.8%), and 3 other transfer learning datasets, with an order of magnitude fewer parameters._

This model was contributed by [adirik](https://huggingface.co/adirik). The original code can be found [here](https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet).

## [](#transformers.EfficientNetConfig)EfficientNetConfig

### class transformers.EfficientNetConfig

[](#transformers.EfficientNetConfig)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/configuration_efficientnet.py#L34)

( num\_channels: int = 3image\_size: int = 600width\_coefficient: float = 2.0depth\_coefficient: float = 3.1depth\_divisor: int = 8kernel\_sizes: typing.List\[int\] = \[3, 3, 5, 3, 5, 5, 3\]in\_channels: typing.List\[int\] = \[32, 16, 24, 40, 80, 112, 192\]out\_channels: typing.List\[int\] = \[16, 24, 40, 80, 112, 192, 320\]depthwise\_padding: typing.List\[int\] = \[\]strides: typing.List\[int\] = \[1, 2, 2, 2, 1, 2, 1\]num\_block\_repeats: typing.List\[int\] = \[1, 2, 2, 3, 3, 4, 1\]expand\_ratios: typing.List\[int\] = \[1, 6, 6, 6, 6, 6, 6\]squeeze\_expansion\_ratio: float = 0.25hidden\_act: str = 'swish'hidden\_dim: int = 2560pooling\_type: str = 'mean'initializer\_range: float = 0.02batch\_norm\_eps: float = 0.001batch\_norm\_momentum: float = 0.99dropout\_rate: float = 0.5drop\_connect\_rate: float = 0.2\*\*kwargs )

This is the configuration class to store the configuration of a [EfficientNetModel](/docs/transformers/v4.30.0/en/model_doc/efficientnet#transformers.EfficientNetModel). It is used to instantiate an EfficientNet 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 EfficientNet [google/efficientnet-b7](https://huggingface.co/google/efficientnet-b7) 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 EfficientNetConfig, EfficientNetModel

>>> 
>>> configuration = EfficientNetConfig()

>>> 
>>> model = EfficientNetModel(configuration)

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

## [](#transformers.EfficientNetImageProcessor)EfficientNetImageProcessor

### class transformers.EfficientNetImageProcessor

[](#transformers.EfficientNetImageProcessor)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/image_processing_efficientnet.py#L43)

( do\_resize: bool = Truesize: typing.Dict\[str, int\] = Noneresample: Resampling = 0do\_center\_crop: bool = Falsecrop\_size: typing.Dict\[str, int\] = Nonerescale\_factor: typing.Union\[int, float\] = 0.00392156862745098rescale\_offset: bool = Falsedo\_rescale: bool = Truedo\_normalize: bool = Trueimage\_mean: typing.Union\[float, typing.List\[float\], NoneType\] = Noneimage\_std: typing.Union\[float, typing.List\[float\], NoneType\] = Noneinclude\_top: bool = True\*\*kwargs )

Constructs a EfficientNet image processor.

#### preprocess

[](#transformers.EfficientNetImageProcessor.preprocess)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/image_processing_efficientnet.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: bool = Nonesize: typing.Dict\[str, int\] = Noneresample = Nonedo\_center\_crop: bool = Nonecrop\_size: typing.Dict\[str, int\] = Nonedo\_rescale: bool = Nonerescale\_factor: float = Nonerescale\_offset: bool = Nonedo\_normalize: bool = Noneimage\_mean: typing.Union\[float, typing.List\[float\], NoneType\] = Noneimage\_std: typing.Union\[float, typing.List\[float\], NoneType\] = Noneinclude\_top: bool = Nonereturn\_tensors: typing.Union\[str, transformers.utils.generic.TensorType, NoneType\] = Nonedata\_format: ChannelDimension = <ChannelDimension.FIRST: 'channels\_first'>\*\*kwargs )

Preprocess an image or batch of images.

## [](#transformers.EfficientNetModel)EfficientNetModel

### class transformers.EfficientNetModel

[](#transformers.EfficientNetModel)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/modeling_efficientnet.py#L512)

( config: EfficientNetConfig )

Parameters

-   [](#transformers.EfficientNetModel.config)**config** ([EfficientNetConfig](/docs/transformers/v4.30.0/en/model_doc/efficientnet#transformers.EfficientNetConfig)) — 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 EfficientNet model outputting raw features 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.EfficientNetModel.forward)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/modeling_efficientnet.py#L530)

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

The [EfficientNetModel](/docs/transformers/v4.30.0/en/model_doc/efficientnet#transformers.EfficientNetModel) 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, EfficientNetModel
>>> import torch
>>> from datasets import load_dataset

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

>>> image_processor = AutoImageProcessor.from_pretrained("google/efficientnet-b7")
>>> model = EfficientNetModel.from_pretrained("google/efficientnet-b7")

>>> 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, 768, 7, 7]```

## [](#transformers.EfficientNetForImageClassification)EfficientNetForImageClassification

### class transformers.EfficientNetForImageClassification

[](#transformers.EfficientNetForImageClassification)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/efficientnet/modeling_efficientnet.py#L582)

( config )

Parameters

-   [](#transformers.EfficientNetForImageClassification.config)**config** ([EfficientNetConfig](/docs/transformers/v4.30.0/en/model_doc/efficientnet#transformers.EfficientNetConfig)) — 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.

EfficientNet 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 [EfficientNetForImageClassification](/docs/transformers/v4.30.0/en/model_doc/efficientnet#transformers.EfficientNetForImageClassification) 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, EfficientNetForImageClassification
>>> import torch
>>> from datasets import load_dataset

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

>>> image_processor = AutoImageProcessor.from_pretrained("google/efficientnet-b7")
>>> model = EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7")

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