Prerequisites
Before diving into CNNs, you should be comfortable with:
- Neural Network Fundamentals: Forward/backward pass, activation functions, gradient descent (see Tutorial 21)
- Linear Algebra: 2D/3D matrix operations, convolution as matrix multiplication
- Image Representation: RGB channels, pixel values (0-255), spatial dimensions (H x W x C)
- PyTorch Basics: Tensors, nn.Module, DataLoader, GPU acceleration
Deep Learning
Convolutional Neural Networks — How Computers See Images
Master CNNs and learn how computers extract visual features through convolution, pooling, and learned filters.
- Convolution operations — detect edges, textures, and shapes
- Pooling layers — reduce spatial dimensions efficiently
- Modern architectures — ResNet, EfficientNet, and beyond
A picture is worth a thousand words — and a CNN learns all of them.
Learning Objectives
By the end of this tutorial, you will be able to:
- Compute convolution operations and understand how kernels extract visual features
- Calculate output dimensions given input size, kernel size, stride, and padding
- Compare max pooling, average pooling, and global average pooling
- Explain how ResNet skip connections solve the vanishing gradient problem
- Perform transfer learning with pre-trained CNN models
- Implement a complete CNN in PyTorch for image classification
- Understand the feature hierarchy from edges to objects
Convolutional Neural Networks — Complete Guide
CNNs exploit the spatial structure of images through two key principles: local connectivity (each neuron connects to a small region) and weight sharing (same filter applied everywhere). This yields parameters instead of for fully connected layers.
Convolution Operation
The discrete 2D convolution (cross-correlation in practice) slides a learnable kernel over the input:
Pooling
Pooling reduces spatial dimensions, providing translation invariance and reducing computation.
ResNet and Skip Connections
The residual connection addresses the degradation problem. Instead of learning directly, learn the residual :
PyTorch Implementation
CNN Architecture Comparison
Real-World Applications
1. Medical Imaging Diagnosis CNNs detect tumors, diabetic retinopathy, and pneumonia from X-rays and MRIs with radiologist-level accuracy. U-Net architecture enables precise tumor segmentation for radiation therapy planning.
2. Autonomous Driving CNNs process camera feeds to detect lanes, pedestrians, traffic signs, and obstacles in real-time. Tesla's vision system processes 8 camera streams simultaneously at 36 FPS.
3. Face Recognition Facial recognition systems use deep CNNs (FaceNet, ArcFace) for identity verification. Accuracy exceeds 99.8% on benchmark datasets. Applications include phone unlock and security systems.
4. Satellite Imagery Analysis CNNs analyze satellite images for urban planning, deforestation monitoring, crop health assessment, and disaster response. Models process multi-spectral imagery with up to 13 channels.
5. Quality Control in Manufacturing Visual inspection CNNs detect defects in products on assembly lines at superhuman speed. Companies report 90%+ reduction in defective products reaching customers.
6. Content Moderation CNNs automatically detect inappropriate content, spam, and policy violations across social media platforms, processing billions of images daily with <100ms latency per image.
Common Mistakes & How to Avoid Them
Key Formulas Reference
| Formula | Expression | Use Case |
|---|---|---|
| Output Size | floor((n - k + 2p) / s) + 1 | Conv/Pool output dims |
| Conv Parameters | C_out x (C_in x k x k + 1) | Memory/compute budget |
| Receptive Field | rf = rf + (k-1) x prod(strides) | Effective coverage |
| Residual Block | y = F(x) + x | Deep network training |
| GAP Output | 1/C x sum over HxW | Replace FC layers |
| FLOPs (Conv) | 2 x C_out x C_in x k x k x H x W | Compute estimation |
Interview Questions
Practice Exercise
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# Task 1: Implement a CNN with these specifications:
# - Conv layer 1: 3 -> 32 channels, 3x3 kernel, same padding
# - Conv layer 2: 32 -> 64 channels, 3x3 kernel, same padding
# - Conv layer 3: 64 -> 128 channels, 3x3 kernel, same padding
# - Each conv block: Conv -> BatchNorm -> ReLU -> MaxPool(2)
# - Classifier: GAP -> Linear(128, 64) -> ReLU -> Dropout(0.3) -> Linear(64, 10)
class YourCNN(nn.Module):
def __init__(self):
super().__init__()
# Your implementation here
pass
def forward(self, x):
# Your implementation here
pass
# Task 2: Train for 50 epochs with:
# - Adam optimizer, lr=1e-3
# - CosineAnnealingLR scheduler
# - Data augmentation: RandomCrop(32, padding=4), RandomHorizontalFlip()
# Task 3: Achieve >85% test accuracy on CIFAR-10
# Bonus: Add a residual connection between conv blocks 2 and 3
Success criteria: >85% accuracy on CIFAR-10 test set. Visualize training/validation curves to detect overfitting.
Key Takeaways
What to Learn Next
-> Vision Transformers Apply Transformer architecture to vision tasks.
-> Transfer Learning Leverage pre-trained models for new tasks.
-> Object Detection Find and locate objects in images.
-> Neural Networks Understand the foundation of deep learning.
-> Semantic Segmentation Classify every pixel in an image.
-> Training Deep Networks Master optimizers, batch norm, and regularization.
Advanced Topics
Depthwise Separable Convolutions
Standard convolutions compute across all spatial and channel dimensions simultaneously. Depthwise separable convolutions factorize this into two steps:
- Depthwise convolution: One filter per input channel (spatial filtering only)
- Pointwise convolution: 1x1 conv to combine channels
Parameter reduction: From to . For : standard = 1,179,648 params vs separable = 144,128 params (~8x reduction).
Receptive Field Analysis
The receptive field is the region of the input that influences a particular output neuron. Understanding receptive fields is crucial for designing architectures:
- Single 3x3 conv: receptive field = 3x3
- Two stacked 3x3 convs: receptive field = 5x5
- Three stacked 3x3 convs: receptive field = 7x7 (same as one 7x7 with fewer params)
Formula:
where is kernel size and is stride at layer .
Feature Visualization
Understanding what CNNs learn is crucial for debugging and trust:
- Layer 1: Gabor-like edge detectors, color blobs
- Layer 2: Corners, textures, simple patterns
- Layer 3: Object parts (eyes, wheels, leaves)
- Layer 4: Whole objects (faces, cars, animals)
- Layer 5: Scene-level concepts (faces, specific objects)
Techniques: Deconvolution (Zeiler & FERGUS, 2014), Grad-CAM (visualizing which regions drive predictions), feature inversion (reconstructing inputs from activations).
Comparison Table
| Method | Parameters | Accuracy (ImageNet) | Speed | Best For |
|---|---|---|---|---|
| VGG-16 | 138M | 71.5% | Slow | Feature extraction, transfer learning |
| ResNet-50 | 25.6M | 76.1% | Medium | General purpose, production |
| EfficientNet-B0 | 5.3M | 77.1% | Fast | Mobile, edge deployment |
| ViT-Base | 86M | 77.9% | Fast (GPU) | Large datasets, pre-training |
| ConvNeXt-Tiny | 28.6M | 82.1% | Fast | Modern CNN alternative |
Further Reading
- LeCun, Y. et al. (1998). "Gradient-based learning applied to document recognition." — The original LeNet paper that started CNNs.
- He, K. et al. (2016). "Deep Residual Learning for Image Recognition." — The ResNet paper that enabled training 152+ layer networks.
- Simonyan, K. & Zisserman, A. (2014). "Very Deep Convolutional Networks for Large-Scale Image Recognition." — VGGNet, establishing 3x3 as the standard kernel size.
- Howard, A. et al. (2017). "MobileNets: Efficient CNNs for Mobile Vision Applications." — Depthwise separable convolutions for edge deployment.
- Tan, M. & Le, Q. (2019). "EfficientNet: Rethinking Model Scaling for CNNs." — Compound scaling strategy.
- CS231n: Convolutional Networks for Visual Recognition (Stanford) — The definitive free course on CNNs and computer vision.
Quick Reference Cheat Sheet
| Operation | PyTorch | Output Shape |
|---|---|---|
| Conv2d(in, out, k, s, p) | nn.Conv2d() | (B, out, floor((H-k+2p)/s)+1, ...) |
| MaxPool2d(k, s) | nn.MaxPool2d() | (B, C, floor((H-k)/s)+1, ...) |
| AvgPool2d(k, s) | nn.AvgPool2d() | (B, C, floor((H-k)/s)+1, ...) |
| AdaptiveAvgPool2d(out) | nn.AdaptiveAvgPool2d() | (B, C, out_h, out_w) |
| BatchNorm2d(channels) | nn.BatchNorm2d() | (B, C, H, W) — no change |
| Flatten() | nn.Flatten() | (B, CHW) |