Foundations to Frontier
Tests
E. Deep Learning › E7 CNN Building Blocks

Convolution in CNNs: Padding, Stride and Dilation

A convolutional layer slides a small learned kernel over its input, sharing the same weights at every position. That makes it shift-equivariant and far cheaper in parameters than a fully connected layer. Deep-learning libraries actually compute cross-correlation (no kernel flip); padding controls the border, stride subsamples, and dilation spreads the kernel to see further. This page covers the operation, the output-size formula, parameter counts, and how striding breaks shift equivariance. In the exercises you implement convolution with stride, padding and dilation and check it against the size formula, then measure how much a small CNN's pooled features change under a one-pixel shift: 0.9% without subsampling, 15–34% with stride 2–4, and about half that with blur before subsampling.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A small pattern detector slid across the image

Instead of connecting every pixel to every neuron, a convolutional layer uses a small grid of weights (a kernel, say 3 × 3) and slides it across the image, computing a weighted sum at each position. The same kernel is used everywhere, so a detector for, say, vertical edges works wherever the edge appears. The output is a map showing where that pattern occurs. Padding adds a border so the map keeps its size; stride skips positions to shrink it; dilation spaces out the kernel’s taps to look further.

CNN building blocks
input 9 × 9
→
output 7 × 7
output size = ⌊(n + 2p − d(k − 1) − 1)/s⌋ + 1 = ⌊(9 + 0 − 2 − 1)/1⌋ + 1 = 7
Deep-learning "convolution" slides a small learned kernel over the input (no flip). Stride shrinks the output, padding preserves borders, dilation spreads the kernel to see further without more weights. Pooling summarises windows. Stacking layers grows the receptive field. Depthwise-separable and grouped convolutions cut cost by not connecting every input channel to every output channel. Transposed convolution upsamples, but uneven kernel overlap leaves checkerboard patterns. Squeeze-and-excitation rescales whole channels by gates computed from their global averages; CBAM adds a per-pixel spatial gate. (Here the gates are fixed functions of the averages so you can see their effect; in a network the excitation is learned.)
Why engineers care

Convolutions are the core of image, audio, radar and many time-series networks, and of the vision backbones in robots, drones and self-driving cars. Getting padding, stride and dilation right decides output sizes, compute cost and how well the network handles objects that move slightly.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A convolutional layer uses…
  2. 2. Stride 2…
  3. 3. Dilation lets a 3 × 3 kernel…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Operation (cross-correlation): y[i, j] = Σₐ Σ_b x[i·s + a·d, j·s + b·d]·k[a, b] + bias. Mathematical convolution flips the kernel; since kernels are learned, the flip does not matter.
  • Output size: ⌊(n + 2p − d(k − 1) − 1)/s⌋ + 1. Examples:
    • n = 32, k = 3, p = 1, s = 1 → 32 (“same”);
    • n = 224, k = 7, p = 3, s = 2 → 112;
    • n = 9, k = 3, p = 2, d = 2 → 9.
  • Effective kernel size: d(k − 1) + 1: 5 for a 3 × 3 kernel with d = 2, 9 with d = 4. “Same” padding is d(k − 1)/2.
  • Parameters: C_out·C_in·k² + C_out. A 7 × 7 convolution from 3 to 64 channels has 9472. A fully connected layer from a 224 × 224 × 3 image to the same output size would need billions.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Output size for n = 224, k = 7, p = 3, s = 2:
  2. 2. Effective kernel of 3 × 3 at dilation 2:
  3. 3. Parameters of 3→64 channels, 7 × 7, with bias:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Convolution in code

Exercise 1.

  • The implementation’s output shape matched the formula for every combination of stride, padding and dilation tried; for a 9 × 11 input and a 3 × 3 kernel, stride 2 with padding 1 gave 5 × 6.
  • An impulse convolved (in the deep-learning sense) gave the flipped kernel, and flipping the kernel first gave the textbook convolution.

