Convolutional Neural Networks (CNNs)
Use convolutions, pooling, and hierarchical features for vision tasks.
In plain words
A CNN looks at a picture through a tiny window, sliding it everywhere to spot simple shapes, then combines those shapes into bigger ideas such as eyes, wheels and finally whole objects.
Think of it like…
A stamp pressed across a page. The same small stamp (the filter) is moved over every position and records how well the page matches it; many different stamps (edges, curves, textures) produce many maps, and later stamps look at the maps from earlier ones.
Where you’ve seen it
A defect-inspection model classifies 224×224 RGB photos of metal parts. The first layer uses 64 filters of size 3×3×3, so it has 3 × 3 × 3 × 64 + 64 = 1,792 parameters; a fully connected layer from the 150,528 input values to just 1,000 units would have 150,528 × 1,000 + 1,000 = 150,529,000. Output size follows (W − K + 2P) / S + 1: a 3×3 convolution with padding 1 and stride 1 keeps 224 → 224, while the 7×7, stride 2, padding 3 stem used by ResNet gives (224 − 7 + 6) / 2 + 1 = 112. Max pooling 2×2 with stride 2 halves 112 to 56. A tiny hand example shows the filter idea: the edge kernel (−1, 0, 1) slid over the row (10, 10, 10, 50, 50, 50) outputs (0, 40, 40, 0), firing only where the brightness jumps. Stacking three 3×3 convolutions gives a 7×7 receptive field with 3 × 9 × C² = 27C² weights, against 49C² for a single 7×7 layer; for C = 64 that is 110,592 versus 200,704. The team adds random flips, crops and brightness jitter, keeps rare defect classes with class weights and starts from an ImageNet-pretrained backbone rather than training from scratch.
Words you’ll hear
- Filter / kernel
- A small grid of learned weights slid across the input to produce a feature map.
- Stride and padding
- Step size of the filter, and zeros added around the border to control output size.
- Receptive field
- The region of the input that can influence one output unit.
- Pooling
- Downsampling that summarises each window, for example by its maximum.
Visual diagram
Theory & deep dive
Convolutions, sizes and parameters
A convolution layer with input channels C_in, output channels C_out and kernel K × K has C_out × (K × K × C_in) weights plus C_out biases. For RGB input with 64 filters of 3×3: 64 × 27 + 64 = 1,792. The output spatial size is floor((W − K + 2P) / S) + 1. With W = 224, K = 3, P = 1, S = 1 the size stays 224; with K = 7, S = 2, P = 3 it is floor(223 / 2) + 1 = 112. The same filter is applied everywhere, so a pattern detected in one corner is detected in another, and the number of parameters does not depend on the image size.
A worked 1D example: kernel (−1, 0, 1) over the row (10, 10, 10, 50, 50, 50) computes each window's right value minus left value. The four windows give 0, 40, 40 and 0, so the output (6 − 3 + 1 = 4 values) lights up exactly at the edge. Learned filters in early layers resemble such edge and colour detectors. Remember that deep-learning libraries compute cross-correlation (they do not flip the kernel), which makes no difference because the kernel is learned.
Conv 3×3, 3→64 channels: 1,792 parameters, applied at all 224 × 224 positions. Dense 150,528 → 1,000: 150,529,000 parameters, about 84,000 times larger, and tied to one image size.
Pooling, depth and the receptive field
Max pooling with a 2×2 window and stride 2 keeps the largest value in each block and halves each spatial dimension: for the grid [[1, 3, 2, 1], [4, 6, 5, 0], [7, 2, 1, 8], [3, 9, 4, 2]] the pooled result is [[6, 5], [9, 8]]. It provides small translation invariance and reduces compute. Many modern networks use strided convolutions instead, and finish with global average pooling, which averages each channel map into one number so the classifier head is small and works for any input size.
The receptive field grows with depth: two stacked 3×3 layers see 5×5 and three see 7×7. Stacking small filters is preferred because it reaches the same field with fewer parameters (27C² against 49C² for one 7×7 layer, 110,592 against 200,704 at C = 64) and adds more nonlinearities. Practical design: Conv → BatchNorm → ReLU blocks, doubling channels when halving resolution, residual connections for deep nets (ResNet) and augmentation to fight overfitting. CNNs are not rotation or scale invariant by design, so training data should cover the variation you expect.
Input rows: [1, 3, 2, 1], [4, 6, 5, 0], [7, 2, 1, 8], [3, 9, 4, 2]. Top-left block (1, 3, 4, 6) → 6; top-right (2, 1, 5, 0) → 5; bottom-left (7, 2, 3, 9) → 9; bottom-right (1, 8, 4, 2) → 8. Output [[6, 5], [9, 8]].
How it works, step by step
Step 1 of 41. Prepare images
Resize, normalise channel statistics and add augmentations for training only.
- Augment
- train only
When to use it & trade-offs
What it is
A convolutional neural network (CNN) processes grid-like data such as images by sliding small learned filters (kernels) across the input. Each filter shares its weights at every position, so it detects the same pattern (an edge, a texture, a corner) wherever it appears, which gives translation equivariance and far fewer parameters than a fully connected layer. Pooling (or strided convolution) downsamples the feature maps, and stacking layers builds a hierarchy: edges become textures, textures become parts and parts become objects. A typical classifier is a sequence of Conv → BatchNorm → ReLU blocks, global average pooling and a linear head.
Why it matters: Images have local structure and the same pattern means the same thing anywhere. A fully connected layer on a 224×224 RGB image would need 150,528 weights per hidden unit; a 3×3 convolution needs 27 per filter. CNNs therefore learn faster, need less data and generalise better on vision tasks, and the same ideas apply to audio spectrograms, 1D sensor signals and text. Being able to compute output sizes, parameter counts and receptive fields is a standard interview skill and the foundation for transfer learning and detection models.
When to reach for it
- Image classification, quality inspection and medical imaging
- 1D CNNs on sensor and audio signals
- Feature extractors inside detection and segmentation models
When to avoid or be careful
- Dense layers directly on raw pixels
- Training deep CNNs from scratch on a few hundred images
- Assuming rotation or scale invariance without augmentation
Layer choices for images
Strengths: Weight sharing; translation equivariance.
Watch out: Limited global context per layer.
Strengths: Flexible mixing of all inputs.
Watch out: Parameter count explodes with image size.
Strengths: Global attention.
Watch out: Needs more data or pretraining; costlier.
Interview signals & numbers to know
When this topic comes up
- “Why do CNNs use fewer parameters than dense layers?”
- Output size formula
- Receptive field
- Pooling versus strided convolution
- Why 3×3 stacks beat one 7×7
Benchmarks & rules of thumb
- (W − K + 2P) / S + 1
- convolution output size
- 1,792
- parameters in a 3×3 conv from 3 to 64 channels
- 27C² vs 49C²
- three 3×3 layers vs one 7×7 layer
Common mistakes & follow-ups
Common interview mistakes
- Getting padding wrong and silently shrinking feature maps.
- Applying augmentation to the validation set.
- Evaluating only overall accuracy on a heavily imbalanced defect set.
Likely interviewer follow-ups
- How many parameters does this conv layer have?
- What is the receptive field after three 3×3 layers?
- When would you choose a transformer over a CNN?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Convolutional Neural Networks (CNNs) with data science interview Q&A and hands-on practice.