Features, shapes and shortcut connections

ResNet: Inside the Residual Block

Open the stages of ResNet-18, 34 and 50. Follow tensor shapes, compare shortcuts and make the residual addition visible.

12 min to exploreMachine Learning · Computer Vision · Computer Vision with PyTorch
01 / UNDERSTAND

What does a shortcut actually add to a deep network?

A residual branch computes F(x), while the shortcut supplies x or a learned projection. Matching shapes permit elementwise addition. Torchvision’s ResNet blocks apply ReLU after the addition, and stages progressively change channel counts and spatial dimensions.

02 / EXPLORE

Make a prediction.
Then move a control.

Inside the architecture

Follow the features.
Find the shortcut.

Travel from an RGB image to a prediction, then open a residual block to see its two paths.

ResNet · 18 / 34 / 50
ArchitectureResNet-188 residual blocks
Total parameters11,689,512Original 1,000-class ImageNet head
Final feature map512 × 7 × 7Before global average pooling
Input → stem3 × 224 × 224 → 64 × 56 × 56
Pool → head512 × 7 × 7 → 512 → 1,000 logits

Stage 1 · Block 1

Select a block. First blocks may change channels or spatial size.

Identity shortcut
Selected block · learned parameters
Main convolutionsMain BatchNormProjection + BNTotal

A tiny numerical example

One scalar, an identity shortcut and ReLU. This illustrates the addition; it does not simulate convolution.

Why the depth is called 18, 34 or 50

The conventional depth counts the stem convolution, the main-branch convolutions in each residual block, and the final fully connected layer. BatchNorm, activation, pooling and projection-shortcut layers are not included in that naming convention.

Residual connections make identity-like mappings easier to represent and can improve optimization of deep networks. They do not guarantee nonzero gradients or better results on every dataset.

Shapes and parameter counts follow torchvision. The scalar example is a separate teaching illustration.Choose what to fine-tune →

Shapes and parameter counts follow torchvision. The scalar addition is an explicitly simplified teaching example.

03 / SEE THE CODE

The idea,
in Python.

A compact implementation of the core concept. Open the code when you are ready to connect the visual to the algorithm.

Open the core implementation Python ↗
Standalone example · illustrative valuesDownload .py
import torch
from torchvision import models

# weights=None: inspect the architecture without downloading weights.
model = models.resnet18(weights=None).eval()
x = torch.randn(1, 3, 224, 224)

def inspect(name):
    def hook(module, inputs, output):
        print(name, tuple(output.shape))
    return hook

handles = [getattr(model, name).register_forward_hook(inspect(name))
           for name in ["maxpool", "layer1", "layer2", "layer3", "layer4", "avgpool", "fc"]]
with torch.inference_mode():
    logits = model(x)
for handle in handles:
    handle.remove()
print("Total parameters:", sum(p.numel() for p in model.parameters()))
# A basic identity block returns ReLU(F(x) + x).
# A shape-changing block uses ReLU(F(x) + projection(x)).
# For ResNet-50, torchvision uses bottlenecks (1x1, 3x3, 1x1).
04 / TRY IT

Explain the change.
Test your reasoning.

  1. Find an identity shortcut and a projection shortcut. Explain why only one needs learned parameters.
  2. Compare ResNet-18 with ResNet-50. Where do bottlenecks change the structure?
  3. Double the input width and height. What changes in the feature maps, and why does the parameter count stay the same?
← All visual labsNext: Data Augmentation: Build the Variations

Find your next idea.

Search across courses, research, projects, and resources.

Ideas, connected.esc to close