Search

Search papers and lessons

Deep Residual Learning for Image Recognition (ResNet)

Kaiming He, Xiangyu Zhang, Shaoqing Ren et al. · 2015

He et al. make very deep networks trainable by having each block learn a residual added to its input, which fixes a degradation problem where deeper plain nets trained worse.

On this page

  • Colour = topic mix
  • Calculus
  • Neural network basics
  • Architectures
  • Linear algebra
  • Probability
  • Optimizers
  • Sign in to see your progress

TL;DR

Deeper plain networks had a strange failure: adding layers raised the training error, so the bigger model did worse on the training set itself. He et al. (2015) fix this by having each block learn a residual, the change to its input, and add the input back through a shortcut. A block that does nothing is then easy to represent, and a 152-layer network trains and beats shallower ones, where a 34-layer plain network does worse than an 18-layer one. The shortcut adds no parameters and trains with ordinary SGD and backpropagation.

Where you'll see it: almost every deep vision backbone since 2015, and the skip connections in transformers and other deep architectures.

The problem

By 2015, deep convolutional networks had brought large gains on ImageNet, and depth was the main lever. Vanishing and exploding gradients had been mostly handled by better initialisation and by batch normalisation, so networks with tens of layers could start converging. A new problem then appeared, which the paper calls degradation. As depth increased, accuracy saturated and then fell, and the fall showed up in training error as well as test error. Overfitting cannot explain a higher training error.

The paper also gives a thought experiment. Take a shallow model that trains well and append layers that compute the identity. The deeper model then has a solution with the same training error. Since the solver fails to find it, the problem is optimisation, not capacity.

Key idea

Instead of asking a stack of layers to learn a target mapping H(x)\mathcal{H}(\mathbf{x}) directly, ask it to learn the residual F(x)=H(x)−x\mathcal{F}(\mathbf{x}) = \mathcal{H}(\mathbf{x}) - \mathbf{x}, and add x\mathbf{x} back. If the best block is the identity, the layers only have to push their weights to zero. Pushing weights to zero is easy for gradient descent, whereas fitting an identity map with several nonlinear layers was what the solver could not do.

flowchart LR
  X[input x] --> C1[conv, BN, ReLU]
  C1 --> C2[conv, BN]
  X --> ADD((+))
  C2 --> ADD
  ADD --> R[ReLU]
  R --> Y[output]

The arrow that skips the layers is the shortcut connection. It carries the input unchanged.

The method

A residual block computes

y=F(x,{Wi})+x\mathbf{y} = \mathcal{F}(\mathbf{x}, \{W_i\}) + \mathbf{x}

where F\mathcal{F} is a small stack of layers. In the paper's two-layer version, F=W2σ(W1x)\mathcal{F} = W_2 \sigma(W_1 \mathbf{x}) with σ\sigma the ReLU, and the output takes a second ReLU after the addition. The addition is element-wise and the shortcut has no weights, so the block has the same parameter count and cost as the plain two-layer block it replaces.

When the input and output shapes differ, for example when the channel count doubles, the shortcut needs a projection:

y=F(x,{Wi})+Wsx\mathbf{y} = \mathcal{F}(\mathbf{x}, \{W_i\}) + W_s \mathbf{x}

where WsW_s is a linear map, implemented as a 1×11 \times 1 convolution. The paper compares three options for these projections, zero-padding (option A), projection only when dimensions change (option B) and projection everywhere (option C). The three are close (B is slightly better than A, and C marginally better than B), so projections are not essential, and the identity shortcuts keep the model small. The 50, 101 and 152-layer models use option B.

Why the gradient survives

Differentiate the block with respect to its input:

∂y∂x=I+∂F∂x\frac{\partial \mathbf{y}}{\partial \mathbf{x}} = \mathbf{I} + \frac{\partial \mathcal{F}}{\partial \mathbf{x}}

