Convolutional Neural Networks (CNNs)
Part of the Machine Learning Roadmap
In short
A small 224×224 colour photo has about 1.5 lakh pixel values. Connect each one to every neuron and the network becomes huge and slow. CNNs scan the image with tiny shared filters instead, and that one idea made modern computer vision possible.
CNNs slide small learned filters across an image to detect edges, textures and shapes, then stack layers to recognise whole objects. Sharing filters across the image makes them far more efficient for pictures than fully connected networks.

Think of it like this
Checking a saree with a magnifying glass
A weaver checking a new saree doesn't stare at the whole thing at once. She slides a small magnifying glass across it, looking for the same few things everywhere: a broken thread, a border pattern, a motif. Early filters spot edges, the next ones spot patterns made of edges, and later ones recognise whole motifs. The same glass works anywhere on the cloth.
Watch it happen
A small CNN classifying a 32×32 photo
1/6A colour image: 3 channels (red, green, blue), each 32 by 32 pixels.
The big ideas
What you need to know
Filters are learned
A filter is a small grid of weights, like 3×3. Training discovers which patterns are useful to detect.
Weight sharing
The same filter is reused at every position, so a cat's ear is detected wherever it appears and the parameter count stays small.
Pooling shrinks
Pooling and strides reduce the spatial size, making later layers see bigger areas and run faster.
Simple to complex
Early layers find edges and colours, middle layers textures and parts, deep layers whole objects.
See it in code
Walk through the code
1import torch2import torch.nn as nn3 4class SmallCNN(nn.Module):5 def __init__(self, num_classes=10):6 super().__init__()7 self.features = nn.Sequential(8 nn.Conv2d(3, 16, kernel_size=3, padding=1), nn.ReLU(),9 nn.MaxPool2d(2), # 32x32 -> 16x1610 nn.Conv2d(16, 32, kernel_size=3, padding=1), nn.ReLU(),11 nn.MaxPool2d(2), # 16x16 -> 8x812 )13 self.classifier = nn.Sequential(14 nn.Flatten(),15 nn.Linear(32 * 8 * 8, 128), nn.ReLU(),16 nn.Linear(128, num_classes),17 )18 19 def forward(self, x):20 return self.classifier(self.features(x))21 22model = SmallCNN()23x = torch.randn(8, 3, 32, 32) # a batch of 8 colour images24print(model(x).shape) # torch.Size([8, 10])25print(sum(p.numel() for p in model.parameters())) # 2686501/5First block: 16 filters of size 3×3 over 3 input channels. padding=1 keeps the size at 32×32 until pooling halves it.
A two-block CNN for 32×32 colour images, like CIFAR-10. Train it with the same loop as any PyTorch model.
Save yourself the pain
Common mistakes
Oops
Feeding images as (batch, height, width, channels).
Do this instead
PyTorch expects (batch, channels, height, width). torchvision's ToTensor() converts images to that layout for you.
Oops
Getting the Linear input size wrong after the conv layers.
Do this instead
Pass a dummy batch through self.features and print its shape, or use nn.LazyLinear to infer it.
Oops
Training a CNN from scratch on 500 photos.
Do this instead
With small data, start from a pretrained model like ResNet18 and fine-tune it. That's transfer learning.
Your mission
Train on CIFAR-10
Quick check
Why do CNNs need far fewer parameters than fully connected networks for images?
Go deeper
- 1Build a small CNN in PyTorch with Conv2d, ReLU and MaxPool2d layers for CIFAR-10.
- 2Work out the output shape after each layer before running the code.
- 3Visualise the learned filters of the first layer.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.