A convolutional neural network (CNN) is a neural network designed for images. Instead of connecting every pixel to every neuron, it slides small learned filters across the image to detect local patterns, first edges and textures, then shapes and object parts in deeper layers, and finally combines them to classify or locate objects. Weight sharing and local connections make CNNs efficient and good at recognising a pattern wherever it appears.
Why don't ordinary neural networks work well on images?
A modest 224 × 224 colour image has 224 × 224 × 3 = 150,528 input values. A fully connected layer with just 1,000 neurons would need over 150 million weights for that first layer alone, would overfit easily, and would have to learn "a cat ear in the top left" separately from "a cat ear in the bottom right". CNNs exploit two facts about images: nearby pixels are related (locality), and a useful pattern is useful anywhere (translation invariance).
How does a convolution work?
A filter (kernel) is a small grid of weights, typically 3 × 3. It slides across the image; at each position it multiplies its weights with the pixels underneath and sums them into one number. The grid of all those numbers is a feature map that lights up wherever the image matches the filter. A layer has many filters (say 64), producing 64 feature maps. Crucially, the filter weights are learned by backpropagation; nobody designs an "edge detector", the network discovers that edge detectors are useful.
| Term | Meaning | Effect |
|---|---|---|
| Kernel size | Filter width × height (e.g. 3 × 3) | How large a local pattern each filter sees |
| Stride | How many pixels the filter moves each step | Stride 2 roughly halves the output size |
| Padding | Zeros added around the border | "Same" padding keeps output size equal to input |
| Channels | Depth of input (3 for RGB) and number of filters | More filters = more patterns learned |
| Pooling | Downsampling, usually max over 2 × 2 regions | Smaller maps, some robustness to small shifts |
How many weights does weight sharing save?
A 3 × 3 convolution on a 3-channel input with 64 filters has 3 × 3 × 3 × 64 = 1,728 weights (plus 64 biases), regardless of the image's size. Compare that with the fully connected layer above:
Arithmetic: 150,528 × 1,000 vs 3 × 3 × 3 × 64 (biases excluded). The convolution's bar is far smaller than drawn.
What does a CNN learn at each depth?
Visualisations of trained networks show a consistent hierarchy. Early layers respond to edges, colour blobs and simple textures. Middle layers combine them into corners, curves and repeated patterns such as fur or brickwork. Deep layers respond to object parts (eyes, wheels, text) and whole objects. Each layer's receptive field (the region of the original image it "sees") grows with depth, which is how small 3 × 3 filters end up understanding large structures.
What are the landmark CNN architectures?
| Architecture | Year | Key idea |
|---|---|---|
| LeNet-5 | 1998 | Convolution + pooling for handwritten digits |
| AlexNet | 2012 | Deep CNN on GPUs with ReLU and dropout; won ImageNet by a large margin |
| VGG | 2014 | Very deep stacks of simple 3 × 3 convolutions |
| GoogLeNet / Inception | 2014 | Parallel filters of several sizes in one block |
| ResNet | 2015 | Residual (skip) connections that let networks go 100+ layers deep |
| EfficientNet | 2019 | Balanced scaling of depth, width and resolution |
ResNet's skip connections are worth special attention: by adding a layer's input to its output, they let gradients flow through very deep networks. The same trick is used in every transformer, including the ones inside LLMs.
What tasks do CNNs handle beyond classification?
- Object detection: find and box every object (YOLO, Faster R-CNN), used in traffic monitoring and retail shelves.
- Segmentation: label every pixel (U-Net), used in medical imaging and satellite mapping.
- OCR and document understanding: reading printed and handwritten text, including Indian scripts.
- Image similarity: embeddings for visual search and duplicate detection.
- Audio: CNNs on spectrograms for speech commands and sound classification.
How do you train a CNN well?
- Use transfer learning when data is limited: start from a network pretrained on a large dataset and fine-tune it. See transfer learning explained.
- Augment data: random crops, flips, rotations and colour changes make models robust and reduce overfitting.
- Normalise inputs and use batch normalisation inside the network.
- Watch validation curves: stop or regularise when validation loss starts rising.
- Check errors visually: look at misclassified images; they often reveal label mistakes or data gaps.
How do CNNs compare with vision transformers?
Vision transformers split an image into patches and use attention to relate them, learning global relationships from the start. With very large datasets they can match or beat CNNs; with smaller datasets, CNNs' built-in assumptions (locality, translation invariance) often give better results for less compute. Many modern models mix both ideas, so understanding convolutions remains valuable.
How do you calculate a convolution layer's output size?
For an input of width W, kernel size K, padding P and stride S, the output width is (W − K + 2P) ÷ S + 1. A 32 × 32 image with a 3 × 3 kernel, padding 1 and stride 1 stays 32 × 32; with stride 2 it becomes 16 × 16. A 2 × 2 max-pooling layer with stride 2 halves each dimension again. Working these sizes out by hand before writing code prevents the most common CNN error, a shape mismatch when the convolutional layers hand over to the final fully connected layers.
Where are CNNs used in India?
Image models are already part of everyday Indian services: scanning and verifying identity documents during digital onboarding, reading meters and number plates, sorting produce by quality, detecting crop disease from farmers' phone photos, and assisting radiologists by flagging scans for review. Each of these deals with real-world messiness, such as poor lighting, low-end phone cameras, varied scripts and regional differences, which is why data collection, augmentation and careful evaluation on local data matter as much as the architecture itself.
What mistakes do beginners make with CNNs?
- Training from scratch on tiny datasets instead of fine-tuning a pretrained model.
- Leaking data: near-duplicate images (burst photos, crops of the same scene) in both training and test sets inflate accuracy.
- Mismatched preprocessing: a pretrained model expects the same resizing and normalisation it was trained with.
- Augmentations that change the label: flipping a digit 6 can make it look like a 9, and flipping text breaks it.
- Only reporting accuracy on imbalanced classes, where rare classes matter most.
Most of these show up as a model that scores well in testing and disappoints in real use, so always test on images collected the way real users will take them.
How should you practise?
- Train a small CNN on handwritten digits in PyTorch and inspect the first-layer filters.
- Fine-tune a pretrained ResNet on a small dataset of your own (for example, leaf diseases from a public dataset).
- Compare accuracy with and without augmentation.
- Visualise which image regions drive predictions using Grad-CAM.
CNNs from LeNet to ResNet, alongside RNNs, GANs and transfer learning, are taught in Program Zero's deep learning course, and the phase project is a CNN image classifier. The curriculum shows the full deep learning phase.