(This ignores the ReLU after the addition, which the paper's block keeps.) The identity term I\mathbf{I} passes gradient straight through. Stacking blocks multiplies these factors, and even if every ∂F∂x\frac{\partial \mathcal{F}}{\partial \mathbf{x}} is small, the product stays close to the identity rather than shrinking. This is the mechanism behind the shortcut. The paper does not claim it removes all difficulty, though. It argues that the degradation is an optimisation problem that the shortcut makes easier, and it says vanishing gradients are unlikely to be the cause of the plain-network failure, since batch normalisation keeps the forward and backward signals healthy.

Architecture

The paper's plain baseline follows VGG's design rules. Convolutions are 3×33 \times 3. If the feature map is halved in size, the number of filters doubles, so the cost per layer stays roughly constant. Downsampling uses stride-2 convolutions. The network ends with global average pooling, a 1000-way fully connected layer and softmax. The 34-layer plain net takes 3.6 billion multiply-adds, against 19.6 billion for VGG-19. The residual version inserts shortcuts across each pair of 3×33 \times 3 convolutions.

For ImageNet's 50, 101 and 152-layer models the paper uses a bottleneck block instead, with 1×11 \times 1, 3×33 \times 3 and 1×11 \times 1 convolutions. The first 1×11 \times 1 reduces the channels, the 3×33 \times 3 works on the narrow representation, and the last 1×11 \times 1 restores the width. This keeps the cost manageable at great depth.

Example
A residual block with one weight

Use a scalar version so every step can be checked by hand. Let F(x)=w2 ReLU(w1x)\mathcal{F}(x) = w_2 \, \mathrm{ReLU}(w_1 x) with w1=0.5w_1 = 0.5 and w2=0.1w_2 = 0.1, and input x=2x = 2.

  1. Inner layer: w1x=0.5×2=1.0w_1 x = 0.5 \times 2 = 1.0, and ReLU leaves it at 1.01.0.
  2. Second layer: w2×1.0=0.1w_2 \times 1.0 = 0.1, so F(2)=0.1\mathcal{F}(2) = 0.1.
  3. Block output: y=F(2)+2=2.1y = \mathcal{F}(2) + 2 = 2.1.
  4. Now set w2=0w_2 = 0. Then F(x)=0\mathcal{F}(x) = 0 for every xx, and y=xy = x exactly. The block has become the identity, and it got there by pushing one weight to zero.
  5. Derivative: dydx=1+dFdx\frac{dy}{dx} = 1 + \frac{d\mathcal{F}}{dx}. With w2=0w_2 = 0 the derivative is exactly 11, so the gradient passes unchanged, whatever the earlier layers do.

Interpretation: the block only has to learn the correction 0.10.1. If the right answer were 22, the block could simply learn F=0\mathcal{F}=0, which is the easy direction for gradient descent to find.

Example
Why the bottleneck costs about the same as the basic block

Compare two blocks at the same resolution, ignoring biases and batch-norm parameters. The basic block for ResNet-34 at 64 channels has two 3×33 \times 3 convolutions, each with 3×3×64×643 \times 3 \times 64 \times 64 weights.

  1. Basic block: 2×(9×64×64)=2×36,864=73,7282 \times (9 \times 64 \times 64) = 2 \times 36{,}864 = 73{,}728 weights.
  2. Bottleneck block with 256 output channels: a 1×11 \times 1 conv from 256256 to 6464 has 256×64=16,384256 \times 64 = 16{,}384 weights. The 3×33 \times 3 conv from 6464 to 6464 has 36,86436{,}864. The 1×11 \times 1 conv from 6464 to 256256 has 16,38416{,}384.
  3. Bottleneck total: 16,384+36,864+16,384=69,63216{,}384 + 36{,}864 + 16{,}384 = 69{,}632 weights.

Interpretation: the bottleneck's output has four times the channels of the basic block, and its weight count is still slightly lower. This is what lets the paper reach 152 layers at 11.3 billion FLOPs, below VGG-16 (15.3 billion) and VGG-19 (19.6 billion).

Results

ImageNet training is standard: SGD with momentum 0.9, weight decay 0.0001, initial learning rate 0.1 divided by 10 when the error plateaus, batch normalisation after each convolution, and no dropout. Scale augmentation and 10-crop testing follow earlier ImageNet work. (The CIFAR-10 runs below use their own recipe, with mini-batch 128.)

On ImageNet, the first comparison uses identity shortcuts and zero-padding, so the residual and plain nets have exactly the same parameters.

ArchitecturePlain top-1 errorResNet top-1 error
18 layers27.94%27.88%
34 layers28.54%25.03%

The plain 34-layer net is worse than the plain 18-layer net, and its training error is higher throughout training. The 34-layer ResNet is better than the 18-layer ResNet, and its training error is lower. The 18-layer ResNet also converges faster than its plain counterpart, which the paper reads as the shortcut easing early optimisation even when the net is not overly deep.

Deeper bottleneck models keep improving. On ImageNet validation, top-5 error falls from 7.76% for ResNet-34 (option A) to 6.71% for ResNet-50, 6.05% for ResNet-101 and 5.71% for ResNet-152 (10-crop testing, Table 3). The paper's text quotes a lower single-model figure for ResNet-152, 4.49% top-5 validation error, from Table 5. The two numbers are both correct for the same model: Table 3 uses 10-crop testing, while the 4.49% uses the fully-convolutional, multi-scale testing the paper adopts for its best results. A six-model ensemble, two of them 152-layer, reaches 3.57% top-5 error on the ImageNet test set, which won first place in ILSVRC 2015 classification. On CIFAR-10 the paper trains 20 to 110-layer networks with a 1202-layer one as a stress test. The 110-layer ResNet reaches 6.43% test error (its best of five runs; the mean is 6.61%). The 1202-layer model reaches under 0.1% training error but 7.93% test error, which the authors attribute to overfitting a small dataset.

Beyond classification, swapping VGG-16 for ResNet-101 in Faster R-CNN raises COCO mAP@[.5, .95] by 6.0 points, a 28% relative gain, which the paper credits entirely to the learned features.

Why it mattered

The residual block became the default building block of deep vision networks, and the paper's main claim that depth helps once optimisation is fixed was confirmed across many tasks. It also set a pattern of using identity paths to keep gradients and information flowing, which later appeared in transformer blocks, where each sublayer is wrapped in a residual connection with layer normalisation.

Follow-up work refined the design. He et al. (2016) analysed the block as a pre-activation design, with normalisation and ReLU placed before each convolution, and showed that this makes the identity path cleaner still. Later work on stochastic depth and wider residual networks questioned how much depth is needed. The core idea, a parameter-free identity path, remained.

Reading guide

Read the paper in this order. Each section relies on the listed prerequisites.

  1. Section 1 (introduction) and Figure 1. Needs Vanishing Gradient Problem to see why the problem is not simply vanishing gradients, and Convolutional Neural Network for the setting.
  2. Section 2 (related work). Needs Residual Connection to place the idea against highway networks and earlier shortcuts.
  3. Section 3.1 (residual learning). Needs Residual Connection and Feedforward Neural Network. This is the core argument.
  4. Section 3.2 (identity shortcuts) and Figure 2. Needs Rectified Linear Unit (ReLU) for the activation and Stochastic Gradient Descent for training.
  5. Section 3.3 (architectures) and Table 1. Needs Convolutional Neural Network for the convolution sizes and strides. Work through the FLOP counts with the bottleneck example above.
  6. Section 3.4 and Section 4.1 (ImageNet results). Needs Stochastic Gradient Descent, Momentum and L2 Regularization for the training recipe, and Softmax for the output.
  7. Section 4.2 (CIFAR-10 and the 1202-layer analysis). Read after the ImageNet results, since it reuses the same ideas on a smaller dataset.
  8. Section 4.3 and the appendices. Detection and localisation build on the same backbone. Read them last.

Initialisation He (Kaiming) Initialization and normalisation Batch Normalization are used throughout the experiments. Read those lessons before Section 3.4.

Check yourself

QuestionIn the residual formulation, what does the block learn, and what does the shortcut add?Show answer

The stacked layers learn the residual F(x)=H(x)−x\mathcal{F}(\mathbf{x}) = \mathcal{H}(\mathbf{x}) - \mathbf{x}, the difference between the desired mapping and the input. The shortcut adds the input x\mathbf{x} back unchanged, so the block output is F(x)+x\mathcal{F}(\mathbf{x}) + \mathbf{x}. If the ideal block is the identity, the layers only need to push F\mathcal{F} towards zero.

QuestionA residual block has output y = F(x) + x, and F(2) = 0.1. What is y, and what is dy/dx when F has zero derivative at x = 2?Show answer

y=0.1+2=2.1y = 0.1 + 2 = 2.1. With dFdx=0\frac{d\mathcal{F}}{dx} = 0, dydx=1+0=1\frac{dy}{dx} = 1 + 0 = 1. The gradient passes through the block unchanged.

QuestionWhy does the paper argue that the plain 34-layer network's failure is not caused by vanishing gradients?Show answer

The plain networks use batch normalisation, which keeps the forward signals at non-zero variance and the backward gradients at healthy norms. The 34-layer plain net still reaches competitive accuracy, so the solver is working to some extent. The higher training error is therefore an optimisation difficulty, which the authors conjecture is an exponentially low convergence rate, rather than a lack of gradient.

QuestionWhy do the paper's identity shortcuts keep the bottleneck blocks cheap, and what does option B add?Show answer

An identity shortcut has no weights and costs one element-wise addition. In a bottleneck block the shortcut connects the two wide ends of the block, so a projection there would roughly double the time complexity and model size. Option B adds a projection only where dimensions change, and the paper finds it only slightly better than option A, so identity shortcuts are used everywhere else.

QuestionA deeper plain net has higher training error than a shallower one. Explain why this points to an optimisation problem rather than a capacity problem.Show answer

A shallower model can be extended with identity layers to give a deeper model that achieves the same training error, so the deeper model has enough capacity. Since the solver still fails to reach that error, the difficulty lies in finding the solution, not in representing it.

Sources

Read the full paper

Kaiming He, Xiangyu Zhang, Shaoqing Ren et al., 2015. arXiv:1512.03385