Deep Learning Slides 📂 Introduction · 13 of 18 35 min read

Pooling & Spatial Hierarchy in CNNs

After convolution finds features, pooling summarises them — shrinking the map, keeping what matters, and making the network shrug off small shifts. This visual guide covers max vs average pooling, a full worked example, translation invariance, receptive fields, the spatial hierarchy, GAP, and PyTorch code.

Pooling & Spatial Hierarchy in CNNs

After convolution finds features, pooling summarises them — shrinking the map, keeping what matters, and making the network shrug off small shifts. It's how a CNN climbs from pixels to edges to whole objects.
Max & Average Pool Translation Invariance Spatial Hierarchy PyTorch

Press Next → or use ← → arrow keys

Section 01

Why Pooling Exists

Hold a newspaper an inch from your nose and you see individual dots of ink. Step back and those dots become letters; step back further and you see headlines and layout. Nothing important was lost — the meaning got summarised at each distance. Pooling does the same to a feature map: zoom out, keep the gist, drop the clutter.
🎯
Four jobs at once

Pooling shrinks the spatial size, cuts computation, limits parameters downstream, and makes the network robust to small shifts — all with zero learnable parameters.

Section 01

What Pooling Does

Slide a small window across the feature map and replace each window with a single summary number — the maximum, or the average. No weights, no learning, just a fixed reduction.

0Learnable parameters
75%Fewer values with a 2×2 stride-2 pool
1Number per window — max or mean
🔑
Convolution finds, pooling summarises

Convolution is equivariant — shift the input and the feature map shifts with it. Pooling adds invariance: it stops caring exactly where in a small region a feature was found.

Section 02

Max Pooling vs Average Pooling

AspectMax PoolingAverage Pooling
RuleMaximum value in the windowMean of the window
Answers"Did this feature appear here?""How active is this region overall?"
KeepsThe strongest activationThe diffuse, background signal
Best forSharp features: edges, cornersGlobal context, final summaries
Seen inAlexNet, VGG, ResNetGoogLeNet GAP, MobileNet
⚖️
Max dominates the backbone

Inside a CNN's convolutional blocks, max pooling is the default — it preserves the "a feature fired here" signal. Average pooling shines at the very end, where you want a smooth global summary.

Section 02

Worked Example — 2×2 Pool, Stride 2

Each coloured 2×2 window collapses to one number. Max pooling keeps the biggest value in each:

Input 4×4 1 3 2 4 5 6 1 2 3 2 4 7 1 0 6 3 Max pool 2×2 6 4 3 7
WindowValuesMaxAverage
Top-left1, 3, 5, 663.75
Top-right2, 4, 1, 242.25
Bottom-left3, 2, 1, 031.50
Bottom-right4, 7, 6, 375.00
Section 03

Translation Invariance

A CNN should spot a dog whether it sits top-left or bottom-right of the photo. Convolution alone shifts its response with the dog; pooling makes the answer stop moving. If the strongest activation lands anywhere inside a pooling window, max pooling reports the same result.
±1 pxShift-tolerance from one 2×2 pool
±8 pxAfter stacking three pooling layers
0Extra parameters to gain it
🧭
Equivariance in, invariance out

Small, local invariance is exactly what you want early in a network: features can wobble a few pixels without changing the summary. Stack pools and that tolerance compounds.

Section 04

The Spatial Hierarchy — A Pyramid of Meaning

As pooling shrinks the map layer after layer, each neuron sees a bigger slice of the original image — and the features grow from simple to semantic:

224 edges 112 textures 56 corners 28 parts 7 objects
🏔️
Pixels → edges → textures → parts → objects

Early layers detect edges and colour blobs; middle layers find corners and textures; deep layers recognise wheels, faces, and whole objects. Pooling stabilises each step of that climb.

Section 05

Receptive Field — How Much a Neuron Sees

A neuron's receptive field is the patch of the original image it depends on. It grows with every layer — and every pooling step accelerates the growth.

1
Conv 3×3: receptive field = 3×3
2
Another conv 3×3: grows to 5×5
3
MaxPool 2×2, stride 2: doubles the field → 10×10
4
Conv 3×3 → pool: 14×14 → 28×28 — compounding fast
🔭
Pooling is a receptive-field multiplier

For L stacked 3×3 conv layers, RF = 2L + 1. But a single 2×2 stride-2 pool doubles the effective field of everything after it — which is how deep CNNs come to "see" the whole image.

