CNN

Convolutional Neural Networks

Slide 1 / 10
Deep Learning & Computer Vision

Understanding Convolutional Neural Networks

How machines process multi-channel images, extract spatial features, and power modern computer vision—from digit recognition to real-time object detection.

👁️

Spatial Hierarchy

Preserves 2D/3D pixel relationships instead of flattening images into 1D lists.

🔍

Feature Extraction

Automatically learns filters for edges, textures, shapes, and complex objects.

⚡

Parameter Sharing

Reuses small kernels across the entire input volume, drastically cutting parameters.

01 / The Motivation

Why Not Standard Neural Networks (ANNs)?

⚠️ 1. Parameter Explosion

A tiny 28×28 grayscale digit image has 784 pixels. Connecting this to one hidden layer of 512 neurons requires 401,408 weights. For a 1000×1000×3 RGB photo, a single layer needs 3 billion parameters!

🧩 2. Loss of Spatial Structure

Flattening a 2D grid into a 1D vector destroys vital neighborhood context. Pixels adjacent vertically or diagonally are separated, making it hard to recognize patterns under translation.

Traditional ANN Approach: Flattening a 28×28 Image of Digit "9"

28×28 Image 784 ⋮ 0 1 2 ⋮ 9 (Active)

CNNs solve this by using local receptive fields—scanning small regions at a time!

02 / Core Mechanism

The Convolution Operation (Interactive)

A small matrix called a Kernel (or Filter) slides across the Input Image. At each position, we compute the element-wise multiplication and sum the results to produce an Activation / Feature Map.

Input (3×3)
∗
Kernel (2×2)
=
Feature Map (2×2)
Dot Product Calculation at Current Position:
(0×0) + (1×1) + (3×2) + (4×3) = 0 + 1 + 6 + 12 = 19
03 / Feature Extraction

How Kernels Detect Visual Features

Different weights inside a kernel act as specialized feature detectors. Early CNN layers learn low-level filters (edges, corners), while deeper layers combine them into high-level shapes.

6×6 Input Patch
3×3 Kernel
4×4 Output Map

Hover over any cell in the 4×4 Output Map to highlight its 3×3 receptive field!

04 / Multi-Channel Processing

How a 3-Channel RGB Image is Processed

Filters (K):

An RGB image has shape H × W × 3 (Red, Green, Blue). A single convolutional filter also has depth 3 (F × F × 3)—one kernel slice per color channel—whose dot products are summed element-wise (+ bias) into one 2D feature map.

1. RGB Input Volume (3×3×3)
3 Stacked 2D Color Channels
Red (R)
201 130 021
Green (G)
120 012 101
Blue (B)
012 201 110
∗ Per Channel
2. 3D Filter W₁ (3×3×3)
Separate Weights for R, G & B
WR (Sum=6)
101 010 10-1
WG (Sum=4)
010 110 100
WB (Sum=3)
001 011 001
= Sum + b
3. Output Map
1 × 1 × 1
14 Map 1
6(R) + 4(G) + 3(B) + 1(bias) = 14 (1 channel!)
End-to-End RGB Flow: H×W×3 → Conv (K filters) → O×O×K → ReLU → Pool → Flatten → Softmax.
05 / Architecture Analysis (CS 230)

Layer Parameters & Receptive Field

Number of Learnable Parameters per Layer

Understanding model complexity across CONV, POOL, and Fully Connected (FC) layers:

Layer Type Input → Output Parameters Formula
CONV I×I×C → O×O×K (F × F × C + 1) · K
POOL I×I×C → O×O×C 0 (Fixed operation)
FC (Dense) Nin → Nout (Nin + 1) × Nout

* "+ 1" accounts for the bias parameter per filter (CONV) or per neuron (FC).

Receptive Field (Rk)

The area Rk × Rk of the original input image that a single neuron in layer k can "see". Stacking small 3×3 layers expands the effective field of view with fewer parameters!

5 × 5
Input (R₂=5)
→
3 × 3
Conv 1 (3×3)
→
1×1
Conv 2 (3×3)
Two 3×3 layers (18 params) = One 5×5 view (25 params)
06 / Non-Linearity

Activation Functions (ReLU & Variants)

Convolution is a linear operation (dot products). Applying a non-linear activation function element-wise after every CONV layer enables CNNs to learn complex, non-linear decision boundaries.

ReLU
g(z) = max(0, z)

Fast, sparse activations; zeroes out negative values.

Leaky ReLU
g(z) = max(εz, z)

Small slope (ε ≈ 0.01) prevents "dying ReLU" neurons.

ELU
α(ez-1) if z<0

Smooth curve everywhere; pushes mean activations near 0.

Element-Wise ReLU Transformation
Feature Map
-4 12 7 -9
→
After ReLU
0 12 7 0
07 / Spatial Downsampling & Invariance

Different Types of Pooling Layers

Interactive Pooling Playground f(X) = max(X)

Click any pooling type on the left to see how it compresses the same 4×4 activation map:

4×4 Feature Map
12 20 30 0 8 12 2 0 34 70 37 4 112 100 25 12
→
2×2 Max Pooled
20 30 112 37
Why Pooling Matters: Reduces spatial size by 75% (for 2×2, S=2), cuts parameters in subsequent layers, and gives local translation invariance.
08 / Live End-to-End Walkthrough

How an End-to-End CNN Works (Live Demo)

Test Input:

Watch a 6×6 binary image pass through every single layer of a real CNN forward pass: Input (6×6) → Conv 3×3 + ReLU (4×4) → Max Pool 2×2 (2×2) → Flatten (4D) → Dense + Softmax.

1. Input Image 6 × 6 × 1
3×3 Filter →
2. Conv + ReLU 4 × 4 × 1
2×2 Max →
3. Max Pooling 2 × 2 × 1
Flatten+FC →
4. Flatten & Softmax
[4, 4, 4, 4]
Trace: The 3×3 diagonal-cross kernel activates strongly at all 4 quadrants of "X" → Max Pool keeps peak activations [4, 4, 4, 4] → Softmax predicts Pattern "X" (94%).
09 / Beyond Image Classification (CS 230)

Where CNNs Are Used in Computer Vision

🎯

Object Detection

Predicts both class probabilities and bounding box coordinates (bx, by, bh, bw) for multiple objects using architectures like YOLO and Faster R-CNN.

Key Metrics: Intersection over Union (IoU ≥ 0.5) & Non-Max Suppression.
🪪

Face Verification

Uses Siamese Networks and Triplet Loss to encode faces into compact embeddings and measure similarity distance d(img₁, img₂) for one-shot recognition.

Goal: Minimize Anchor-Positive distance while maximizing Anchor-Negative distance.
🎨

Famous CNN Backbones

Modern vision systems build upon landmark CNN designs that solved vanishing gradients and multi-scale feature extraction:

• LeNet / AlexNet / VGG: Classic stacked blocks
• ResNet: Residual skip connections
• Inception: Multi-size parallel filters