Practice.

  • nn.Conv2d(C_in, C_out, k, stride, padding, dilation, groups, bias); use padding="same" for stride 1.
  • Prefer small kernels stacked (two 3 × 3 see as far as one 5 × 5 with fewer parameters).
  • Use stride 2 or pooling to downsample; consider blur pooling where small shifts matter.
  • Use dilation to enlarge receptive fields without losing resolution (segmentation, audio), but beware gridding artefacts from large dilations.
conv = torch.nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1)   # 56x56 -> 28x28
🐍 Exercise 1: convolution with stride, padding and dilation
Implement a 2-D (cross-correlation) convolution with stride, padding and dilation, check the output-size formula, and compare with the flipped-kernel convolution.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Two stacked 3 × 3 convolutions see as far as…
  2. 2. Large dilations can cause…
  3. 3. padding="same" in PyTorch works for…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Equivariance. Convolution commutes with shifts: shifting the input shifts the output by the same amount (exactly, apart from borders). Combined with weight sharing, this is the inductive bias that lets CNNs recognise patterns anywhere with few parameters.

Striding breaks it. With stride s, the output grid only exists at multiples of s, so a one-pixel input shift cannot be represented as a shift of the output. The sampling theorem explains why: subsampling without low-pass filtering aliases high frequencies, so small shifts change the output a lot (Azulay and Weiss, 2019). Zhang (2019) showed that blurring before subsampling (“anti-aliased CNNs”) restores much of the consistency and can improve accuracy.

Convolution as a structured matrix. A convolutional layer is a linear map whose matrix is (doubly) block Toeplitz: sparse (local connections) with tied weights (sharing). This is why it is so much cheaper than a dense layer.

Frequency view. By the convolution theorem, convolution multiplies the Fourier transforms of signal and kernel, which is how some large-kernel layers are computed efficiently.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Without striding, convolution is shift…
  2. 2. Striding without blurring causes…
  3. 3. A convolutional layer's matrix is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How much does a one-pixel shift change a CNN?

History.

  • K. Fukushima (1980) introduced the neocognitron; Y. LeCun et al. (1989) trained convolutional networks by backpropagation.
  • F. Yu and V. Koltun (2016) used dilated convolutions for segmentation.
  • A. Azulay and Y. Weiss (2019) showed CNNs are surprisingly sensitive to small shifts; R. Zhang (2019) proposed anti-aliased CNNs.

Exercise 2. Two layers of random 3 × 3 convolutions with ReLU, each optionally followed by subsampling (with or without a [1, 2, 1] blur first), then global max pooling. The relative change of the pooled features when a smooth random image is shifted by one pixel, averaged over 20 images:

subsampling stride blur before subsampling relative change
1 no 0.009
2 no 0.147
2 yes 0.076
4 no 0.342
4 yes 0.142
  • Without subsampling the features barely changed: convolution is shift-equivariant and global pooling makes it invariant.
  • Subsampling broke this, more with a larger stride.
  • Blurring before subsampling halved the change or better, as the sampling theorem predicts; it does not remove it completely.
🐍 Exercise 2 (research): how much does a one-pixel shift change a CNN?
Measure how much globally pooled CNN features change under a one-pixel shift with different subsampling strides, with and without blur.

Convolutions in AI, robotics and aerospace

  • Perception backbones for detection, segmentation and depth estimation on robots and vehicles.
  • Radar, sonar and vibration processing with 1-D and 2-D convolutions.
  • Robust tracking: shift-consistent features reduce flicker in detections between consecutive video frames.

References

  1. Y. LeCun, B. Boser, J. S. Denker et al. (1989). “Backpropagation applied to handwritten zip code recognition.” Neural Computation 1(4).
  2. V. Dumoulin, F. Visin (2016). “A guide to convolution arithmetic for deep learning.” arXiv:1603.07285.
  3. F. Yu, V. Koltun (2016). “Multi-scale context aggregation by dilated convolutions.” Proc. ICLR.
  4. R. Zhang (2019). “Making convolutional networks shift-invariant again.” Proc. ICML.
  5. A. Azulay, Y. Weiss (2019). “Why do deep convolutional networks generalize so poorly to small image transformations?” JMLR 20.