Search

Search papers and lessons

A ConvNet for the 2020s

Zhuang Liu, Hanzi Mao, Chao-Yuan Wu et al. · 2022

Liu et al. take a standard ResNet, change it one design choice at a time toward a Swin Transformer, and get ConvNeXt, a pure convolutional network that matches Swin on ImageNet, COCO and ADE20K.

On this page

  • Colour = topic mix
  • Calculus
  • Neural network basics
  • Discrete math
  • Probability
  • Architectures
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Vision Transformers overtook convolutional networks on image classification around 2020, and the hierarchical Swin Transformer then beat them on detection and segmentation too. Liu and colleagues asked whether the advantage came from attention itself or from the training recipe and layout choices that came with it. They started from a ResNet-50, changed one design decision at a time toward the Swin Transformer, and kept the changes that helped. The result, ConvNeXt, uses only standard convolution modules and matches or beats Swin on several benchmarks.

Builds on: Residual Connection, Depthwise Separable Convolution, Layer Normalization, Gaussian Error Linear Unit (GELU), Vision Transformer (ViT) · Leads to: convolutional networks as a serious option for new vision backbones

Why it mattered

ConvNeXt changed a belief that had settled into the field: that attention-based backbones were simply better. After the paper, a plain ConvNet could be the default choice in a vision project without a loss in accuracy. The paper reports 87.8% ImageNet-1K top-1 accuracy for its largest model, ConvNeXt-XL, when it is pre-trained on ImageNet-22K and tested at 384 by 384 pixels. Its object detection and segmentation results on COCO and ADE20K put it level with or slightly ahead of Swin at comparable sizes. The code is public at github.com/facebookresearch/ConvNeXt.

The method, changing one design choice at a time and recording each step, is also a model for comparing architectures. Many gains that look like "the new architecture" turn out to come from the training procedure, the stem, or the normalization layer.

The problem

Before this paper, comparing a ConvNet with a Transformer usually compared two complete systems that differed in many ways at once: the training recipe, the optimizer, the augmentation, the normalization, the activation, the block shape and the stage layout. The two families are built on similar inductive biases, but they differ in training procedure and in macro and micro architecture design, so the comparison mixes all of these differences together.

The question the paper asks is narrow: how do design decisions in Transformers impact ConvNets' performance? To answer it, the authors need a fixed starting point and a fixed measuring stick. The starting point is ResNet-50. The measuring stick is ImageNet-1K top-1 accuracy at a similar compute budget (about 4.5 billion FLOPs, the "ResNet-50 / Swin-T regime").

Two facts make this hard. A vanilla Vision Transformer uses global self-attention, whose cost grows quadratically with the number of input tokens, so it struggles with the high-resolution inputs that detection and segmentation need. And the hierarchical Swin Transformer reintroduces local windows and a multi-stage layout, which are ConvNet ideas. So the question becomes which of Swin's design choices carry the performance, and whether they can be written as plain convolutions.

How it works