Section 06

The Output-Size Formula

Pooling shrinks the map by a predictable amount:

O = (N − F) / S + 1
N = input size · F = pool window · S = stride · (no padding needed)
InputPoolOutputReduction
224×2242×2, s2112×11275% fewer
56×562×2, s228×2875% fewer
14×142×2, s27×775% fewer
7×77×7 GAP1×198% fewer
📉
The classic ladder

A 2×2 stride-2 pool halves each dimension: 224 → 112 → 56 → 28 → 14 → 7. Five pools take a big image down to a tiny, information-dense grid.

Section 06

Global Average Pooling

At the end of a modern CNN, Global Average Pooling replaces the old flatten-plus-fully-connected block. It averages each entire feature map down to a single number per channel.

1
7×7×512 → 1×1×512: one scalar per channel, summarising the whole image.
2
No fully-connected layer: eliminates millions of parameters.
3
Input-size agnostic: works for any image size — and acts as a strong regulariser.
🏆
The modern standard

ResNet, EfficientNet, and MobileNet all end with GAP. It's lighter, resists overfitting, and feeds a clean vector straight into the classifier.

Section 07

Pooling From Scratch & in PyTorch

The operation is a double loop of "grab a patch, take max or mean" — and one line each in PyTorch:

def pool2d(x, size=2, stride=2, mode='max'):
    H, W = x.shape
    OH = (H - size) // stride + 1
    OW = (W - size) // stride + 1
    out = np.zeros((OH, OW))
    for i in range(OH):
        for j in range(OW):
            patch = x[i*stride:i*stride+size, j*stride:j*stride+size]
            out[i, j] = patch.max() if mode=='max' else patch.mean()
    return out

# PyTorch — the same three operations
max_pool = nn.MaxPool2d(kernel_size=2, stride=2)
avg_pool = nn.AvgPool2d(kernel_size=2, stride=2)
gap      = nn.AdaptiveAvgPool2d((1, 1))   # global average pool
Matches the worked example

Run pool2d on our 4×4 map and it returns [[6, 4], [3, 7]] — exactly the max-pool result from the table.

Section 07

Pooling Inside a Mini-CNN

Conv → ReLU → Pool, repeated, then GAP into a classifier — the standard modern shape:

class MiniCNN(nn.Module):
    def __init__(self, num_classes=10):
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 32, 3, padding=1), nn.ReLU(),
            nn.MaxPool2d(2, 2),                # 28×28 → 14×14
            nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(),
            nn.MaxPool2d(2, 2),                # 14×14 → 7×7
        )
        self.gap  = nn.AdaptiveAvgPool2d((1, 1))  # 7×7 → 1×1
        self.head = nn.Linear(64, num_classes)
    def forward(self, x):
        x = self.features(x)
        x = self.gap(x).flatten(1)          # (B, 64)
        return self.head(x)
🏗️
Watch the shapes shrink

28×28 → 14×14 → 7×7 → 1×1, then a single linear layer to the classes. Each MaxPool halves the spatial size while ReLU and the next conv add richer features.

Section 08

Everything in One Table

ConceptMax PoolAverage PoolGlobal Avg Pool
Operationmax(window)mean(window)mean(whole map)
ParametersZeroZeroZero
PreservesStrongest activationOverall energyChannel global average
InvarianceStrong localModerateFull spatial
Typical useConv blocksIntermediate/finalReplaces flatten + FC
Output size⌊(N−F)/S⌋+1same1×1×C
📌
Three flavours, one idea

All three summarise a region into fewer numbers with zero parameters. Which statistic and which scope you choose is the only decision.

Section 08

5 Golden Rules

  Pooling cheat-sheet
1Pooling has zero learnable parameters — a fixed reduction with negligible cost.
2Max pooling dominates CNN backbones — it keeps the presence signal and gives strong local invariance.
3Every 2×2 stride-2 pool halves the spatial size and doubles the effective receptive field.
4Global Average Pooling replaces fully-connected layers — fewer parameters, input-size agnostic, regularising.
5Spatial hierarchy is the CNN's superpower: pixels → edges → textures → parts → objects.
🚀
Summarise, shrink, generalise

Convolution finds features; pooling distils them and grows the view. Together they turn a grid of pixels into an understanding of what's in the picture.