CNN Fundamentals: Convolution, Pooling, and Architecture Design

942 words
5 min read
Python Week 1: the first filter for runtime behavior
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:
  1. Parameter explosion: A 224ร—224ร—3 image โ†’ 150,528 inputs. A hidden layer with 1024 units requires 154M parameters!
  2. 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:
(Iโˆ—K)(i,j)=โˆ‘m=0Mโˆ’1โˆ‘n=0Nโˆ’1I(i+m,j+n)โ‹…K(m,n)(I * K)(i, j) = \sum_{m=0}^{M-1} \sum_{n=0}^{N-1} I(i+m, j+n) \cdot K(m, n)

1.3 Output Size Formula

O=Wโˆ’K+2PS+1O = \frac{W - K + 2P}{S} + 1
Where:
  • OO: Output spatial dimension
  • WW: Input spatial dimension
  • KK: Kernel size
  • PP: Padding
  • SS: Stride

Worked Examples

Example 1: Input 32ร—32, kernel 3ร—3, stride 1, padding 0
O=32โˆ’3+01+1=29+1=30O = \frac{32 - 3 + 0}{1} + 1 = 29 + 1 = 30
Example 2: Input 224ร—224, kernel 7ร—7, stride 2, padding 3
O=224โˆ’7+62+1=2232+1=111.5+1=112.5O = \frac{224 - 7 + 6}{2} + 1 = \frac{223}{2} + 1 = 111.5 + 1 = 112.5
Since O must be integer, this combination is invalid! Use padding=3 gives:
O=224โˆ’7+62+1=2232+1=112O = \frac{224 - 7 + 6}{2} + 1 = \frac{223}{2} + 1 = 112
Example 3: Input 64ร—64, kernel 3ร—3, stride 2, padding 1 (keep same spatial dim)
O=64โˆ’3+22+1=632+1=32.5โ†’invalidO = \frac{64 - 3 + 2}{2} + 1 = \frac{63}{2} + 1 = 32.5 \rightarrow \text{invalid}
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:
RFl=RFlโˆ’1+(Klโˆ’1)โ‹…โˆi=1lโˆ’1SiRF_{l} = RF_{l-1} + (K_l - 1) \cdot \prod_{i=1}^{l-1} S_i
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

TypeOperationTypical SizeEffect
Max PoolingTake maximum2ร—2, stride 2Halves dimensions, keeps strongest features
Average PoolingTake average2ร—2, stride 2Halves dimensions, smooths features
Global Avg PoolingAverage entire mapFull feature mapReduces to 1ร—1 per channel

๐Ÿ“ Practice Questions

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 = 55
Output: 55ร—55ร—96
Parameter count: 11 ร— 11 ร— 3 ร— 96 + 96 = 34,944 Q2
<strong>Q2</strong>: Why are two 3ร—3 convolutions better than one 5ร—5 convolution?
  1. Fewer parameters: Two 3ร—3 layers = 2 ร— (3ร—3ร—Cร—C) = 18Cยฒ vs one 5ร—5 = 25Cยฒ
  2. Same receptive field: Two 3ร—3 layers give RF of 5
  3. More nonlinearity: Two ReLU activations instead of one
  4. 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 = 32
With 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 = 7
Max 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
</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)
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.