Skip to main content
Convolutional Neural Networks (CNNs) are the backbone of modern image classification. Unlike fully-connected networks, CNNs exploit spatial structure through weight sharing and local connectivity, making them far more parameter-efficient on image data.

CNN architecture

A typical CNN consists of three stages:

1. Convolutional layers

A convolution slides a small filter (kernel) across the input, computing a dot product at each position: output[i,j]=∑m,ninput[i+m, j+n]⋅kernel[m,n]\text{output}[i,j] = \sum_{m,n} \text{input}[i+m,\, j+n] \cdot \text{kernel}[m,n] Key hyperparameters: kernel size, stride, padding, number of filters (channels).

2. Pooling layers

Pooling reduces spatial dimensions while retaining dominant features. Max pooling selects the largest value in each local window, making representations more robust to small translations.

3. Fully connected layers

After several conv+pool blocks, the feature maps are flattened and passed through standard dense layers to produce class logits.

Training a CNN with PyTorch

1

Prepare data loaders

Use torchvision.datasets and DataLoader to load and batch your images with on-the-fly augmentations.
2

Define the model

Build a custom CNN or load a pretrained architecture from torchvision.models.
3

Set loss and optimizer

Cross-entropy loss is standard for multi-class classification. Adam is a reliable default optimizer.
4

Run the training loop

Iterate over epochs, perform forward and backward passes, and update weights.

Transfer learning with pretrained models

Training from scratch requires large datasets. Transfer learning repurposes a model pretrained on ImageNet (1.2 M images, 1000 classes) by replacing only the final classification head.
Freeze the backbone layers initially (param.requires_grad = False for all but model.fc), train the head for a few epochs, then unfreeze and fine-tune end-to-end with a lower learning rate (e.g., 1e-5).

Common pretrained architectures

Evaluation

After training, evaluate on a held-out test set:

Resources

Exercise E05: CNN Training

Hands-on exercise: train a CNN from scratch in Google Colab.

VisionColab: Image Classification

Collection of CNN examples and notebooks from the course.

Video: CNN Lecture (2021)

Recorded lecture covering CNN architecture and training.

Video: Complementary CNN

Additional video resource on convolutional neural networks.