Deep Learning Specializations

Convolutional Neural Networks (CNN)

Lesson 3: Convolutional Neural Networks (CNN) — Computer Vision | Beginner Guide
Deep Learning · Lesson 3

Convolutional Neural Networks (CNN)
Computer Vision & Working with Images

A detailed beginner guide explaining how machines “see” images: from pixels and color channels to convolution and pooling, famous architectures (ResNet, YOLO), transfer learning, object detection, segmentation, and hands-on projects — with numeric examples and everyday analogies.

✍ Abdurrahman Al-Rifai
9 sections
CNN + YOLO
Examples + code
01 Section 1

Image Processing Basics

Before building a CNN, we need to understand how an image is stored on a computer. An image is not a “picture” in the visual sense — it is a table of numbers. A neural network does not “see”; it performs math on those numbers.

Pixels

A pixel (Picture Element) is the smallest unit of an image — a tiny square. Each pixel holds a numeric value representing color intensity or brightness.

Analogy: a mosaic

Think of an image as a mosaic: each small tile = one pixel. Zoom in close on an old TV and you see RGB squares — those are pixels. Resolution = pixel count (e.g. 1920×1080 ≈ 2 million pixels).

Numeric example: 4×4 grayscale image

A tiny 4×4 image = 16 pixels. Each value ranges from 0 (black) to 255 (white):

[ 0, 50, 100, 150,
80, 120, 200, 255,
30, 60, 90, 110,
200, 180, 160, 140 ]

This 4×4 matrix is what the computer “sees.” A CNN slides filters over these numbers to detect patterns (edges, gradients).

Channels

A channel = a separate layer of data per pixel. In RGB, each pixel has 3 values: red, green, blue. Grayscale uses one channel only.

RGB image = 3 stacked channels R — Red G — Green B — Blue Color image Tensor shape: (Height, Width, Channels) — e.g. (224, 224, 3)
Each channel is a 2D matrix — combined they produce the final color

RGB

RGB = Red, Green, Blue. Any screen color is a mix of these three. Values 0–255 per channel:

White

R=255, G=255, B=255 — all channels on.

Black

R=0, G=0, B=0 — no light.

Pure red

R=255, G=0, B=0.

Purple

R=128, G=0, B=128 — red + blue.

Example: one pixel

A “light sky blue” pixel might be: R=135, G=206, B=235. A CNN learns that clusters of such values represent “sky,” “water,” or “grass.”

Grayscale

A grayscale image has one channel. Each pixel = brightness from 0 (black) to 255 (white). Convert RGB with:

Gray = 0.299×R + 0.587×G + 0.114×B

Green is weighted more because the human eye is most sensitive to it. MNIST (handwritten digits) uses 28×28 grayscale — ideal for beginners.

TypeChannelsExampleTensor shape
Grayscale1MNIST, X-rays(28, 28, 1)
RGB3CIFAR-10, selfies(32, 32, 3)
RGBA4PNG with transparency(H, W, 4)
Multi-spectral5+Satellite, medical(H, W, N)
Normalization — essential step

Before feeding images to the network, divide by 255 to get 0–1 — or use mean/std (e.g. ImageNet stats). Without this, training is slow or fails to converge. Example: img = img / 255.0

02 Section 2

CNN Architecture

Why not use a fully connected network for images? A 224×224×3 image = 150,528 inputs. One layer → 1000 neurons = 150 million weights! CNNs solve this with three ideas: weight sharing, local focus, and hierarchical features.

Convolution — step-by-step example

Convolution = a small window (filter) slides over the image, multiplies values, and sums them. Output = a feature map.

Manual calculation: vertical edge filter

3×3 filter (detects vertical edges):

Filter = [-1, 0, 1]
[-1, 0, 1]
[-1, 0, 1]

3×3 patch from the image (brightness values):

Patch = [10, 50, 90]
[10, 55, 95]
[12, 52, 88]

Sum: (-1×10)+(0×50)+(1×90)+(-1×10)+(0×55)+(1×95)+(-1×12)+(0×52)+(1×88) = 191

High value = “strong vertical edge here.” Repeat at every position → full feature map.

Analogy: a lens searching for a pattern

Slide a 3×3 lens over the image and ask: “Do I see a vertical line?” A vertical-edge filter gives high values at edges and low values elsewhere.

Convolution: 3×3 filter on an image Filter = Multiply matching elements + sum = one feature map value
A filter detects a pattern — high value = “pattern found”

Filters

