CNN Fundamentals: Convolution, Pooling, and Architecture Design
942 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# CNN Fundamentals: Convolution, Pooling, and Architecture Design ## 🎯 Learning Objectives - Explain the convolution operation and its mathematical formulation - Compute output dimensions given input size, kernel size, stride, and padding - Understand pooling and its role in spatial dimension reduction - Calculate...

CNN Fundamentals: Convolution, Pooling, and Architecture Design
🎯 Learning Objectives
- Explain the convolution operation and its mathematical formulation
- Compute output dimensions given input size, kernel size, stride, and padding
- Understand pooling and its role in spatial dimension reduction
- Calculate parameter counts for any convolutional layer
- Design a CNN architecture for a given task
📋 Prerequisites
- Linear algebra (matrix operations)
- Basic neural network concepts
- Image representation (channels, height, width)
1. 📖 Core Content
1.1 Why Convolutions?
Traditional fully-connected networks for images have two problems:
- Parameter explosion: A 224×224×3 image → 150,528 inputs. A hidden layer with 1024 units requires 154M parameters!
- No spatial invariance: The same object at different positions would need different weights. Convolution solves both by:
- Weight sharing: Same kernel applied everywhere
- Local connectivity: Each neuron connects to a local region only
- Translation equivariance: Objects shifted in input = features shifted in output
1.2 The Convolution Operation
2D convolution: Slide a kernel (filter) over the input, computing dot products:
1.3 Output Size Formula
O=SW−K+2P+1Where:
- O: Output spatial dimension
- W: Input spatial dimension
- K: Kernel size
- P: Padding
- S: Stride
Worked Examples
Example 1: Input 32×32, kernel 3×3, stride 1, padding 0
Example 2: Input 224×224, kernel 7×7, stride 2, padding 3
Since O must be integer, this combination is invalid! Use padding=3 gives:
Example 3: Input 64×64, kernel 3×3, stride 2, padding 1 (keep same spatial dim)
Need padding different to make it divisible.
python# runnable import numpy as np def conv_output_size(W, K, P, S): """Compute output spatial dimension after convolution""" return (W - K + 2*P) // S + 1 def conv_param_count(K, C_in, C_out): """Count parameters in convolutional layer""" weights = K * K * C_in * C_out biases = C_out return weights + biases # Test cases test_cases = [ (224, 7, 3, 2), # Typical first layer (56, 3, 1, 1), # Standard conv block (28, 3, 1, 2), # Strided conv (14, 3, 1, 1), # Deep layer ] print("Input | Kernel | Pad | Stride | Output | Params") print("-" * 55) for W, K, P, S in test_cases: out = conv_output_size(W, K, P, S) params = conv_param_count(K, 64, 128) # Example C_in=64, C_out=128 print(f"{W:5d} | {K:6d} | {P:3d} | {S:6d} | {out:5d} | {params:>7,}")
1.4 Receptive Field
The receptive field is the region in the input that influences a neuron in a deeper layer:
Example: Two 3×3 conv layers with stride 1:
- Layer 1: RF = 1 + (3-1) × 1 = 3
- Layer 2: RF = 3 + (3-1) × 1 = 5 Two 3×3 layers have same RF as one 5×5 layer, but with fewer parameters!
1.5 Pooling
| Type | Operation | Typical Size | Effect |
|---|---|---|---|
| Max Pooling | Take maximum | 2×2, stride 2 | Halves dimensions, keeps strongest features |
| Average Pooling | Take average | 2×2, stride 2 | Halves dimensions, smooths features |
| Global Avg Pooling | Average entire map | Full feature map | Reduces to 1×1 per channel |
📝 Practice Questions
</details> * * * ## 🔗 Cross-References - **Next**: [Advanced CNNs](/courses/bsda5006/notes/.%2Fweek02%2F02-advanced-cnns) - **Video**: BSDA5006 Week 1-2 transcripts [Join Discord](https://discord.gg/gE2m4Qrdqv) [Next**ResNet & Skip Connections**](/notes/04-degree-electives-bsda5006-dl-cv-week02-02-resnet-skip-connections)Q1: Input 227×227×3, first layer: 96 kernels of size 11×11, stride 4, pad 0. Output size?O = (227 - 11 + 0)/4 + 1 = 216/4 + 1 = 55Output: 55×55×96Parameter count: 11 × 11 × 3 × 96 + 96 = 34,944 Q2<strong>Q2</strong>: Why are two 3×3 convolutions better than one 5×5 convolution?
- Fewer parameters: Two 3×3 layers = 2 × (3×3×C×C) = 18C² vs one 5×5 = 25C²
- Same receptive field: Two 3×3 layers give RF of 5
- More nonlinearity: Two ReLU activations instead of one
- More depth: Deeper network = more capacity
This insight from VGGNet (2014) showed that stacking small kernels is better than using large ones. Q3<strong>Q3<strong>Q3<strong>Q3</strong>: Input 32×32, 5×5 conv stride 1, pad 2 → output size?O = (32 - 5 + 4)/1 + 1 = 31 + 1 = 32With pad = (K-1)/2 = 2, the output size equals input size (same convolution). This is the standard "same" padding used in most modern architectures. Q4<strong>Q4<strong>Q4</strong>: A CNN has 3 conv layers (3×3, stride 1) followed by a 2×2 max pool (stride 2). What's the receptive field at the end?Conv layers: each adds (3-1)×1 = 2 to RF After 3 convs: RF = 1 + 3×2 = 7Max pool (2×2, stride 2):
- K_pool = 2, S_pool = 2
- RF_after_pool = RF_before + (K_pool - 1) × ΠS_before
- = 7 + (2-1) × 1 = 7 + 1 = 8
Total receptive field: 8×8