Day 34: Convolutions: kernels, stride, padding, receptive field
Why images need a different kind of layer
A fully-connected layer treats every pixel as independent — it ignores that nearby pixels are related and that a shirt collar looks the same wherever it appears in the frame. Convolutional layers fix both. They slide a small learnable kernel across the image (exactly Stage 0 Day 9's filter2D, but the kernel weights are *learned* by gradient descent), detecting the same local pattern anywhere it occurs. This is the core of every CV model in FitXpert.
The four dials
- Kernel size — the patch each filter looks at (3×3 is the workhorse).
- Stride — how far the kernel jumps each step; stride 2 halves the output resolution (downsampling).
- Padding — adding a border of zeros so edge pixels get fair treatment and output size is controllable.
- Channels — a layer learns *many* kernels; each produces one output channel (feature map) detecting a different pattern.
import torch.nn as nn
# 3 input channels (RGB) -> 32 learned feature maps, 3x3 kernels
conv = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
# input: (batch, 3, 224, 224)
# output: (batch, 32, 224, 224) padding=1 keeps spatial size with a 3x3 kernelReceptive field: how a network sees big things
One 3×3 kernel sees only a 3×3 patch. But stack convolutions and each deeper neuron indirectly sees a larger region of the original image — its receptive field grows. That's how a network built from tiny 3×3 kernels can recognize a whole garment: early layers see edges, deeper layers combine them into shapes, then objects. This hierarchy is what U-Net (Stage 2) and diffusion (Stage 4) build on.
Key terms
- Convolutional layer
- A layer that slides learnable kernels across an image, detecting local patterns regardless of position.
- Stride
- The step size the kernel moves each time; larger strides downsample the output.
- Padding
- A border (usually zeros) added around the input so edges are handled and output size is controlled.
- Receptive field
- The region of the original input that influences a particular neuron; it grows with network depth.
How does a convolutional layer differ from Stage 0's hand-designed Sobel edge-detection kernel?