A filter = a weight matrix learned during training. Early layers: edges. Deep layers: eyes, wheels, faces.

Example: Conv2D(filters=32)

Input 128×128×3 → output 128×128×32 — 32 maps, each searching for a different pattern.

Kernels

Kernel = filter (same meaning in most contexts). Common sizes: 3×3, 5×5, 7×7. Larger kernel = wider view but more parameters.

output = (input - kernel + 2×padding) / stride + 1

Feature maps

Each filter → one feature map. 64 filters → 64 maps. The hierarchy:

EdgesShapesPartsWhole objectClass label
model = nn.Sequential(
nn.Conv2d(3, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
nn.Flatten(), nn.Linear(64*56*56, 10)
)
03 Section 3

Pooling

Pooling shrinks the image and keeps the most important information — a “summary” of a region. It reduces computation and adds translation invariance: a cat on the left or right — the model still recognizes it.

Max pooling

A 2×2 window → take the maximum value. Most common in CNNs.

2×2 example
[1,3] [2,6] → max = 6

224×224 → MaxPool 2×2 → 112×112

Average pooling

Average all values in the window — smoother, preserves more information.

[1,3] [2,6] → avg = 3

Global pooling

GAP (Global Average Pooling): average each entire feature map → one vector. 512 maps of 7×7 → 512 numbers. Replaces large Flatten layers — ResNet uses it heavily.

TypeIdeaWhen to use
Max poolingMaximum valueDefault — edges, textures
Average poolingMean valueSmoothing
Global poolingMean of entire mapBefore classification
04 Section 4

CNN Layers — in detail

Each layer has hyperparameters that control output shape. Understanding them = easier debugging and better architecture choices.

Padding

Add zeros (or other values) around image borders before convolution. Same padding keeps size: 224×224 stays 224×224.

Without vs with padding

5×5 image, 3×3 filter, no padding → 3×3 (shrinks!). With padding=1 → 5×5 (same size).

Stride

Stride = how many pixels the filter moves. Stride=1 (slow, high detail). Stride=2 (faster, downsampling like pooling).

stride=2 on 224×224 → 112×112

Flatten

After Conv+Pool, a 3D tensor (e.g. 7×7×512) → 1D vector (25,088). Connects CNN to dense layers for classification.

Dense layers

At the end: Flatten → Dense(128) → ReLU → Dropout → Dense(num_classes) → Softmax. Dense = every neuron connected to every input.

# Keras
model = keras.Sequential([
layers.Conv2D(32, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(64, 3, activation='relu'),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
Conv + ReLU: feature extraction — ReLU adds non-linearity.
BatchNorm: speeds up and stabilizes training — common after Conv.
05 Section 5

Famous Architectures — from LeNet to EfficientNet

Each architecture is a “recipe” for stacking layers. You rarely build from scratch — use pre-built models from PyTorch (torchvision.models) or TensorFlow.

LeNet-5 (1998)

Yann LeCun — MNIST digits. Conv → Pool → Conv → FC. First commercially successful CNN (check reading).

AlexNet (2012)

ImageNet revolution — 8 layers, ReLU, Dropout, GPU. ~26% better error than SVM. Started the deep learning era.

VGG (2014)

Repeated 3×3 layers — VGG16 (16 layers). Simple but ~138M parameters.

GoogLeNet (2014)

Inception modules — multiple filter sizes in parallel. 22 layers but fewer params than VGG.

ResNet (2015)

Skip connections — solved vanishing gradient. ResNet-50/101/152. Industry standard today.

DenseNet (2017)

Every layer connects to all following layers — feature reuse, fewer parameters.

EfficientNet (2019)

Balances depth/width/resolution — high accuracy with fewer params. EfficientNet-B0 to B7.

GoogLeNet — Inception module

Inception idea: “I don’t know the best filter size — use them all!” In one layer: 1×1, 3×3, 5×5 conv + MaxPool — then concatenate. 1×1 conv reduces channels before large conv (bottleneck) — saves parameters.

ResNet — skip connections in detail

Analogy: a shortcut road

Instead of signal passing through 50 layers (and weakening), ResNet adds a shortcut: output = F(x) + x. If F(x)≈0, the layer is “skipped” — training deep networks (152+ layers) becomes feasible.

ArchitectureYearImageNet Top-5ParametersNotes
LeNet-5199860KMNIST
AlexNet201215.3%60MRevolution start
VGG-1620147.3%138MSimple design
ResNet-5020153.6%25MMost used
EfficientNet-B02019~3.3%5MHigh efficiency
from torchvision import models
resnet = models.resnet50(weights='IMAGENET1K_V2')
efficient = models.efficientnet_b0(weights='IMAGENET1K_V1')
06 Section 6

Transfer Learning

Training ResNet on ImageNet (1.2M images) takes weeks on GPU. Transfer learning: take a pre-trained model and adapt it to your task — high accuracy with little data.

Feature extraction

Freeze all pre-trained CNN layers — use them as a ready-made “feature extractor.” Add only new dense layers for classification.

Example: 500 cat/dog images
  1. Load ImageNet-trained ResNet-50.
  2. Freeze all layers: for p in model.parameters(): p.requires_grad = False
  3. Replace final Dense with Dense(2) — cat/dog.
  4. Train only the last layer — 10 epochs may be enough!
  5. ~95%+ accuracy with only 500 images.

Fine tuning

After feature extraction, unfreeze the last 1–3 CNN blocks and train with a small learning rate (0.0001). The model adapts general features (edges, shapes) to your domain (cancer types, plant species).

When to fine-tune?
  • Your data is very different from ImageNet (X-rays, satellite).
  • Feature extraction alone is not enough.
  • You have enough data (1000+).
# Fine-tune last block
for param in model.layer4.parameters():
param.requires_grad = True
optimizer = optim.Adam(
filter(lambda p: p.requires_grad, model.parameters()),
lr=1e-4
)
Small data (<500): feature extraction + augmentation.
Medium data (500–5000): fine-tune last block.
07 Section 7

Object Detection

Classification says: “This image contains a cat.” Detection says: “Cat at (x=120, y=80) size 200×150, and dog at (300, 200).” You need: what + where.

R-CNN (2014)

Region proposals → CNN per region → SVM. Accurate but very slow — seconds per image.

Fast R-CNN (2015)

One CNN on full image + ROI pooling. ~10× faster than R-CNN.

Faster R-CNN (2015)

RPN — Region Proposal Network learns proposals. End-to-end, ~5 FPS.

YOLO (2016+)

You Only Look Once — image → grid, each cell predicts boxes + classes. Real-time — 30–100+ FPS.

SSD (2016)

Single Shot Detector — multi-scale feature maps. Speed/accuracy balance between YOLO and Faster R-CNN.

YOLO — how it works simply

YOLOv8 example
  1. 640×640 image → CNN backbone (e.g. CSPDarknet).
  2. Split into grid (e.g. 20×20).
  3. Each cell predicts: bounding box (x,y,w,h) + confidence + class probabilities.
  4. Non-Max Suppression: remove overlapping boxes — keep the best.
  5. Output: “person 0.92 at [100,50,200,400]”, “car 0.87 at [300,200,500,350]”
IoU — Intersection over Union

Measures overlap between predicted and ground-truth box. IoU > 0.5 = “correct detection.” mAP (mean Average Precision) = standard metric on benchmarks like COCO.

# YOLO with ultralytics — 3 lines!
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
results = model('street.jpg') # detect cars, pedestrians, signs
ModelSpeedAccuracyUse case
Faster R-CNNSlow (~5 FPS)HighResearch, critical accuracy
SSDMedium (~20 FPS)GoodMobile, embedded
YOLOv8Fast (30–100 FPS)Good–highReal-time, CCTV, robotics
08 Section 8

Segmentation

Detection draws a rectangle around an object. Segmentation labels every pixel: “this pixel is cat,” “this is background,” “this is road.” Critical for medicine, self-driving, photo editing.

FCN (2015)

Fully Convolutional Network — first end-to-end segmentation. Replaces FC with conv — output heatmap near full image size.

U-Net (2015)

Encoder-decoder + skip connections in a U shape. Excellent for medical images — works with little data.

Mask R-CNN (2017)

Faster R-CNN + branch for per-object pixel masks. Instance segmentation — separate mask per cat.

DeepLab (2018)

Atrous (dilated) conv — larger receptive field without downsampling. Precise semantic segmentation.

Semantic vs instance segmentation

Semantic: every pixel gets a class — “all blue pixels = sky.” Does not separate cat1 from cat2.
Instance: separate mask per object — “cat1,” “cat2,” “dog1.”

U-Net — why the U shape?

U-Net: Encoder (shrink) → Decoder (expand) Bottleneck Skip connections pass fine details from encoder to decoder
U-Net — U shape with bridges between matching layers
Medical example: tumor segmentation

Input: 256×256 brain MRI. U-Net → 256×256 mask — each pixel: 0=background, 1=tumor. Dice coefficient measures overlap with doctor’s mask — >0.85 is excellent.

09 Section 9

Hands-on Projects — from idea to model

The best way to learn: a real project. Four popular domains with practical steps for beginners.

Image classification

Project: CIFAR-10 (10 classes)
  1. Data: 60,000 images 32×32 — airplane, car, cat...
  2. Preprocessing: normalize, augmentation (flip, crop).
  3. Model: 3 Conv blocks + MaxPool + Dense — or ResNet-18 transfer learning.
  4. Training: Adam, lr=0.001, 50 epochs, early stopping.
  5. Expected: simple CNN ~75%, ResNet transfer ~93%+.
# Quick CIFAR-10 — PyTorch
train_set = datasets.CIFAR10('./data', train=True, transform=transforms.ToTensor())
model = models.resnet18(weights='IMAGENET1K_V1')
model.fc = nn.Linear(512, 10) # 10 classes

Face recognition

Pipeline: face detection (MTCNN or Haar) → face embedding (FaceNet, ArcFace) → cosine similarity comparison.

How Face ID works

1. Detect face in camera. 2. CNN (e.g. FaceNet) maps face → 128-number vector (embedding). 3. Enrollment: store your embedding. 4. Unlock: compare new embedding vs stored — if similarity > threshold → unlock.

Beginner project

Folders faces/person1/, faces/person2/ — 20 photos each. Use facenet-pytorch or deepface. ~95%+ with 20 photos/person.

Medical imaging

X-rays, MRI, CT — CNN + transfer learning. Challenges: little data, class imbalance, critical accuracy.

TaskDatasetModelMetric
Pneumonia from X-rayChestX-ray14DenseNet-121 + TLAUC, Sensitivity
Tumor segmentation MRIBraTS3D U-NetDice score
Diabetic retinopathyEyePACSEfficientNetQuadratic Kappa
Data augmentation: rotate, flip — essential for small medical datasets.
Class weights: if 95% healthy and 5% disease — weight the loss.

OCR — text in images

Optical Character Recognition: image with text → digital text. Modern pipeline: detection (where is text?) + recognition (what does it say?).

Example: license plate reading
  1. YOLO or EAST — detect plate region.
  2. Crop + perspective correction.
  3. CNN + CTC or Transformer (CRNN) — read characters.
  4. Output: “ABC 1234”

Ready tools: Tesseract (classic), EasyOCR, PaddleOCR — CNN + RNN/Transformer inside.

import easyocr
reader = easyocr.Reader(['en', 'ar'])
result = reader.readtext('document.jpg')
for bbox, text, conf in result:
print(f'{text} ({conf:.2f})')
Your first project — 14-day plan
  1. Days 1–2: MNIST with simple CNN — understand Conv+Pool.
  2. Days 3–5: CIFAR-10 + transfer learning (ResNet).
  3. Days 6–8: Your project: 2–3 classes from phone photos.
  4. Days 9–10: YOLO — detect objects in video.
  5. Days 11–12: EasyOCR — read Arabic/English text.
  6. Days 13–14: GitHub README + demo — document your work!
FAQ Questions

Common CNN questions

CNN vs fully connected for images?
Always CNN for images — fewer parameters, translation invariance, hierarchical features. FC only for tiny images (MNIST) or after conv layers.
How many filters in the first Conv layer?
Start with 32, then 64, 128 in later layers — the “double” pattern is common. For beginners: use pre-built ResNet/EfficientNet instead of guessing.
Classification vs detection vs segmentation?
Classification: “what is in the image?” (cat). Detection: “what and where?” (cat at x,y). Segmentation: “which pixel belongs to which class?” — mask per pixel.
Do I need a GPU?
MNIST/CIFAR on CPU is fine. Transfer learning and YOLO — GPU recommended (free Google Colab). Training ImageNet from scratch — powerful GPU for weeks.
PyTorch or TensorFlow for CNN?
Both are excellent. PyTorch + torchvision for learning and research. TensorFlow + Keras for production and TFLite on mobile. Concepts are identical.
Overfitting on images — what do I do?
(1) Data augmentation. (2) Dropout. (3) Transfer learning instead of training from scratch. (4) Early stopping. (5) More data — most important!
What’s next after this lesson?
Try CIFAR-10 + YOLO projects. Then Part 3 on Transformers and GANs. For math: linear algebra and calculus & probability.