Walk me through how CNNs work
Written by the mlprep team · Updated
Walk me through how convolutional neural networks work. What does a convolution actually compute, and why does the architecture make sense for images?
A CNN exploits two properties of natural images that a fully connected network ignores: locality (nearby pixels are related) and translation invariance (a cat in the corner looks like a cat in the center).
What a convolution computes#
A convolutional layer applies a small learned filter (say 3×3) to every position in the input. At each position, it computes the dot product between the filter weights and the patch of input underneath it. This produces a single number — how strongly that filter pattern is present at that location.
Apply this sliding across the full input and you get a 2D feature map. Apply K different filters and you get K feature maps stacked — these are the "channels" of the layer's output.
output[i,j,k] = sum over (p,q,c): filter_k[p,q,c] · input[i+p, j+q, c]
Why this makes sense#
Weight sharing: the same filter is used at every spatial position. A filter that detects horizontal edges applies everywhere — you don't need separate detectors for each location like a fully connected layer would. This dramatically reduces parameter count and enforces translation equivariance.
Local connectivity: each output unit only depends on a small patch of the input. This makes sense because nearby pixels form meaningful structures (edges, corners, textures); long-range pixel dependencies are less direct.
The hierarchy of features#
Early layers learn low-level features: edges, colors, gradients. Middle layers combine these into textures and shapes. Later layers represent high-level concepts: eyes, wheels, faces. This hierarchical composition naturally emerges from stacking convolutions — each layer's filters operate on the previous layer's feature maps, building increasingly abstract representations.
Pooling#
Max pooling (take the max in each region) downsamples the feature maps, reducing spatial resolution and providing a degree of translation invariance — a feature detected slightly left or right of center will still produce the same max. It also reduces computation for later layers.
Average pooling (global average pooling in the final layer) collapses each feature map to a single number before classification, reducing parameters vs. a fully connected head.
You mentioned weight sharing enforces translation equivariance — what's the difference between equivariance and invariance, and where does that distinction matter in practice?
Equivariance vs invariance#
Translation equivariance: if you shift the input, the output shifts by the same amount. A convolution is equivariant — if a cat moves 10 pixels right in the image, the feature map activations representing "cat features" also shift 10 pixels right. The response moves with the input.
Translation invariance: the output doesn't change regardless of where the input is. Global pooling (averaging or max-ing over spatial dimensions) achieves this — no matter where a feature appeared in the feature map, the pooled output is the same.
A CNN is equivariant through the convolutional layers and approximately invariant at the pooled output. This is the right design for image classification: you want the intermediate representations to track where things are, but the final class label shouldn't depend on position.
Where the distinction matters#
Object detection: you need equivariance, not invariance. The location of the detection must reflect the location of the object. This is why detection heads operate on spatial feature maps, not pooled ones.
Pose estimation and keypoint detection: strictly need equivariance throughout — you're predicting pixel coordinates.
Rotation and scale: standard CNNs are neither rotation-equivariant nor scale-equivariant — they only share weights across translations. Rotating an image by 90 degrees will activate different filters unless the model has seen that orientation in training. Techniques like data augmentation (train on rotated/scaled images), spatial transformer networks, or equivariant architectures (group CNNs) address this when it matters.
In practice for classification: data augmentation covers most of this, which is why it's so important. For geometric tasks, you need to be more careful.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
CNNs exploit locality and translation structure in images. A convolution applies a learned filter at every spatial position (weight sharing), producing a feature map that tracks where patterns appear. Stacked layers build from low-level edges to high-level concepts. CNNs are translation-equivariant (features track position) and approximately invariant after pooling. Standard CNNs aren't rotation or scale invariant — data augmentation is the practical fix.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How does a 1×1 convolution differ from a standard convolution and when is it useful?
- What is batch normalization doing in a CNN and why does it usually go between the convolution and the activation?
- How would you adapt a classification CNN backbone for a dense prediction task like semantic segmentation?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions