Convolutional Neural Networks (CNNs)
The architecture for vision — leveraging spatial structure through convolutions
Why Not Fully Connected for Images?
A 224x224 RGB image has 150,528 values (50,176 pixels x 3 channels). A fully connected layer to 1000 hidden units needs 150 million weights — just for one layer.
Problems with FC for Images
| Issue | Description |
|---|---|
| Too many parameters | Memory and computation explosion |
| No spatial structure | Treats adjacent pixels same as distant ones |
| Not translation invariant | Cat in corner vs center requires different weights |
| Overfitting | Huge parameter count with limited data |
CNN Advantages
- Parameter sharing: Same filter applied everywhere
- Local connectivity: Each neuron sees only a small region
- Translation equivariance: Features detected regardless of position
The Convolution Operation
A convolution slides a small filter/kernel over the input, computing element-wise products and sums.
Mathematical Definition
For 2D input
Example: 3x3 Filter on 5x5 Input

Input (5x5) Filter (3x3) Output (3x3)
┌────────────────┐ ┌───────┐ ┌──────────┐
│ 1 2 3 4 5 │ │ 1 0 1 │ │ 35 40 45 │
│ 6 7 8 9 10 │ * │ 0 1 0 │ = │ 60 65 70 │
│ 11 12 13 14 15 │ │ 1 0 1 │ │ 85 90 95 │
│ 16 17 18 19 20 │ └───────┘ └──────────┘
│ 21 22 23 24 25 │
└────────────────┘Stride and Padding
Stride: How many pixels to move the filter each step
- Stride 1: Output slightly smaller than input
- Stride 2: Output half the size (used for downsampling)
Padding: Adding zeros around the input
- "Valid": No padding, output shrinks
- "Same": Pad to keep output same size as input
Output size formula:
Key CNN Concepts
Multiple Filters = Multiple Feature Maps
Each filter detects one type of feature. A layer with 64 filters produces 64 feature maps.
Channels/Depth
- Input: 3 channels (RGB)
- After conv layer with 64 filters: 64 channels
- Filter shape: (height, width, input_channels)
- Output shape: (height, width, num_filters)
Receptive Field
The receptive field is the input region that affects a particular output neuron.

