---
local: convnext
sections:
- local: overview
  title: Overview
- local: transformers.models.convnext.modeling_convnext.ConvNextModelOutput
  title: ConvNeXT specific outputs
- local: transformers.ConvNextConfig
  title: ConvNextConfig
- local: transformers.ConvNextFeatureExtractor
  title: ConvNextFeatureExtractor
- local: transformers.ConvNextModel
  title: ConvNextModel
- local: transformers.ConvNextForImageClassification
  title: ConvNextForImageClassification
title: ConvNeXT
---
<script>
import Tip from "./Tip.svelte";
import Youtube from "./Youtube.svelte";
import Docstring from "./Docstring.svelte";
import CodeBlock from "./CodeBlock.svelte";
import CodeBlockFw from "./CodeBlockFw.svelte";
import DocNotebookDropdown from "./DocNotebookDropdown.svelte";
import IconCopyLink from "./IconCopyLink.svelte";
export let fw: "pt" | "tf"
</script>
<!--Copyright 2022 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

<h1 id="convnext">ConvNeXT</h1>

<h2 id="overview">Overview</h2>

The ConvNeXT model was proposed in [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie.
ConvNeXT is a pure convolutional model (ConvNet), inspired by the design of Vision Transformers, that claims to outperform them.

The abstract from the paper is the following:

*The "Roaring 20s" of visual recognition began with the introduction of Vision Transformers (ViTs), which quickly superseded ConvNets as the state-of-the-art image classification model.
A vanilla ViT, on the other hand, faces difficulties when applied to general computer vision tasks such as object detection and semantic segmentation. It is the hierarchical Transformers
(e.g., Swin Transformers) that reintroduced several ConvNet priors, making Transformers practically viable as a generic vision backbone and demonstrating remarkable performance on a wide
variety of vision tasks. However, the effectiveness of such hybrid approaches is still largely credited to the intrinsic superiority of Transformers, rather than the inherent inductive
biases of convolutions. In this work, we reexamine the design spaces and test the limits of what a pure ConvNet can achieve. We gradually "modernize" a standard ResNet toward the design
of a vision Transformer, and discover several key components that contribute to the performance difference along the way. The outcome of this exploration is a family of pure ConvNet models
dubbed ConvNeXt. Constructed entirely from standard ConvNet modules, ConvNeXts compete favorably with Transformers in terms of accuracy and scalability, achieving 87.8% ImageNet top-1 accuracy
and outperforming Swin Transformers on COCO detection and ADE20K segmentation, while maintaining the simplicity and efficiency of standard ConvNets.*

Tips:

- See the code examples below each model regarding usage.

<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/convnext_architecture.jpg"
alt="drawing" width="600"/> 

<small> ConvNeXT architecture. Taken from the <a href="https://arxiv.org/abs/2201.03545">original paper</a>.</small>

This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/facebookresearch/ConvNeXt).

<h2 id="transformers.models.convnext.modeling_convnext.ConvNextModelOutput">ConvNeXT specific outputs</h2>

<div class="docstring">

<docstring><name>class transformers.models.convnext.modeling\_convnext.ConvNextModelOutput</name><anchor>transformers.models.convnext.modeling_convnext.ConvNextModelOutput</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/modeling_convnext.py#L76</source><parameters>[{"name": "last_hidden_state", "val": ": FloatTensor = None"}, {"name": "pooler_output", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "hidden_states", "val": ": typing.Optional[typing.Tuple[torch.FloatTensor]] = None"}]</parameters><paramsdesc>- **last_hidden_state** (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) --
  Last hidden states (final feature map) of the last stage of the model.
- **pooler_output** (`torch.FloatTensor` of shape `(batch_size, config.dim[-1])`) --
  Global average pooling of the last feature map followed by a layernorm.
- **hidden_states** (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) --
  Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
  shape `(batch_size, num_channels, height, width)`. Hidden-states (also called feature maps) of the model at
  the output of each stage.</paramsdesc><paramgroups>0</paramgroups></docstring>

Class for [ConvNextModel](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextModel)'s outputs, with potential hidden states (feature maps).




</div>

<h2 id="transformers.ConvNextConfig">ConvNextConfig</h2>

<div class="docstring">

<docstring><name>class transformers.ConvNextConfig</name><anchor>transformers.ConvNextConfig</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/configuration_convnext.py#L29</source><parameters>[{"name": "num_channels", "val": " = 3"}, {"name": "patch_size", "val": " = 4"}, {"name": "num_stages", "val": " = 4"}, {"name": "hidden_sizes", "val": " = None"}, {"name": "depths", "val": " = None"}, {"name": "hidden_act", "val": " = 'gelu'"}, {"name": "initializer_range", "val": " = 0.02"}, {"name": "layer_norm_eps", "val": " = 1e-12"}, {"name": "is_encoder_decoder", "val": " = False"}, {"name": "layer_scale_init_value", "val": " = 1e-06"}, {"name": "drop_path_rate", "val": " = 0.0"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **num_channels** (`int`, *optional*, defaults to 3) --
  The number of input channels.