The paper's method is a modernization procedure. Here is the pipeline in the order it runs, with the accuracy after each step in the ResNet-50 regime, from the paper's Appendix C:

  1. Start with a strong baseline. Train a standard ResNet-50 with the vision Transformer training recipe (AdamW, heavy data augmentation, 300 epochs). Accuracy goes from 76.1% to 78.8%, a gain of 2.7 points from training alone.
  2. Change the stage compute ratio. Swin-T puts its blocks in a 1:1:3:1 ratio across four stages. The ResNet-50 block counts (3, 4, 6, 3) become (3, 3, 9, 3). Accuracy goes from 78.8% to 79.4%.
  3. Replace the stem with a "patchify" layer. The ResNet stem (a 7 by 7 stride-2 convolution plus max pooling) becomes one 4 by 4 convolution with stride 4 and no overlap. Accuracy goes from 79.4% to 79.5%.
  4. Use depthwise convolution and widen. Each channel gets its own spatial filter, which is cheap but drops accuracy to 78.3%. Widening the network to 96 channels in the first stage, the width of Swin-T, brings it back to 80.5%, at 5.3 billion FLOPs.
  5. Invert the bottleneck. The block first widens to 4 times the input channels, then projects back down. Accuracy goes from 80.5% to 80.6%, while total FLOPs drop to 4.6 billion.
  6. Move the depthwise layer up. The spatial filter moves to the start of the block, so the expensive spatial step works on fewer channels. This costs accuracy at first (79.9%) but makes large kernels affordable.
  7. Use 7 by 7 kernels. Accuracy rises from 79.9% with 3 by 3 kernels to 80.6% with 7 by 7, at about the same FLOPs.
  8. Use fewer activations and normalization layers. Swapping ReLU for GELU gives 80.6%. A Transformer block has one nonlinearity in its MLP, so the ConvNeXt block keeps one GELU between the two 1 by 1 layers. Accuracy goes to 81.3%, then 81.4% when only one normalization layer remains.
  9. Replace BatchNorm with LayerNorm. Accuracy goes to 81.5%.
  10. Use separate downsampling layers. Stages are joined by their own 2 by 2 stride-2 convolutions, with normalization around them. Accuracy goes to 82.0%, above Swin-T's 81.3%.

The final block is shown below. The input has C channels; the output has the same shape and is added back to the input.

flowchart LR
  X["input x, C channels"] --> DW["7 by 7 depthwise conv"]
  DW --> LN["LayerNorm"]
  LN --> E["1 by 1 conv, expand to 4C"]
  E --> G["GELU"]
  G --> P["1 by 1 conv, project to C"]
  P --> S["layer scale"]
  X --> ADD(("+"))
  S --> ADD
  ADD --> Y["output y, C channels"]

A tiny instance shows the spatial step. Take a one-channel row of three values, x = (1, 2, 3), and a depthwise kernel of three weights (1, 1, 1) with zeros outside the row. Sliding the kernel across the row:

  • position 1: 0 + 1 + 2 = 3
  • position 2: 1 + 2 + 3 = 6
  • position 3: 2 + 3 + 0 = 5

The output is (3, 6, 5). Each output value depends only on nearby inputs, and no channel is mixed with another. The 1 by 1 layers do the opposite: at each position they mix channels and never look at neighbours. The paper's observation is that a Transformer block also separates these two jobs, with attention or windows mixing space and the MLP mixing channels.

Now trace one real-sized input through the whole network. A 224 by 224 RGB image enters ConvNeXt-T, whose stage widths are (96, 192, 384, 768) and block counts (3, 3, 9, 3):

  1. The stem, a 4 by 4 convolution with stride 4, gives 224/4=56224 / 4 = 56: a 56 by 56 map with 96 channels.
  2. Stage 1 runs three blocks at 56 by 56.
  3. A 2 by 2 stride-2 convolution halves the resolution to 28 by 28 and doubles the channels to 192. Stage 2 runs three blocks there.
  4. Another downsampling gives 14 by 14 with 384 channels. Stage 3 runs nine blocks, which is the 1:1:9:1 ratio from step 2.
  5. Downsampling gives 7 by 7 with 768 channels. Stage 4 runs three blocks.
  6. Global average pooling over the 7 by 7 grid gives one vector of 768 numbers for the classifier.

The math

Let the input be a feature map X with H rows, W columns and C channels, and write X[i, j, c] for the value at row i, column j, channel c.

Definition: Depthwise convolution

A depthwise convolution with a k by k kernel K has one filter per channel. Each output value depends only on the same channel of the input: Y[i,j,c]=∑a=0k−1∑b=0k−1K[a,b,c] X[i+a, j+b, c].Y[i,j,c] = \sum_{a=0}^{k-1}\sum_{b=0}^{k-1} K[a,b,c]\, X[i+a,\, j+b,\, c]. Its parameter count is k2Ck^2 C. A standard convolution with the same kernel size and CC output channels has k2C2k^2 C^2 parameters, because every output channel reads every input channel.