- Each conv layer increases receptive field by (kernel_size - 1)
- Deep networks have large receptive fields
- Important for capturing context (e.g., for segmentation)
Pooling Layers
Pooling reduces spatial dimensions while preserving important features.
Max Pooling vs Average Pooling
Input (4x4) Max Pool (2x2) Avg Pool (2x2)
┌───────────────┐ ┌───────┐ ┌───────┐
│ 1 3 2 4 │ │ 7 8 │ │ 4 5 │
│ 5 7 6 8 │ → │ 15 16 │ │ 12 13 │
│ 9 11 10 12 │ └───────┘ └───────┘
│ 13 15 14 16 │
└───────────────┘Max pooling: Takes maximum value — preserves strongest activation Average pooling: Takes mean — smoother downsampling
Why Pool?
- Reduce computation: Smaller feature maps = faster processing
- Translation invariance: Small shifts don't change max
- Larger receptive field: Fewer pixels cover same input region
Modern Alternative: Strided Convolutions
Many modern architectures replace pooling with stride-2 convolutions:
- More learnable (filter weights vs fixed pooling)
- Smoother downsampling
- Used in ResNet, EfficientNet
Classic Architectures
LeNet (1998)
The original CNN for digit recognition:
Input → Conv → Pool → Conv → Pool → FC → FC → OutputAlexNet (2012)
Won ImageNet, sparked deep learning revolution:
- 5 conv layers, 3 FC layers
- ReLU activation (not sigmoid)
- Dropout for regularization
- GPU training
VGG (2014)
Simple principle: stack 3x3 convolutions
Two 3x3 convs have same receptive field as one 5x5
But fewer parameters and more non-linearityResNet (2015)
Introduced skip connections to train very deep networks.
Skip Connections (Residual Blocks)
The key innovation of ResNet:
Instead of learning
Why Skip Connections Work
Gradient flow: Even if
Easier optimization: Learning
Enables depth: ResNet-152 (152 layers) trains successfully; plain networks fail beyond ~20 layers.
Types of Skip Connections
| Type | Formula | Used In |
|---|---|---|
| Residual | ResNet | |
| Dense | DenseNet | |
| Highway | Highway Networks |
Modern Convolution Variants
1x1 Convolutions
A 1x1 conv is a channel mixer — combines channels without spatial processing:
# 256 channels → 64 channels (dimension reduction)
nn.Conv2d(256, 64, kernel_size=1)Uses:
- Reduce channels (bottleneck in ResNet)
- Increase channels cheaply
- Add non-linearity between large convolutions
Depthwise Separable Convolutions
Split convolution into:
- Depthwise: One filter per input channel (spatial only)
- Pointwise: 1x1 conv to mix channels
Efficiency: Much fewer parameters than standard conv Used in: MobileNet, EfficientNet
Dilated/Atrous Convolutions
Insert gaps in the filter to increase receptive field without more parameters:
Standard 3x3: Dilated 3x3 (rate=2):
[X X X] [X . X . X]
[X X X] [. . . . .]
[X X X] [X . X . X]Used in: Semantic segmentation (DeepLab)
CNN for Different Tasks
| Task | Output | Architecture |
|---|---|---|
| Classification | Single label | CNN → Global Pool → FC → Softmax |
| Detection | Boxes + labels | CNN → Region proposals → FC |
| Segmentation | Pixel labels | Encoder → Decoder (U-Net) |
Transfer Learning
Pre-trained CNNs (on ImageNet) transfer well:
- Load pre-trained weights
- Replace final FC layer for new task
- Fine-tune with lower learning rate
Interview Questions
Q1: "Why do we use convolutions instead of fully connected layers for images?"
Three key reasons:
Parameter efficiency: A 3x3 conv has 9 parameters per channel pair. FC connecting 224x224 to 1000 units needs 150M parameters. Convolutions share weights across spatial positions.
Spatial locality: Images have local structure — nearby pixels are related. Convolutions capture this with local receptive fields. FC treats all pixels equally, ignoring spatial relationships.
Translation equivariance: The same filter applied everywhere detects features regardless of position. A cat detector works whether the cat is in the corner or center. FC would need separate weights for each position.
Result: CNNs need fewer parameters, generalize better, and are computationally tractable for high-resolution images.
Q2: "Explain skip connections and why they help."
Skip connections add the input directly to the output:
Why they help:
Gradient highway: The gradient flows directly through the skip:
. Even if (vanishing gradient), the +1 ensures gradient reaches early layers. Easier optimization: The network only needs to learn the residual
. If the identity is optimal, just learn (set weights to zero). This is easier than learning the full transformation. Enables depth: Without skip connections, plain networks beyond ~20 layers degrade (not just overfit — training error increases). With skips, ResNet-152 trains successfully.
Key insight: Skip connections change the problem from "learn y" to "learn how y differs from x."
Q3: "What is the receptive field and why does it matter?"
Receptive field: The region of input that influences a particular output neuron.
How it grows:
- Each conv layer increases by (kernel_size - 1)
- Each pooling/stride-2 layer doubles it
- Dilated convolutions increase it without more parameters
Why it matters:
Context: For classification, the receptive field should cover the whole object. For segmentation, it should capture context around each pixel.
Architecture design: Too small = can't see enough context. Too large = may be wasteful.
Depth requirement: A network needs enough layers for its receptive field to cover relevant input regions.
Example: A 7-layer network with 3x3 convs has receptive field of 15x15. This may be too small for 224x224 ImageNet images — need deeper networks or dilated convolutions.
Key Takeaways
Convolutions exploit spatial structure through local connectivity and weight sharing.
Pooling/striding reduces spatial dimensions while increasing receptive field.
Skip connections enable very deep networks by providing gradient highways.
1x1 convolutions mix channels efficiently — crucial in modern architectures.
Receptive field determines context — must be large enough to capture relevant input regions.
Modern CNNs use depthwise separable convs, efficient blocks, and transfer learning.