Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
LECTURE NOTES
Deep Learning for
Computer Vision
Course: Deep Learning / Artificial Intelligence
Program: BS Computer Science — 8th Semester
Week / Lecture: Week 8
Topic: Deep Learning for Computer Vision (ConvNets)
Instructor: Prof. Muhammad Arslan Naveed
Reference Book: Deep Learning with Python — François Chollet
Status: Confidential — For Students Only
"ConvNets are to computer vision what language models are to NLP — the fundamental
building block."
Department of Computer Science | Confidential Lecture Material Page 1
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Table of Contents
1. Introduction to Convolutional Neural Networks (ConvNets)
2. The Convolution Operation
3. The Max-Pooling Operation
4. Training a ConvNet from Scratch on a Small Dataset
5. Data Preprocessing
6. Data Augmentation
7. Summary Table
8. Exam Practice Questions
9. Key Terms Glossary
Department of Computer Science | Confidential Lecture Material Page 2
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 1: Introduction to Convolutional Neural Networks
(ConvNets)
1.1 What is a ConvNet?
A Convolutional Neural Network (CNN/ConvNet) is a class of deep neural networks specifically designed
to process structured grid data such as images, video frames, and any data with a spatial hierarchy.
ConvNets have revolutionized computer vision tasks including image classification, object detection, image
segmentation, and facial recognition.
1.2 Why Not Use Simple Dense Networks for Images?
A traditional fully-connected (dense) neural network treats every input pixel independently, ignoring all spatial
structure. This leads to enormous parameter counts and poor generalization.
Feature Dense Network ConvNet
Parameter count Extremely high Much lower (weight sharing)
Spatial awareness None Yes — local connectivity
Translation invariance No Yes
Performance on images Poor Excellent
Training speed Slow Faster due to fewer params
Concrete Example:
A 224×224 RGB image has 224 × 224 × 3 = 150,528 input values. A single dense layer with 512 neurons
would require 150,528 × 512 = ~77 million parameters — just for ONE layer! ConvNets solve this by using
local connectivity and weight sharing through filters.
1.3 Architecture of a Basic ConvNet
INPUT IMAGE (e.g., 150x150x3) | [Conv2D Layer] --> Feature Maps (detects edges, shapes) |
[Activation - ReLU] --> Non-linearity | [MaxPooling2D] --> Reduced Feature Maps | [Conv2D
Layer] --> Deeper Feature Maps (textures) | [MaxPooling2D] | [Conv2D Layer] --> High-level
features (object parts) | [Flatten] | [Dense Layer - 512 neurons] | [Output Layer -
Sigmoid/Softmax] | PREDICTION (e.g., Cat or Dog)
1.4 Hierarchical Feature Learning
Department of Computer Science | Confidential Lecture Material Page 3
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
One of the most powerful aspects of ConvNets is that they learn features in a hierarchical manner. Each
successive layer learns increasingly abstract features:
• Layer 1: Edges, lines, color gradients
• Layer 2: Textures, corners, simple patterns
• Layer 3: Object parts (eyes, wheels, handles)
• Layer 4+: Complete objects (faces, cars, animals)
■ This hierarchical learning is what makes ConvNets so powerful — they do not need hand-crafted
feature engineering; they learn features automatically from data.
Department of Computer Science | Confidential Lecture Material Page 4
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 2: The Convolution Operation
2.1 What is Convolution?
The convolution operation applies a small matrix called a filter (kernel) over the input image to detect
specific features. The filter slides across the image and at each position performs an element-wise
multiplication followed by a sum, producing a single output value. The collection of all output values forms the
feature map (activation map).
2.2 Step-by-Step Convolution Process
Step 1 — Define the Filter (Kernel)
A filter is typically 3×3 or 5×5 in size. Different filters detect different features:
Example 3x3 Edge Detection Filter (Horizontal edges): [ -1 -1 -1 ] [ 0 0 0 ] [ 1 1 1 ]
Example 3x3 Sharpening Filter: [ 0 -1 0 ] [ -1 5 -1 ] [ 0 -1 0 ]
Step 2 — Slide the Filter Across the Image
The filter moves across the image with a defined stride. At each position, element-wise multiplication is
performed between the filter and the covered image patch, then all values are summed to produce one
number.
Input Patch: Filter: Computation: [ 10 20 30 ] [ 1 0 -1 ] (10*1)+(20*0)+(30*-1) = -20 [ 40
50 60 ] [ 1 0 -1 ] + (40*1)+(50*0)+(60*-1) = -20 [ 70 80 90 ] [ 1 0 -1 ] +
(70*1)+(80*0)+(90*-1) = -20 ------------------------- Output Value = -60
Step 3 — Produce the Feature Map
Each position of the sliding filter produces one output value. All values together form the feature map.
Multiple filters produce multiple feature maps (one per filter), giving the network the ability to detect many
different features simultaneously.
2.3 Key Parameters of the Convolution Operation
Parameter Definition Effect on Output
Filter Size Dimensions of kernel (e.g. 3x3, 5x5) Larger = bigger receptive field, more context
Stride How many pixels the filter moves each stepLarger stride = smaller feature map
Padding (same) Zeros added around input border Keeps output same size as input
Padding (valid) No padding applied Output is smaller than input
No. of Filters How many different kernels are used More filters = more features detected
Department of Computer Science | Confidential Lecture Material Page 5
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Depth (Channels) Input channels (e.g. R,G,B = 3) Filter depth must match input depth
2.4 Convolution in Keras — Full Code Example
from [Link] import layers, models, optimizers model = [Link]() # Conv
Layer 1: 32 filters of size 3x3, ReLU activation # Input: 150x150 RGB image
[Link](layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
[Link](layers.MaxPooling2D((2, 2))) # Conv Layer 2: 64 filters
[Link](layers.Conv2D(64, (3, 3), activation='relu')) [Link](layers.MaxPooling2D((2,
2))) # Conv Layer 3: 128 filters [Link](layers.Conv2D(128, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2))) # Conv Layer 4: 128 filters
[Link](layers.Conv2D(128, (3, 3), activation='relu')) [Link](layers.MaxPooling2D((2,
2))) # Flatten and Dense layers [Link]([Link]()) [Link]([Link](512,
activation='relu')) [Link]([Link](1, activation='sigmoid')) # Binary output
[Link]( loss='binary_crossentropy', optimizer=[Link](lr=1e-4),
metrics=['accuracy'] ) [Link]()
Department of Computer Science | Confidential Lecture Material Page 6
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 3: The Max-Pooling Operation
3.1 What is Pooling?
Pooling is a down-sampling operation that reduces the spatial dimensions (width and height) of feature
maps while retaining the most important information. It is applied after convolutional layers and is a critical
component in preventing overfitting and reducing computation.
3.2 Max-Pooling — Detailed Explanation
Max-Pooling takes the maximum value from each pooling window. A 2×2 max-pool with stride 2 reduces
each spatial dimension by half.
Input Feature Map (4x4): After Max-Pool (2x2, stride=2): +------+------+------+------+
+------+------+ | 1 | 3 | 2 | 4 | | 6 | 8 | +------+------+------+------+ -->
+------+------+ | 5 | 6 | 7 | 8 | | 3 | 4 | +------+------+------+------+ +------+------+ |
3 | 2 | 1 | 0 | +------+------+------+------+ Explanation: | 1 | 2 | 3 | 4 | Top-left 2x2
block: max(1,3,5,6) = 6 +------+------+------+------+ Top-right 2x2 block: max(2,4,7,8) =
8 Bottom-left 2x2 block: max(3,2,1,2) = 3 Bottom-right 2x2 block: max(1,0,3,4) = 4
3.3 Why Use Max-Pooling?
Benefit Explanation
Dimensionality Reduction Reduces memory and computation in subsequent layers
Translation Invariance Small shifts/rotations in image do not affect output significantly
Overfitting Prevention Fewer parameters = less risk of memorizing training data
Dominant Feature Retention Keeps the strongest activations — most important signals
Faster Training Smaller feature maps mean faster forward and backward passes
3.4 Types of Pooling
Pooling Type Operation Use Case
Max Pooling Takes maximum value in window Most common — retains dominant features
Average Pooling Takes average of values in window Smoother output, less sharp
Global Average Pooling One average value per feature map Used before output layer — fewer params
Global Max Pooling One maximum value per feature map Alternative to flatten layer
Department of Computer Science | Confidential Lecture Material Page 7
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
■ In practice, Max-Pooling is the default choice because it aggressively retains the most activated
(most relevant) features and tends to yield better classification accuracy.
Department of Computer Science | Confidential Lecture Material Page 8
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 4: Training a ConvNet from Scratch on a Small
Dataset
4.1 The Core Problem — Small Data and Overfitting
Deep learning models typically require large amounts of labeled training data to learn generalizable
representations. When working with a small dataset (hundreds or a few thousand images), the model risks
overfitting — memorizing the training data instead of learning general patterns.
What is Overfitting?
Training Accuracy: 98% <-- Model has memorized the training set Validation Accuracy: 55%
<-- Model fails on unseen data ^^^^ This large gap = OVERFITTING Signs of overfitting: -
Training loss decreasing while validation loss increases - Training accuracy much higher
than validation accuracy - Model performs well on seen data but poorly on new data
4.2 Dataset Organization — Dogs vs Cats Example
We use a small subset of the Kaggle Dogs vs Cats dataset with the following structure:
cats_and_dogs_small/ train/ cats/ --> 1,000 cat images dogs/ --> 1,000 dog images
validation/ cats/ --> 500 cat images dogs/ --> 500 dog images test/ cats/ --> 500 cat
images dogs/ --> 500 dog images Total training samples: 2,000 Total validation samples:
1,000 Total test samples: 1,000
4.3 Downloading and Organizing the Data
import os import shutil original_dataset_dir = '/path/to/original/train' base_dir =
'/path/to/cats_and_dogs_small' [Link](base_dir, exist_ok=True) # Create directory
structure for split in ['train', 'validation', 'test']: for category in ['cats', 'dogs']:
dir_path = [Link](base_dir, split, category) [Link](dir_path, exist_ok=True) #
Copy cat images fnames = [f'cat.{i}.jpg' for i in range(1000)] for fname in fnames: src =
[Link](original_dataset_dir, fname) dst = [Link](base_dir, 'train', 'cats',
fname) [Link](src, dst) # Copy dog images fnames = [f'dog.{i}.jpg' for i in
range(1000)] for fname in fnames: src = [Link](original_dataset_dir, fname) dst =
[Link](base_dir, 'train', 'dogs', fname) [Link](src, dst) print("Dataset
organized successfully!")
4.4 Building the ConvNet Architecture
from [Link] import layers, models, optimizers model = [Link]([ #
Block 1 layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
layers.MaxPooling2D((2,2)), # Block 2 layers.Conv2D(64, (3,3), activation='relu'),
Department of Computer Science | Confidential Lecture Material Page 9
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
layers.MaxPooling2D((2,2)), # Block 3 layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)), # Block 4 layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)), # Classification head [Link](), [Link](512,
activation='relu'), [Link](1, activation='sigmoid') # Binary: cat(0) or dog(1) ])
[Link]( loss='binary_crossentropy', optimizer=[Link](lr=1e-4),
metrics=['accuracy'] ) [Link]()
Department of Computer Science | Confidential Lecture Material Page 10
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 5: Data Preprocessing
5.1 Why Preprocess Images?
Neural networks cannot directly process raw image files (JPEG/PNG). Images must be converted into
floating-point tensors with normalized pixel values. There are two main reasons for normalization:
• Numerical Stability: Raw pixel values range from 0–255. Large input values can cause unstable
gradients during training.
• Faster Convergence: Normalized inputs [0, 1] help the optimizer converge more quickly and reliably.
5.2 The Image Preprocessing Pipeline
Step 1: Read the raw image file from disk (.jpg / .png) Step 2: Decode JPEG/PNG content
into a pixel grid (H x W x C) Step 3: Convert integer pixel values to float32 tensors Step
4: Rescale pixel values: [0, 255] --> [0.0, 1.0] Formula: normalized_value = pixel_value /
255.0 Step 5: Resize all images to a uniform size (e.g., 150 x 150) Step 6: Feed batch of
tensors into the network
5.3 Using ImageDataGenerator for Preprocessing
from [Link] import ImageDataGenerator # Rescale pixel values
from [0,255] to [0,1] train_datagen = ImageDataGenerator(rescale=1./255) test_datagen =
ImageDataGenerator(rescale=1./255) # Only rescale for val/test # Create training data
generator train_generator = train_datagen.flow_from_directory( train_dir, # Directory path
target_size=(150, 150), # Resize all images to 150x150 batch_size=20, # Process 20 images
at a time class_mode='binary' # Binary labels: 0 (cat) or 1 (dog) ) # Create validation
data generator validation_generator = test_datagen.flow_from_directory( validation_dir,
target_size=(150, 150), batch_size=20, class_mode='binary' ) # Check output for
data_batch, labels_batch in train_generator: print('Data batch shape:', data_batch.shape)
# (20, 150, 150, 3) print('Labels batch shape:', labels_batch.shape) # (20,) break
5.4 Training the Model with Generators
history = [Link]( train_generator, steps_per_epoch=100, # 100 batches x 20 images = 2000
train images/epoch epochs=30, validation_data=validation_generator, validation_steps=50 #
50 batches x 20 images = 1000 val images/epoch ) # Save the model
[Link]('cats_and_dogs_small_1.h5') print("Model saved!")
Department of Computer Science | Confidential Lecture Material Page 11
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 6: Data Augmentation
6.1 What is Data Augmentation?
Data augmentation is the practice of artificially expanding your training dataset by applying random but
realistic transformations to existing training images. Each time a training image is shown to the network, it is
transformed differently — so the model effectively sees more unique examples even though the original
dataset remains the same size.
■ Data augmentation does NOT create new information — it creates new PERSPECTIVES of existing
information. This forces the model to learn more robust, generalizable features.
6.2 Common Augmentation Techniques
Technique Description Keras Parameter Example Value
Rotation Randomly rotate image by up torotation_range
N degrees 40
Width Shift Horizontally shift image by fraction
width_shift_range 0.2
Height Shift Vertically shift image by fraction height_shift_range 0.2
Shear Shear the image along an axis shear_range 0.2
Zoom Randomly zoom in or out zoom_range 0.2
Horizontal Flip Mirror image left-to-right horizontal_flip True
Vertical Flip Mirror image top-to-bottom vertical_flip False (usually)
Fill Mode How to fill empty pixels after transform
fill_mode 'nearest'
Brightness Randomly change brightness brightness_range [0.8, 1.2]
Channel Shift Randomly shift colour channels channel_shift_range 20
6.3 Implementing Data Augmentation in Keras
from [Link] import ImageDataGenerator # Training generator
WITH augmentation train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=40, #
Rotate up to 40 degrees width_shift_range=0.2, # Shift horizontally up to 20%
height_shift_range=0.2, # Shift vertically up to 20% shear_range=0.2, # Shear
transformation zoom_range=0.2, # Zoom in/out up to 20% horizontal_flip=True, # Randomly
flip horizontally fill_mode='nearest' # Fill gaps with nearest pixel ) # Validation/Test
generator WITHOUT augmentation (only rescale!) test_datagen =
ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory(
train_dir, target_size=(150, 150), batch_size=32, class_mode='binary' )
Department of Computer Science | Confidential Lecture Material Page 12
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
validation_generator = test_datagen.flow_from_directory( validation_dir, target_size=(150,
150), batch_size=32, class_mode='binary' )
■■ CRITICAL RULE: Never apply augmentation to validation or test data! Only the TRAINING data
should be augmented. Validation and test data must remain unchanged (only normalize/rescale) to give
an honest evaluation of model performance.
6.4 Building an Augmented Model with Dropout
When using data augmentation, it is common to also add a Dropout layer to further combat overfitting:
model = [Link]([ layers.Conv2D(32, (3,3), activation='relu', input_shape=(150,
150, 3)), layers.MaxPooling2D((2,2)), layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)), layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)), layers.Conv2D(128, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)), [Link](), [Link](0.5), # Dropout: randomly
zero 50% of neurons [Link](512, activation='relu'), [Link](1,
activation='sigmoid') ]) [Link]( loss='binary_crossentropy',
optimizer=[Link](lr=1e-4), metrics=['accuracy'] ) # Train with augmented data
history = [Link]( train_generator, steps_per_epoch=100, epochs=100,
validation_data=validation_generator, validation_steps=50 )
[Link]('cats_and_dogs_augmented.h5')
6.5 Visualizing Training Results
import [Link] as plt acc = [Link]['accuracy'] val_acc =
[Link]['val_accuracy'] loss = [Link]['loss'] val_loss =
[Link]['val_loss'] epochs = range(1, len(acc) + 1) [Link](figsize=(14, 5)) #
Accuracy plot [Link](1, 2, 1) [Link](epochs, acc, 'bo-', label='Training Accuracy')
[Link](epochs, val_acc, 'ro-', label='Validation Accuracy') [Link]('Training vs
Validation Accuracy') [Link]('Epochs') [Link]('Accuracy') [Link]() # Loss plot
[Link](1, 2, 2) [Link](epochs, loss, 'bo-', label='Training Loss') [Link](epochs,
val_loss, 'ro-', label='Validation Loss') [Link]('Training vs Validation Loss')
[Link]('Epochs') [Link]('Loss') [Link]() plt.tight_layout()
[Link]('training_history.png', dpi=150) [Link]()
Department of Computer Science | Confidential Lecture Material Page 13
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 7: Complete Summary
Topic Key Concept Key Benefit
ConvNet Specialized neural network for image processing
Efficient,
using filters
spatially-aware feature learning
Convolution Filter slides over image, element-wise multiply Detects
and sumlocal features: edges, textures, objects
Feature Map Output of a convolution operation Represents where features appear in the image
Max-Pooling Reduces spatial size by taking max value in each
Reduces
window
computation, adds translation invariance
Small Dataset Limited training data causes overfitting Augmentation and Dropout help overcome this
Preprocessing Normalize pixel values [0,255] -> [0,1], resize images
Numerical stability and faster convergence
Data Augmentation Random transforms on training images Artificially expands dataset, reduces overfitting
Dropout Randomly zero neurons during training Forces network to learn redundant representations
RMSprop Adaptive learning rate optimizer Good default optimizer for ConvNets in Keras
Binary Crossentropy Loss function for binary classification Measures difference between predicted and true labels
Department of Computer Science | Confidential Lecture Material Page 14
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 8: Exam Practice Questions
Q1 [2 marks]: Define a Convolutional Neural Network. How does it differ from a traditional
fully-connected neural network?
Q2 [3 marks]: Explain the convolution operation in detail. What is a filter/kernel, and what
does a feature map represent?
Q3 [2 marks]: What is the purpose of the Max-Pooling operation? Show with an example how
a 2x2 max-pool with stride 2 works on a 4x4 matrix.
Q4 [3 marks]: Why does training a ConvNet on a small dataset cause overfitting? How does
data augmentation address this problem?
Q5 [5 marks]: Write complete Keras code to: (a) build a ConvNet for binary image
classification, (b) add data augmentation, and (c) train the model using ImageDataGenerator.
Q6 [2 marks]: Why should data augmentation NOT be applied to validation data? What would
happen if you did?
Q7 [2 marks]: Explain the concept of hierarchical feature learning in ConvNets. What types of
features are learned at different depths?
Q8 [3 marks]: Differentiate between padding='same' and padding='valid' in a Conv2D layer.
How does stride affect the output dimensions?
Q9 [2 marks]: What is the role of the Dropout layer? How does it prevent overfitting?
Q10 [3 marks]: Compare Max-Pooling and Average Pooling. When would you prefer to use
Global Average Pooling?
Department of Computer Science | Confidential Lecture Material Page 15
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Part 9: Key Terms Glossary
ConvNet / CNN
Convolutional Neural Network — specialized deep learning architecture for image data.
Filter / Kernel
A small matrix (e.g., 3x3) that slides over the input to detect features via convolution.
Feature Map
The output of a convolution operation; represents detected features at each spatial location.
Stride
The number of pixels the filter moves at each step during convolution.
Padding
Zeros added around the input border. 'same' preserves size; 'valid' allows shrinkage.
Max-Pooling
Down-sampling by taking the maximum value in each pooling window.
Overfitting
When a model memorizes training data and fails to generalize to new, unseen data.
Data Augmentation
Artificially expanding training data via random transformations (rotate, flip, zoom, etc.).
Rescaling
Normalizing pixel values from [0, 255] to [0.0, 1.0] for stable neural network training.
Dropout
Regularization technique that randomly sets a fraction of neurons to zero during training.
ReLU
Rectified Linear Unit — activation function: f(x) = max(0, x). Introduces non-linearity.
Department of Computer Science | Confidential Lecture Material Page 16
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
Sigmoid
Activation function that outputs values between 0 and 1. Used for binary classification.
Binary Crossentropy
Loss function for binary classification tasks.
RMSprop
Root Mean Square Propagation — adaptive learning rate optimizer, popular for ConvNets.
ImageDataGenerator
Keras utility for loading images from disk and applying real-time augmentation.
Translation Invariance
Property where the output is unchanged (or minimally affected) by spatial shifts in input.
Weight Sharing
In ConvNets, the same filter weights are applied across the entire image, reducing parameters.
Hierarchical Learning
Progressive learning of increasingly abstract features from simple edges to complex objects.
Department of Computer Science | Confidential Lecture Material Page 17
Deep Learning for Computer Vision — Week 8
Prof. Muhammad Arslan Naveed
BS Computer Science | 8th Semester
End of Lecture — Week 8
Next Lecture (Week 9):
Using Pretrained ConvNets — Feature Extraction & Fine-Tuning
Visualizing What ConvNets Learn
Prepared exclusively for BS CS 8th Semester Students
Prof. Muhammad Arslan Naveed | Department of Computer Science
Department of Computer Science | Confidential Lecture Material Page 18