- **patch_size** (`int`, optional, defaults to 4) --
  Patch size to use in the patch embedding layer.
- **num_stages** (`int`, optional, defaults to 4) --
  The number of stages in the model.
- **hidden_sizes** (`List[int]`, *optional*, defaults to [96, 192, 384, 768]) --
  Dimensionality (hidden size) at each stage.
- **depths** (`List[int]`, *optional*, defaults to [3, 3, 9, 3]) --
  Depth (number of blocks) for each stage.
- **hidden_act** (`str` or `function`, *optional*, defaults to `"gelu"`) --
  The non-linear activation function (function or string) in each block. If string, `"gelu"`, `"relu"`,
  `"selu"` and `"gelu_new"` are supported.
- **initializer_range** (`float`, *optional*, defaults to 0.02) --
  The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
- **layer_norm_eps** (`float`, *optional*, defaults to 1e-12) --
  The epsilon used by the layer normalization layers.
- **layer_scale_init_value** (`float`, *optional*, defaults to 1e-6) --
  The initial value for the layer scale.
- **drop_path_rate** (`float`, *optional*, defaults to 0.0) --
  The drop rate for stochastic depth.</paramsdesc><paramgroups>0</paramgroups></docstring>

This is the configuration class to store the configuration of a [ConvNextModel](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextModel). It is used to instantiate an
ConvNeXT 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 ConvNeXT
[facebook/convnext-tiny-224](https://huggingface.co/facebook/convnext-tiny-224) architecture.

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



Example:
```python
>>> from transformers import ConvNextModel, ConvNextConfig

>>> # Initializing a ConvNext convnext-tiny-224 style configuration
>>> configuration = ConvNextConfig()
>>> # Initializing a model from the convnext-tiny-224 style configuration
>>> model = ConvNextModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
```

</div>

<h2 id="transformers.ConvNextFeatureExtractor">ConvNextFeatureExtractor</h2>

<div class="docstring">

<docstring><name>class transformers.ConvNextFeatureExtractor</name><anchor>transformers.ConvNextFeatureExtractor</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/feature_extraction_convnext.py#L37</source><parameters>[{"name": "do_resize", "val": " = True"}, {"name": "size", "val": " = 224"}, {"name": "resample", "val": " = 3"}, {"name": "crop_pct", "val": " = None"}, {"name": "do_normalize", "val": " = True"}, {"name": "image_mean", "val": " = None"}, {"name": "image_std", "val": " = None"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **do_resize** (`bool`, *optional*, defaults to `True`) --
  Whether to resize (and optionally center crop) the input to a certain `size`.
- **size** (`int`, *optional*, defaults to 224) --
  Resize the input to the given size. If 384 or larger, the image is resized to (`size`, `size`). Else, the
  smaller edge of the image will be matched to int(`size`/ `crop_pct`), after which the image is cropped to
  `size`. Only has an effect if `do_resize` is set to `True`.
- **resample** (`int`, *optional*, defaults to `PIL.Image.BICUBIC`) --
  An optional resampling filter. This can be one of `PIL.Image.NEAREST`, `PIL.Image.BOX`,
  `PIL.Image.BILINEAR`, `PIL.Image.HAMMING`, `PIL.Image.BICUBIC` or `PIL.Image.LANCZOS`. Only has an effect
  if `do_resize` is set to `True`.
- **crop_pct** (`float`, *optional*) --
  The percentage of the image to crop. If `None`, then a cropping percentage of 224 / 256 is used. Only has
  an effect if `do_resize` is set to `True` and `size` &amp;lt; 384.
- **do_normalize** (`bool`, *optional*, defaults to `True`) --
  Whether or not to normalize the input with mean and standard deviation.
- **image_mean** (`List[int]`, defaults to `[0.485, 0.456, 0.406]`) --
  The sequence of means for each channel, to be used when normalizing images.
- **image_std** (`List[int]`, defaults to `[0.229, 0.224, 0.225]`) --
  The sequence of standard deviations for each channel, to be used when normalizing images.</paramsdesc><paramgroups>0</paramgroups></docstring>

Constructs a ConvNeXT feature extractor.

This feature extractor inherits from [FeatureExtractionMixin](/docs/transformers/pr_15592/en/main_classes/feature_extractor#transformers.FeatureExtractionMixin) which contains most of the main methods. Users
should refer to this superclass for more information regarding those methods.




</div>

<h2 id="transformers.ConvNextModel">ConvNextModel</h2>

<div class="docstring">

<docstring><name>class transformers.ConvNextModel</name><anchor>transformers.ConvNextModel</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/modeling_convnext.py#L368</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([ConvNextConfig](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextConfig)) -- 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/pr_15592/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring>
The bare ConvNext 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.





<div class="docstring">
<docstring><name>forward</name><anchor>transformers.ConvNextModel.forward</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/modeling_convnext.py#L382</source><parameters>[{"name": "pixel_values", "val": " = None"}, {"name": "output_hidden_states", "val": " = None"}, {"name": "return_dict", "val": " = None"}]</parameters><paramsdesc>- **pixel_values** (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) --
  Pixel values. Pixel values can be obtained using [AutoFeatureExtractor](/docs/transformers/pr_15592/en/model_doc/auto#transformers.AutoFeatureExtractor). See
  `AutoFeatureExtractor.__call__()`for details.

- **output_hidden_states** (`bool`, *optional*) --
  Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
  more detail.
- **return_dict** (`bool`, *optional*) --
  Whether or not to return a [ModelOutput](/docs/transformers/pr_15592/en/main_classes/output#transformers.file_utils.ModelOutput) instead of a plain tuple.</paramsdesc><paramgroups>0</paramgroups><rettype>[transformers.models.convnext.modeling_convnext.ConvNextModelOutput](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.models.convnext.modeling_convnext.ConvNextModelOutput) or `tuple(torch.FloatTensor)`</rettype><retdesc>A [transformers.models.convnext.modeling_convnext.ConvNextModelOutput](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.models.convnext.modeling_convnext.ConvNextModelOutput) or a tuple of
`torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various
elements depending on the configuration ([ConvNextConfig](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextConfig)) and inputs.

- **last_hidden_state** (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) -- Last hidden states (final feature map) of the last stage of the model.
- **pooler_output** (`torch.FloatTensor` of shape `(batch_size, config.dim[-1])`) -- Global average pooling of the last feature map followed by a layernorm.
- **hidden_states** (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) -- Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
  shape `(batch_size, num_channels, height, width)`. Hidden-states (also called feature maps) of the model at
  the output of each stage.</retdesc></docstring>
The [ConvNextModel](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextModel) forward method, overrides the `__call__` special method.

<Tip>

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.

</Tip>







Example:

```python
>>> from transformers import ConvNextFeatureExtractor, ConvNextModel
>>> import torch
>>> from datasets import load_dataset

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

>>> feature_extractor = ConvNextFeatureExtractor.from_pretrained("facebook/convnext-tiny-224")
>>> model = ConvNextModel.from_pretrained("facebook/convnext-tiny-224")

>>> inputs = feature_extractor(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]
```


</div></div>

<h2 id="transformers.ConvNextForImageClassification">ConvNextForImageClassification</h2>

<div class="docstring">

<docstring><name>class transformers.ConvNextForImageClassification</name><anchor>transformers.ConvNextForImageClassification</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/modeling_convnext.py#L430</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([ConvNextConfig](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextConfig)) -- 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/pr_15592/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring>

ConvNext 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.





<div class="docstring">
<docstring><name>forward</name><anchor>transformers.ConvNextForImageClassification.forward</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/convnext/modeling_convnext.py#L445</source><parameters>[{"name": "pixel_values", "val": " = None"}, {"name": "labels", "val": " = None"}, {"name": "output_hidden_states", "val": " = None"}, {"name": "return_dict", "val": " = None"}]</parameters><paramsdesc>- **pixel_values** (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) --
  Pixel values. Pixel values can be obtained using [AutoFeatureExtractor](/docs/transformers/pr_15592/en/model_doc/auto#transformers.AutoFeatureExtractor). See
  `AutoFeatureExtractor.__call__()`for details.

- **output_hidden_states** (`bool`, *optional*) --
  Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
  more detail.
- **return_dict** (`bool`, *optional*) --
  Whether or not to return a [ModelOutput](/docs/transformers/pr_15592/en/main_classes/output#transformers.file_utils.ModelOutput) instead of a plain tuple.

- **labels** (`torch.LongTensor` of shape `(batch_size,)`, *optional*) --
  Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
  config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
  `config.num_labels > 1` a classification loss is computed (Cross-Entropy).</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.convnext.modeling_convnext.ConvNextClassifierOutput`or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.convnext.modeling_convnext.ConvNextClassifierOutput`or a tuple of
`torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various
elements depending on the configuration ([ConvNextConfig](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextConfig)) and inputs.

- **loss** (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided) -- Classification (or regression if config.num_labels==1) loss.
- **logits** (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`) -- Classification (or regression if config.num_labels==1) scores (before SoftMax).
- **hidden_states** (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) -- Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
  shape `(batch_size, num_channels, height, width)`. Hidden-states (also called feature maps) of the model at
  the output of each stage.</retdesc></docstring>
The [ConvNextForImageClassification](/docs/transformers/pr_15592/en/model_doc/convnext#transformers.ConvNextForImageClassification) forward method, overrides the `__call__` special method.

<Tip>

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.

</Tip>







Example:

```python
>>> from transformers import ConvNextFeatureExtractor, ConvNextForImageClassification
>>> import torch
>>> from datasets import load_dataset

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

>>> feature_extractor = ConvNextFeatureExtractor.from_pretrained("facebook/convnext-tiny-224")
>>> model = ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224")

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

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

>>> # model predicts one of the 1000 ImageNet classes
>>> predicted_label = logits.argmax(-1).item()
>>> print(model.config.id2label[predicted_label])
'tabby, tabby cat'
```


</div></div>
