LLM + RoboticsUnit 2 · Deep learning explosion · Day 09

Deep Residual Learning for Image Recognition (ResNet)

Make each block learn a correction to its input, not a whole new function. With that change, a 152-layer network trains more easily than a 34-layer one used to.

AuthorsKaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun (Microsoft Research)
Year / venuearXiv Dec 2015 · CVPR 2016
Core ideay = F(x) + x
Study time3–5 h

Original: arXiv:1512.03385 · Code, notebook and exercises: 30u30 Day 9

Start here (no maths needed)

Very deep networks used to get worse when you added more layers, until ResNet gave every few layers a shortcut so they only had to learn small fixes.

Think of it like this

Imagine passing a drawing down a line of 100 classmates, where each person redraws it from scratch. By the end it looks nothing like the original. Now change the rule: each person gets the drawing itself and only adds small touch-ups. After 100 people the drawing is still recognisable, and better. ResNet uses that second rule.

How it works

  1. The picture goes into a block of layers.
  2. The block works out a small change, a "correction".
  3. A shortcut carries the block's input straight past it, unchanged.
  4. The input and the correction are added together and passed to the next block.
  5. If a block has nothing useful to add, it can learn to add almost nothing. So extra blocks don't make things worse.

Why robots care

Many robots that learn by copying people (for example, learning to fold a towel from demonstrations) use a small ResNet to understand their camera images. The same idea also works for control: keep a robot controller that already works, and only learn a small correction on top of it.

Try it

Words to know

Layer
One step of processing inside a neural network.
Shortcut (skip connection)
A path that carries the input past some layers without changing it.
Residual
The small change a block adds on top of its input.
Deep network
A network with many layers stacked on top of each other.

Going deeper: everything below is the full version, with some maths. On a first read, skim the headings and the "Why it matters for robotics" box, and come back for the rest later.

1. Summary

TL;DR

Stacking more layers onto a plain CNN made it worse, and not because of overfitting. Training error itself went up. In principle, the deeper net could copy the shallow one and set its extra layers to the identity, but SGD failed to find that solution. ResNet adds a shortcut around every few layers, so each block outputs F(x) + x. Doing nothing now just means pushing F towards zero. Residual nets of 50, 101 and 152 layers trained cleanly. An ensemble reached 3.57% top-5 error on the ImageNet test set and won ILSVRC 2015.

2. The core idea

Call the mapping a few layers should learn H(x). Instead of fitting H directly, let the layers fit the residual F(x) = H(x) − x, and add the input back:

y = F(x, {W_i}) + x                 # identity shortcut
y = F(x, {W_i}) + W_s · x           # projection shortcut, only when shapes change
F(x) = W_2 · σ(W_1 · x)             # basic block (BN omitted); output is σ(y)

The whole network is 3×3 convs in four stages (56, 28, 14 and 7 pixels wide). Channels double each time the resolution halves. It ends in global average pooling and a single 1000-way FC layer, with no big hidden FC layers like AlexNet's.

3. Deep dive

Why "learn the correction" is easier

Picture a good 18-layer net and add 16 layers. If the new layers computed the identity, the deeper net would be exactly as good. A stack of conv + ReLU layers with random weights is nowhere near the identity, though. It has to learn to pass its input through unchanged, and the experiments showed SGD doesn't manage it. With a shortcut, the identity is the default, reached when F = 0. Small weights are close to that already. The paper also measured the trained residual branches and found their responses generally small. The blocks really do act as small corrections.

Note what the paper does not claim. Its plain nets used batch norm, and the authors checked that gradients kept healthy norms. They argue that degradation is an optimisation problem, not vanishing gradients. The cleaner gradient story came a year later in Day 10.

Interactive: what a deep stack does to its input at initialisation

A 64-wide toy network with random He-initialised weights, before any training. The plot shows the cosine similarity between the input vector and the signal after each layer. Plain: x ← ReLU(W·x). Residual: x ← x + s·W₂·ReLU(W₁·x). The slider sets s, the scale of the residual branch.

Plain, layer 50
–
Residual, layer 50
–

A random plain layer scrambles its input at once, because a random matrix is nothing like the identity. Similarity falls below 0.2 after one layer. The residual stack drifts gradually instead. At s = 0.1 the similarity is still about 0.6 after 50 layers, and at s = 0 the stack is exactly the identity. Training then only has to learn useful edits to a signal that is already flowing. This is a toy without batch norm, so read it as intuition, not as a reproduction of the paper.

The bottleneck block

For ResNet-50 and deeper, each block is a 1×1 conv that cuts channels (say 256 → 64), a 3×3 conv at the reduced width, and a 1×1 conv back up to 256. The expensive 3×3 runs on a quarter of the channels. Identity shortcuts matter here: a projection on the 256-wide shortcut would roughly double the block's cost.

Limitations, and what came after

4. Why it matters for robotics

5. How to study it

  1. Read the paper (about 1.5 h). Study Figure 1 (degradation), Figure 2 (the block), Table 1 (architectures) and Figure 4 (plain versus residual training curves).
  2. In the 30u30 Day 9 folder, read implementation.py. Find the basic block, the bottleneck block and where the projection shortcut is used. Run train_minimal.py.
  3. Work through notebook.ipynb, then the exercises: build ResNet-18, ablate the skip connections, measure gradient flow, scale depth and try transfer learning.
  4. Key experiment: train a plain and a residual net of the same depth (say 20 and 56 layers on CIFAR-10) and plot training error. You should see the degradation problem appear and disappear.
  5. Stretch: fine-tune a pretrained ResNet-18 on a small set of your own object images and time inference on the hardware you'd put on a robot.

6. Self-check

Why is the degradation problem not overfitting?

Overfitting means low training error and high test error. Here the deeper plain net had higher training error too. The optimiser failed to fit, even though a solution at least as good as the shallower net existed.

What does a residual block output when its weights are all zero?

Its input, up to the final ReLU. F(x) = 0, so y = x. The identity is the default behaviour.

When is a projection shortcut W_s needed?

When the block changes the number of channels or the spatial size, so x and F(x) can't be added directly. The paper uses a 1×1 conv with stride 2 at those points.

Why use 1×1 convs in the bottleneck block?

They shrink the channel count before the costly 3×3 conv and restore it afterwards. Depth grows while compute per block stays about the same.

Did the paper claim residuals fix vanishing gradients?

No. Its plain nets used batch norm and had healthy gradient norms. It frames the problem as optimisation difficulty and the residual form as a better starting point (preconditioning).

7. Further reading