Quiz 2

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:
(IK)(i,j)=m=0M1n=0N1I(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=WK+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=323+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=2247+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=2247+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=643+22+1=632+1=32.5invalidO = \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=RFl1+(Kl1)i=1l1SiRF_{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
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.