Convolutional Neural Networks (CNNs)

Related: 4. Deep Learning and FCNs | 5. Activation Functions | 6. Loss Functions and Backpropagation | 9. CNNs Part II — Improving Generalisability | 10. Transfer Learning and Pre-trained CNNs | Artificial Intelligence


Why CNNs?

FCNs treat input as flat vectors, losing spatial locality — the relationship between neighbouring pixels. CNNs are designed to preserve and exploit spatial structure.

CNN Architecture Components

1. Convolutional Layer

A kernel (filter) slides over the image, performing element-wise multiplication and summation to produce a feature map.

Key parameters:

  • Kernel size — e.g., ,
  • Stride — step size of the filter movement
  • Padding:
    • valid — no padding, output smaller than input
    • same — zero-padding, output same size as input

Output feature map size:

Important: The filter must have the same number of channels as the input (e.g., a filter on an RGB image is actually ).

Number of learnable parameters in a conv layer:

2. Pooling Layer

Reduces spatial dimensions while retaining key information. Max Pooling takes the maximum value in each pooling window.

MaxPooling2D(pool_size=(2, 2), strides=(2, 2))

3. Fully Connected (Classification) Layer

After feature extraction through conv and pooling layers, the feature maps are flattened into a 1D vector and fed to dense layers for classification.

Example: Parameter Calculation

For a 32×32 input image (no padding, stride=1):

LayerConfigOutput ShapeParameters
Conv13×3, 16 filters30×30×16
Conv23×3, 32 filters28×28×32
FC10 outputs10
Total255,632

Pooling layers help reduce the number of parameters in subsequent FC layers.

LeNet Architecture

A classic CNN for handwritten digit recognition (MNIST):

Input (28×28×1)
→ Conv2D(20, kernel=5, valid) → ReLU → MaxPool(2×2)
→ Conv2D(50, kernel=5, valid) → ReLU → MaxPool(2×2)
→ Flatten → Dense(500) → ReLU
→ Dense(10) → Softmax