Convolutional Neural Networks (CNNs)
How CNNs see images: convolutions, filters, pooling, feature maps and the architecture of a classic image classifier.
Why images need a special network
A small 224 × 224 colour image has 150,528 numbers. Connecting every pixel to every neuron in a fully connected layer would need millions of weights for a single layer — and the network would have to relearn "what an edge looks like" separately for every position in the image.
Convolutional Neural Networks (CNNs) solve both problems with two ideas:
- Local connections — each neuron looks at a small patch of the image.
- Weight sharing — the same small filter slides across the whole image, so a pattern learned in one place is recognised everywhere.
The convolution operation
A filter (or kernel) is a small grid of weights, typically 3 × 3. It slides over the image; at each position it multiplies the overlapping pixels by its weights and sums them into one output value. The grid of outputs is a feature map.
Here's a 3 × 3 vertical-edge filter applied to an image that's bright on the left and dark on the right:
import numpy as np
image = np.array([
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
], dtype=float)
kernel = np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]], dtype=float)
def conv2d(img, k):
kh, kw = k.shape
out_h, out_w = img.shape[0] - kh + 1, img.shape[1] - kw + 1
out = np.zeros((out_h, out_w))
for i in range(out_h):
for j in range(out_w):
out[i, j] = np.sum(img[i:i + kh, j:j + kw] * k)
return out
print(conv2d(image, kernel))
Output:
[[ 0. 30. 30. 0.]
[ 0. 30. 30. 0.]
[ 0. 30. 30. 0.]
[ 0. 30. 30. 0.]]
The large values (30) appear exactly where brightness changes — the filter detected the vertical edge. In a real CNN, nobody designs these filters: the network learns thousands of them during training.
Stride, padding and output size
- Stride — how many pixels the filter moves each step. Stride 2 halves the output size.
- Padding — adding zeros around the border so the output keeps the input's size ("same" padding).
Output size for one dimension:
output = (input − kernel + 2 × padding) / stride + 1
For our example: (6 − 3 + 0) / 1 + 1 = 4, which matches the 4 × 4 output.
Channels and multiple filters
Colour images have 3 channels (red, green, blue), so each filter is actually 3 × 3 × 3. A convolutional layer applies many filters — say 64 — producing 64 feature maps stacked as channels for the next layer.
Pooling
Pooling shrinks feature maps and makes the network less sensitive to small shifts:
- Max pooling (2 × 2) keeps the largest value in each 2 × 2 block — halving width and height.
- Global average pooling averages each whole feature map into one number, often just before the final classifier.
A typical CNN architecture
Input image (224×224×3)
→ [Conv 3×3 → ReLU] ×2 → MaxPool edges, colours
→ [Conv 3×3 → ReLU] ×2 → MaxPool textures, simple shapes
→ [Conv 3×3 → ReLU] ×3 → MaxPool parts: eyes, wheels, letters
→ Global average pooling
→ Fully connected layer → Softmax class probabilities: cat 0.92, dog 0.07, …
Early layers learn simple, general features; deeper layers combine them into complex, task-specific ones.
Famous CNN architectures
| Model | Year | Key idea |
|---|---|---|
| LeNet-5 | 1998 | Early CNN for handwritten digits |
| AlexNet | 2012 | GPUs + ReLU + dropout; kicked off the deep learning boom |
| VGG | 2014 | Simple stacks of 3×3 convolutions |
| ResNet | 2015 | Residual (skip) connections made very deep networks trainable |
| EfficientNet | 2019 | Balanced scaling of depth, width and resolution |
Today Vision Transformers (ViTs) compete with and often beat CNNs on large datasets, but CNNs remain fast, efficient and widely deployed — especially on phones and edge devices.
Transfer learning: the practical shortcut
Training a CNN from scratch needs huge datasets. Instead, take a model pre-trained on ImageNet, keep its feature-extracting layers, and retrain only the final layer on your images. A few hundred labelled photos can then be enough for a good classifier — for example, detecting crop diseases from leaf photos.
Common mistakes
- Training from scratch on a small dataset. Use transfer learning.
- No data augmentation. Random flips, crops and colour jitter reduce overfitting dramatically.
- Mismatched preprocessing. Use the same image size and normalisation the pre-trained model expects.
- Ignoring class imbalance. 95% "healthy" images will produce a model that always says "healthy".
Key takeaways
- CNNs use small shared filters that slide over images to detect local patterns.
- Stacked convolutions learn a hierarchy: edges → shapes → objects.
- Pooling and stride reduce size; padding controls output dimensions.
- Transfer learning lets you build accurate image models with little data.