Step 4 in symbols

The sum above is the row example. For k = 3, C = 1 and the row (1, 2, 3), the kernel K = (1, 1, 1) gives Y = (3, 6, 5), with zero padding at both ends.

Steps 5 and 8 in symbols

A 1 by 1 convolution applied to C channels at one position is a matrix-vector product y=Wx\mathbf{y} = W\mathbf{x}, with W∈RC′×CW \in \mathbb{R}^{C' \times C}. The inverted bottleneck uses C′=4CC' = 4C and then C′=CC' = C. The block has one nonlinearity in between, so it computes

y=x+γ⊙(W2 σ ⁣(W1 LN(DW7×7(x)))),\mathbf{y} = \mathbf{x} + \gamma \odot \left( W_2 \, \sigma\!\left( W_1 \, \mathrm{LN}\big(\mathrm{DW}_{7\times 7}(\mathbf{x})\big) \right) \right),

where DW7×7\mathrm{DW}_{7\times 7} is the 7 by 7 depthwise convolution applied position by position, LN\mathrm{LN} is layer normalization over the channels at one position, W1∈R4C×CW_1 \in \mathbb{R}^{4C \times C}, W2∈RC×4CW_2 \in \mathbb{R}^{C \times 4C}, σ\sigma is GELU, γ\gamma is the layer-scale vector and ⊙\odot multiplies element by element. The paper's Table 5 gives the layer-scale initial value as 10−610^{-6}.

Parameter count for one block

Take C = 96, the width of the first ConvNeXt-T stage:

  • depthwise 7 by 7: 49×96=4,70449 \times 96 = 4{,}704 weights.
  • standard 7 by 7 convolution with 96 in and 96 out: 49×96×96=451,58449 \times 96 \times 96 = 451{,}584 weights.
  • the two 1 by 1 layers: 4×96×96+4×96×96=73,7284 \times 96 \times 96 + 4 \times 96 \times 96 = 73{,}728 weights.

So the depthwise layer is 96 times smaller than a dense 7 by 7 convolution with the same channels, and the block is dominated by the two 1 by 1 layers. Summed, the block has 73,728+4,704=78,43273{,}728 + 4{,}704 = 78{,}432 weights, ignoring biases and normalization parameters.

GELU and LayerNorm, the two new pieces

GELU is GELU(x)=x Φ(x)\mathrm{GELU}(x) = x\,\Phi(x), where Φ\Phi is the cumulative distribution function of a standard Gaussian. Since Φ(1)≈0.841\Phi(1) \approx 0.841 and Φ(−1)≈0.159\Phi(-1) \approx 0.159, GELU(1) is about 0.841 and GELU(-1) is about -0.159. It is near zero for large negative inputs and near the identity for large positive ones, a smooth version of ReLU.

LayerNorm rescales the channels of one position to mean 0 and variance 1. For the vector (1, 2, 3), the mean is 2 and the variance is (1+0+1)/3=2/3(1 + 0 + 1)/3 = 2/3, so the output is (−1/2/3,0,1/2/3)≈(−1.225,0,1.225)(-1/\sqrt{2/3}, 0, 1/\sqrt{2/3}) \approx (-1.225, 0, 1.225). Unlike BatchNorm, the statistics come from one example, so the result does not depend on the other images in the batch.

Results

All numbers below are from the paper's text and tables.

  • ImageNet-1K, same regime. ConvNeXt-T reaches 82.1% top-1 with 29 million parameters and 4.5 billion FLOPs. Swin-T reaches 81.3% with 28 million parameters and 4.5 billion FLOPs. ConvNeXt-B at 384 by 384 resolution reaches 85.1% against 84.5% for Swin-B, with throughput of 95.7 against 85.1 images per second on a V100 (Table 1).
  • ImageNet-22K pre-training. ConvNeXt-XL at 384 by 384 reaches 87.8%, and the paper says ConvNeXts "still perform on par or better than similarly-sized Swin Transformers" at this scale.
  • COCO object detection, Cascade Mask R-CNN with ImageNet-22K pre-training. ConvNeXt-B reaches 54.0 box AP against 53.0 for Swin-B, and ConvNeXt-L reaches 54.8 against 53.9 for Swin-L (Table 3).
  • ADE20K semantic segmentation, UperNet, multi-scale testing. ConvNeXt-T reaches 46.7 mIoU against 45.8 for Swin-T, both with ImageNet-1K pre-training (Table 4).
  • Isotropic variant. On the same width and depth as ViT, a ConvNeXt block performs about the same as ViT: 79.7% against 79.8% at the small size, and 82.6% for both at the large size (Table 2).
  • Speed on an A100. With TF32 and channel-last memory layout, ConvNeXt-T runs at 1943.5 images per second against 1325.6 for Swin-T, about 47 percent faster (Appendix E, Table 12).

Limits, from the paper and its reading:

  • The comparison is with Swin on ImageNet-1K, COCO and ADE20K under the paper's settings. The paper does not claim convolutions win on every task. Its limitations appendix says cross-attention may be preferable for multi-modal feature interactions, and that Transformers may be more flexible for tasks needing discretized, sparse or structured outputs.
  • The big ImageNet-22K gains depend on large pre-training data. The paper's societal-impact appendix states that its findings indicate ConvNeXt models benefit from pre-training on large-scale datasets.
  • Depthwise convolutions are not always fast. The paper notes that models with them are known to be slower and more memory-hungry than dense ConvNets at similar FLOPs. ConvNeXt's advantage over Swin on the V100 is modest, and it is larger only on the A100 with channel-last layout.
  • The ResNet-50 steps in Table 10 are means over three seeds, with standard deviations mostly between 0.02 and 0.18 points. Differences of a few tenths are therefore meaningful, but changes of a tenth of a point are close to noise.

Reading guide

Read next: Vision Transformer (ViT) for the design ConvNeXt modernizes, Deep Residual Learning for Image Recognition (ResNet) for the baseline it starts from, and Swin Transformer: Hierarchical Vision Transformer using Shifted Windows for the design it chases.

Check yourself

QuestionWhich change in the roadmap moved ImageNet-1K accuracy from 78.8% to 79.4%, and what did it change?Show answer

The stage compute ratio changed from ResNet's (3, 4, 6, 3) blocks to (3, 3, 9, 3). This moves most of the blocks into the 14 by 14 stage, matching the 1:1:9:1 ratio the paper uses for the larger Swin models. The paper reports the accuracy change from 78.8% to 79.4%.

QuestionA depthwise 7 by 7 convolution runs on 192 channels. How many weights does it have, and how many would a dense 7 by 7 convolution with 192 input and 192 output channels have?Show answer

Depthwise: 49 times 192 = 9,408 weights. Dense: 49 times 192 times 192 = 49 times 36,864 = 1,806,336 weights. The depthwise layer is 192 times smaller, because it has one filter per channel and the dense layer has one per input-output pair.

QuestionWhy does the paper use LayerNorm rather than BatchNorm in the final block?Show answer

In the paper's ablation, accuracy moves slightly, from 81.4% to 81.5%. LayerNorm computes its statistics from the channels of one example at one position, so its output does not depend on the other images in the batch. LayerNorm is also the normalization Transformer blocks use, which ConvNeXt imitates. In the paper's Table 10 the swap moves accuracy from 81.41% to 81.47%.

QuestionA ConvNeXt-T model is given a 224 by 224 image. What resolution and channel count does its third stage run at, and how many blocks does that stage have?Show answer

Stage 3 runs at 14 by 14 with 384 channels, after two downsamplings (224 to 56 at the stem, then 56 to 28 and 28 to 14). It has nine blocks, from the configuration (3, 3, 9, 3).

Sources

Read the full paper

Zhuang Liu, Hanzi Mao, Chao-Yuan Wu et al., 2022. arXiv:2201.03545