0% found this document useful (0 votes)
14 views106 pages

Convolutional Neural Networks Overview

The document provides an overview of Convolutional Neural Networks (CNNs), detailing their architecture, components, and functionalities, including convolutional layers, pooling layers, and fully connected layers. It discusses various CNN architectures such as LeNet, AlexNet, VGG, and GoogLeNet, highlighting their unique features and contributions to image recognition tasks. Additionally, it outlines steps to build a CNN model using frameworks like TensorFlow/Keras, including data preprocessing, model compilation, training, evaluation, and potential improvements.

Uploaded by

appu3.2k6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views106 pages

Convolutional Neural Networks Overview

The document provides an overview of Convolutional Neural Networks (CNNs), detailing their architecture, components, and functionalities, including convolutional layers, pooling layers, and fully connected layers. It discusses various CNN architectures such as LeNet, AlexNet, VGG, and GoogLeNet, highlighting their unique features and contributions to image recognition tasks. Additionally, it outlines steps to build a CNN model using frameworks like TensorFlow/Keras, including data preprocessing, model compilation, training, evaluation, and potential improvements.

Uploaded by

appu3.2k6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Module-3: Convolution Neural Networks

 Convolutional networks,
 optimization and loss functions in classifiers,
 Convolution layers,
 max pool layers,
 VGG, Google Net, ResNet,
 dropout, normalization, rules update,
 data augmentation,
 transfer learning,
 Analysis of pre trained models

Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE


Binary (B/W) image representation
•Each pixel is either black or white.
•Represented using 1 bit per pixel (bpp).
Grayscale Image (Shades of
Gray):
•Includes shades between black and white.
•Typically uses 8 bits per pixel (bpp) → 256 intensity levels (0–255).
•0 → Black
•255 → White
•Values in between → Shades of gray.
•Only one channel(28x28)
RGB Color image
•Each pixel is represented using Red, Green, Blue (RGB)
components.
•24 bits per pixel (bpp) → 8 bits for each channel.
•Red: 0–255
•Green: 0–255
•Blue: 0–25
• Three channels (28x28x3)

Example:
•(255, 0, 0) → Red
•(0, 255, 0) → Green
•(0, 0, 255) → Blue
•(255, 255, 255) →
White
•(0, 0, 0) → Black
Convolutional Neural Network
• A CNN (Convolutional Neural Network) is a type of deep learning algorithm that is
especially effective for analyzing visual data such as images, videos, and pattern
• Unlike traditional neural networks, CNNs are specifically designed to automatically
extract features (edges, textures, shapes, objects, etc.) from data(images).
• Yann LeCun developed the first CNN in 1988 called LeNet. It was used for recognizing
characters like ZIP codes and digits.
Key Points about CNN:
Specialized for images: Works very well in image recognition, classification, and
computer vision tasks.
Feature extraction: Uses filters (kernels) to automatically detect features like edges,
textures, shapes, etc., without manual feature engineering.
CNN architecture
CNN architecture
Architecture of a CNN consists of following components
Input Layer
• Accepts raw image (e.g., 28×28 grayscale, or 224×224×3 RGB).
Convolutional Layer
• Applies filters (kernels) that slide over the input image to detect features (edges,
textures, shapes).
• Produces a feature map.
• Example: A 3×3 filter extracts edges, corners, or patterns.
Activation Function (ReLU)
• Adds non-linearity, keeping only positive values.
Pooling Layer (Downsampling)
• Reduces the size of the feature map while keeping important information.
• Max Pooling → takes the maximum value in a region.
• Average Pooling → takes the average.
• Example: 2×2 max pooling reduces a 28×28 feature map → 14×14.
CNN architecture
Flattening
• Converts the 2D feature maps into a 1D vector.
• Example: 7×7×64 → 3136 neurons.
Fully Connected (Dense) Layers
• Traditional neural network layer.
• Each neuron is connected to all neurons in the previous layer.
• Combines the extracted features to make predictions.
Output Layer
• Uses Softmax (for classification) or Sigmoid (for binary tasks).
• Gives final probabilities.
• Example: For digit recognition (0–9), output layer has 10 neurons.
WHY CNN AND WHY NOT
ANN ?
CONVOLUTION LAYER
 The first layer of a Convolutional Neural Network is always a Convolutional Layer.

 In Convolutional Layer, features are extracted from an image.

 A convolution converts all the pixels in its receptive field into a single value.

 Convolutional filters (kernels) are small matrices that slide over an image to

extract specific features.

 Different filters are used to detect edges, textures, shapes, and patterns at

various levels of a CNN.


CONVOLUTIONAL LAYER
A single convolutional layer consists of:

Input Image (e.g., 64×64×3 for RGB)

Filters (Kernels) (e.g., 3×3, 5×5 matrices)

Stride (movement of the filter)

Padding (optional, keeps size same)

Activation Function (e.g., ReLU)


HOW CONVOLUTION WORKS
1. A filter, also called a kernel, is applied to the input data

2. The filter is slid over the input image, one pixel at a time

3. At each location, element wise multiplication is performed

4. The results are summed onto a feature map


Keeps output size ≈ input size (when stride
= 1).
SAMPLE PROBLEM ON
CONVOLUTION OPERATION
HOW MANY FILTERS CAN BE USED IN A
CONVOLUTIONAL LAYER?
The number of filters (kernels) in a convolutional layer is a hyper parameter, meaning
you can choose it based on the complexity of the problem and computational resources.
 Small models: 16, 32, 64 filters

 Large models: 128, 256, 512 filters

 More filters → More feature extraction (edges, textures, shapes)

 Too many filters → More computation, risk of overfitting, possible overfitting

General Rule:
 Start with fewer filters (e.g., 32 or 64) in the first layers.

 Increase filters in deeper layers (e.g., 128, 256, 512).


HOW CNN USE FILTERS AT DIFFERENT
LAYERS
2. POOLING LAYER
POOLING LAYER
 A Pooling Layer is used to reduce the size of feature maps while preserving

important information.

 It helps in:

Reducing computation
Making the network more robust to small shifts & distortions
Preventing overfitting

 Types: Max Pooling, Min Pooling and Average Pooling, Global Average Pooling,

Global Max Pooling


1. Max Pooling (Most Common) 2. Min Pooling

 Takes the maximum value in each window. Takes the minimum value in each window.
 Useful in tasks where detecting low-intensity
 Preserves the most important features.
patterns is important.
 Helps in edge detection
3. Average Pooling
 Takes the average value of each window.
 Helps in blurring & smoothing features.

4. Global Pooling (Average and Max)


 Reduces an entire feature map to a single value.
 Often used before fully connected layers.
 Helps in reducing parameters.
4. FLATTEN LAYER
FLATTEN layer
 The Flattening layer is used to convert a multi-

dimensional feature map (output of convolutional or


pooling layers) into a 1D vector, which can then be
fed into a fully connected (dense) layer for
classification.

• Bridges Convolutional & Fully Connected


Layers – CNNs process spatial features using
convolutional layers, but classification requires a
standard 1D input.
FULLY CONNECTED LAYER
 A fully connected (FC) layer is the final stage of a Convolutional Neural Network

(CNN) before making a prediction. It connects every neuron from the previous layer
to every neuron in the next layer, just like in an Artificial Neural Network (ANN).

 The FC layer helps to map the representation between the input and the output.

 Flattens Features; Learns Complex Patterns; Performs Classification


Problem-1

1. Calculate the output size of each layer.


2. Compute the number of trainable parameters in each
layer.
CALCULATING TOTAL NO: OF PARAMETERS

tep 1: First Conv2D Layer


Step 2: First MaxPooling2D Layer
Step 3: Second Conv2D Layer
Step 4: Second MaxPooling2D Layer

Step 5: Flatten Layer


Step 6: Fully Connected Dense Layer

Step 7: Dropout Layer


Step 8: Output Dense Layer
Problem-2
Problem-3
model = [Link]([
layers.Conv2D(filters=6, kernel_size=(5,5), strides=1, padding="valid", activation='relu',
input_shape=(32, 32, 3)),
layers.AveragePooling2D(pool_size=(2,2), strides=2, padding="valid"),

layers.Conv2D(filters=16, kernel_size=(5,5), strides=1, padding="valid", activation='relu'),


layers.AveragePooling2D(pool_size=(2,2), strides=2, padding="valid"),

layers.Conv2D(filters=120, kernel_size=(5,5), strides=1, padding="valid", activation='relu'),

[Link](),
[Link](units=84, activation='relu'),

[Link](units=10, activation='softmax')
])
[Link]()
Steps to Build a CNN Model

1. Import Required Libraries


• Use deep learning frameworks like TensorFlow/Keras or PyTorch.

import tensorflow as tf
from [Link] import datasets, layers, models

2. Load and Preprocess the Dataset


• Load dataset (e.g., CIFAR-10, MNIST, or your custom dataset).
• Normalize pixel values (0–255 → 0–1).
• Convert labels into categorical (one-hot encoding).

(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()


x_train, x_test = x_train / 255.0, x_test / 255.0
Steps to Build a CNN Model

3. Define CNN Architecture

model = [Link]([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
layers.MaxPooling2D((2,2)),

layers.Conv2D(64, (3,3), activation='relu'),


layers.MaxPooling2D((2,2)),

layers.Conv2D(128, (3,3), activation='relu'),


[Link](),

[Link](128, activation='relu'),
[Link](10, activation='softmax') # 10 classes for CIFAR-10
])
Steps to Build a CNN Model

4. Compile the Model


• Optimizer: Adam/SGD
• Loss Function: Categorical Crossentropy
• Metrics: Accuracy

[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy’])

5. Train the Model


• Feed training data to the model.

history = [Link](x_train, y_train, epochs=10,


validation_data=(x_test, y_test))
Steps to Build a CNN Model

6. Evaluate the Model


• Check accuracy on test data.

test_loss, test_acc = [Link](x_test, y_test, verbose=2)


print("Test Accuracy:", test_acc)

7. Make Predictions

y_pred = [Link](x_test[4:5])
print("Predicted:", y_pred.argmax(axis=1))
print("Actual:", y_test[4:5].flatten())
Steps to Build a CNN Model

8. Improve Model (Optional)


• Add Batch Normalization for stable training.
• Use Data Augmentation (rotation, flips, zoom).
• Try deeper architectures (VGG, ResNet).
• Apply regularization (Dropout, L2).
CNN Architectures

• The ImageNet project is a large visual database designed for use in visual object
recognition software research.
• The Image Net project runs an annual software contest, the Image Net Large Scale
Visual Recognition Challenge(ILSVRC), Where Software Programs Compete to
correctly classify and detect objects and scenes.

Winners:
LeNet-5 (1998)
•Proposed by Yann LeCun (1998) for handwritten digit recognition (MNIST, ZIP codes, bank
checks).
•One of the first CNNs that showed the power of deep learning in computer vision.
•Architecture: 7 layers → Conv → Pool → Conv → Pool → Conv → FC → Output.
•Input size: 32×32 grayscale image, output: 10 classes (digits 0–9).
•Very simple, only ~60k parameters.
•Used tanh/sigmoid, not ReLU.
AlexNet (2012)
•Proposed by Alex Krizhevsky
•Won the ILSVRC-2012
•First CNN trained on large-scale ImageNet dataset (1.2M images, 1000 classes).
•First CNN to show deep learning beats traditional CV.
•From AlexNet onwards, CNNs dominated ILSVRC until transformers came.
•Input size: 227×227×3 RGB images.
•Architecture: 8 layers (5 convolutional + 3 fully connected).
•Used ReLU activation (much faster than sigmoid/tanh).
•Trained using 2 GPUs in parallel (96 filters split across GPUs).
•Applied Dropout (p=0.5) in FC layers to reduce overfitting.
•Used Data augmentation (random crops, horizontal flips, RGB jittering).
•Introduced overlapping max pooling (3×3, stride 2) for better down sampling.
AlexNet (2012)
VGG(Visual Geometry Group) Net
•Proposed by Simonyan & Zisserman (2014) from the Visual Geometry Group (Oxford
University).
•Won 2nd place in ILSVRC-2014 (behind GoogLeNet).
•Most popular versions: VGG16 (16 weight layers) and VGG19 (19 weight layers).
•Input size: 224×224×3 RGB images.
•Uses only 3×3 convolution filters (stride 1) and 2×2 max pooling (stride 2) throughout.
•Depth increases gradually: Conv blocks with 64 → 128 → 256 → 512 filters.
•Fully connected layers: Two layers of 4096 neurons + one final softmax layer (1000 classes
in ImageNet).
•Parameters: Very large — ~138 million in VGG16 (most in FC layers).
•Advantages: Simple, uniform design; excellent for transfer learning and feature
extraction.
•Disadvantages: Computationally expensive, memory-heavy, slower than modern models
(ResNet, EfficientNet).
VGG 16

•Known as VGG16 because it has 16 weight layers (13 convolutional + 3 fully


connected).
•Trained on ImageNet (1.2M images, 1000 classes).
•Still widely used in transfer learning and feature extraction.
•ReLU activation after each conv layer.
VGG 16
VGG 19
•Named VGG19 because it has 19 weight layers (16 convolutional + 3 fully connected).
•Trained on ImageNet (1.2M images, 1000 classes).
•It is deeper than VGG16, but very similar in design.
•VGG19 has ≈ 143 million parameters (slightly more than VGG16’s 138M).
VGG 19
GoogLeNet : Going deeper with Convolutions

• GoogLeNet is a Convolutional Neural Network (CNN) architecture developed by


Google researchers.
• It won the ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014.
• The architecture is deep, yet optimized for speed and performance, which makes it
suitable for large-scale visual recognition tasks.
• It brought forward innovative architectural choices such as 1×1 convolutions, global
average pooling and the Inception module, all aimed at improving depth and
computational efficiency.
• It introduces the Inception module, which performs multiple convolution operations
(1x1, 3x3, 5x5) in parallel, along with max pooling and concatenates their outputs.
Inception block
• The basic convolutional block in GoogLeNet is called an Inception block, stemming
from the meme “we need to go deeper” from the movie Inception.

Parallel computation and concatenation


• All branches (1×1, 3×3, 5×5, pooling) are
computed in parallel.
• Their outputs are concatenated depth-wise to
form a single output feature map.

Dimensionality reduction with 1×1 convolutions


• 1×1 conv layers are used before 3×3 and 5×5
filters to reduce the number of input channels
Filter concatenation in Inception block

•Branch 1 → 128 channels


•Branch 2 → 192 channels
•Branch 3 → 96 channels
•Branch 4 → 64 channels
Now just add channels:

✅ Concatenation output shape =


28 × 28 × 480
Branch 1: convolution operations in Inception block
1×1 conv, 128 filters
28×28×128×(1×1×256)
Branch 2:
1×1 conv, 64 filters
28×28×64×(1×1×256)
3×3 conv, 192 filters
Input = 28 × 28 × 64
28×28×192×(3×3×64)
Branch 3:
1×1 conv, 64 filters
28×28×64×(1×1×256) Branch 4:

5×5 conv, 96 filters 3×3 max pool → no conv


Input = 28 × 28 × 64 ops (just comparisons)

28×28×96×(5×5×64) 1×1 conv, 64 filters


Input = 28 × 28 × 256
28×28×64×(1×1×256)

~271M convolution operations


GoogLeNet architecture

Input Size:
• 224×224×3 RGB images
Initial Convolutions
• Starts with 7×7 convolution + 3×3 max pooling.
• Helps capture low-level features (edges, textures).
Intermediate Convolutions
• Uses 3×3 and 1×1 convolutions to refine features
before inception blocks.
Inception Modules
• There are 9 Inception blocks.
• Multiple (1×1, 3×3, 5×5 convolutions + pooling)
applied in parallel.
• Outputs concatenated along depth.
1×1 Convolutions
• Reduce feature depth before 3×3 & 5×5 filters.
GoogLeNet architecture
Depth of Network
• 22 trainable layers deep
Auxiliary Classifiers
• Two intermediate classifiers attached around the
middle layers.
• prevent vanishing gradients.
• Also act as regularizers to reduce overfitting.
Global Average Pooling (GAP)
• Instead of fully connected layers at the end,
GoogLeNet uses average pooling.
• Reduces parameters drastically and avoids
overfitting.
Final Softmax Layer
• 1000 output neurons (for 1000 ImageNet classes).
• Produces probability distribution.
GoogLeNet
ResNet(Residual Network)

• ResNet is a deep convolutional neural network architecture introduced by


Kaiming He et al. in 2015.
• This architecture introduced the concept called Residual Blocks.
• ResNet is an artificial neural network that introduces a so-called “Skip
connection,” which allows the model to skip one or more layers.
• This approach enables the training of significantly deeper networks by reducing
vanishing gradients, often improving performance on complex computer vision
tasks.
ResNet(Residual Network)
How ResNet Works
• The core innovation of ResNet is the use of skip connections.
• In a traditional Convolutional Neural Network (CNN), each layer feeds its output
directly to the next layer in sequence.
• As the network gets deeper, it becomes increasingly difficult for the network to
learn and for gradients to propagate back during training.
• This can lead to a situation where adding more layers actually degrades the model's
performance.
• ResNet addresses this by allowing the input of a layer (or a block of layers) to be
added to its output.
• If a layer is not beneficial, the network can easily learn to ignore it by driving its
weights toward zero, allowing the identity mapping to be passed through the skip
connection.
ResNet(Residual Network)
regular block residual block

skip connections
ResNet(Residual Network)

• In traditional neural networks, each layer tries to learn some mapping function, say
f(x), from the input x.
• In ResNet, instead of directly learning f(x), the network learns the residuals.
• This means the network learns:
f(x) = g(x) + x
• f(x) is the output of the block.
• g(x) is the transformation applied by the layers (convolution, activation, etc.)
• x is the input passed along the skip connection.
• This equation shows that even if g(x) becomes zero, f(x) will still carry the identity
information from x.
• This helps make sure that the network is learning effectively at all times, even if
deeper layers are initially underperforming.
ResNet(Residual Network)

skip connection
• The residual block takes the input and adds it directly to the output after it has
passed through a few layers. This is what we call a skip connection.
Identity Mapping: Skip connections in ResNet create a kind of identity mapping where,
if the additional layers can’t contribute, the network still retains the initial identity, by
skipping those layers.
Preventing Degradation: The degradation problem—where adding more layers makes
performance worse—is also solved with skip connections.
Types of ResNet

• There are multiple versions of ResNet Architecture, each with a different number of
layers:
ResNet-18: A smaller model with 18 layers, used for less complex tasks.
ResNet-34: Similar in size, with a few more layers for better accuracy.
ResNet-50: A popular version, consisting of 50 layers, known for being powerful yet
manageable.
ResNet-101: Contains 101 layers and is used for more complex tasks with high-
dimensional data.
ResNet-152: A deeper version with 152 layers, designed for very sophisticated tasks,
showing the real power of deep learning.
ResNet-34 architecture
Transfer learning

• Transfer learning is a deep learning technique where a model trained on one task
(source task) is reused or adapted for a another but related task (target task).
• Instead of training a neural network from scratch, we transfer the "knowledge"
(weights and features) learned in a pre-trained model.

• The general idea is to use the knowledge a


model has learned from a task with a lot of
available labeled training data in a new task
that doesn’t have much data.
Why Transfer Learning?

Faster Training – avoids training millions of parameters from scratch.


To save time and resources:
• Training a deep learning model from scratch can be time-consuming and
computationally expensive.
• Transfer learning can help you save time and resources by starting with a model
that has already been trained on a large dataset.
To improve model performance:
• Transfer learning can help you improve the performance of your model by
transferring the knowledge that the pre-trained model has learned about the
features of the data.
• This can be especially helpful if you have limited data for your target task.
Types of Transfer Learning
• Transfer learning can be classified in several ways depending on what we
transfer and how similar the source and target tasks/domains are.

1. Feature Extraction
2. Fine-Tuning
Feature Extraction using Transfer Learning
• In feature extraction, the pre-trained model is used to extract features from the data.
• These features are then used to train a new model on the target task.
• In this approach, the pre-trained model acts as a fixed feature extractor.
• The convolutional layers are frozen, meaning their weights don’t change during
training.
• Only the final classification layers are added and trained on the new dataset.
• This method works well when you have a small dataset and the new task is similar to
the original task.

• The key idea here is to leverage the pre-trained


model's weighted layers to extract features, but
not update the model's weights during training
with new data for the new task.
Feature Extraction using Transfer Learning
Steps:
1. Load a pre-trained model (e.g., ResNet, MobileNet, BERT).
2. Remove its final classification head.
3. Freeze all layers (so they don’t update).
4. Add a new classifier for your dataset.
5. Train only the classifier.
Feature Extraction using Transfer Learning

Step 1: Select a Pre-trained Model


• Choose a model already trained on a large dataset (e.g., ResNet, VGG, MobileNet,
BERT).
These models have already learned general features:
images: edges, textures, shapes.
text: word embeddings, sentence representations.
Step 2: Remove the Pre-trained Classifier
• Pre-trained models usually have a fully connected (Dense) classifier for their original
task.
• Example: ResNet trained on ImageNet (1000 classes) have a 1000-way classifier.
• Since your task may have different classes (say, 10 classes), you remove the original
classifier head.
Feature Extraction using Transfer Learning
Step 3: Freeze Pre-trained Layers
• Freeze all layers so their weights don’t change during training.
• This ensures the model works as a fixed feature extractor.
• You’re only reusing the “knowledge” it already learned.
Step 4: Add a New Classifier
• Add one or more Dense (Fully Connected) layers on top of the frozen base model.
• This classifier is trained specifically for your dataset.
• Example: A Dense(10, softmax) for a 10-class classification problem.
Step 5: Train Only the Classifier
• Train the model, but only the classifier’s parameters update.
• The frozen base model just generates features.
Step 6: Evaluate and Use
• After training, the new model can classify based on features from the pre-trained
model.
• You get good accuracy, fast training, and reduced risk of overfitting.
Feature Extraction using Transfer Learning
import tensorflow as tf
from [Link] import ResNet50
from [Link] import Dense, GlobalAveragePooling2D, Input
from [Link] import Model
from [Link] import to_categorical
# Step 1: Load MNIST dataset
(x_train, y_train), (x_test, y_test) = [Link].load_data()

# Expand grayscale to 3 channels (28x28 → 28x28x3)


x_train = [Link].grayscale_to_rgb(tf.expand_dims(x_train, -1))
x_test = [Link].grayscale_to_rgb(tf.expand_dims(x_test, -1))

# Resize images to 224x224 for ResNet50


x_train = [Link](x_train, (224,224))
x_test = [Link](x_test, (224,224))
# Normalize pixel values [0,1]
x_train = x_train / 255.0
x_test = x_test / 255.0
# One-hot encode labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
Feature Extraction using Transfer Learning
# Step 2: Load Pretrained ResNet50 (without top)
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))

# Freeze base layers


base_model.trainable = False

# Step 3: Add custom classifier


x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(128, activation='relu')(x)
output = Dense(10, activation='softmax')(x) # 10 digits

model = Model(inputs=base_model.input, outputs=output)

# Step 4: Compile Model


[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Step 5: Train
history = [Link](x_train, y_train, validation_split=0.1, epochs=3, batch_size=32)

# Step 6: Evaluate
loss, acc = [Link](x_test, y_test)
print(f"Test Accuracy: {acc:.2f}")
Fine-Tuning

• Fine-tuning is the process of taking a pre-trained model trained on a large dataset


like ImageNet and adapting it to a new task or dataset by training it further.
• Fine-tuning refers to the process of taking a pre-trained model, which has already
learned from a large dataset, and making small adjustments to better suit a specific
task or dataset.
• fine-tuning refers to using the weights of an already trained network as the starting
values for training a new network
• Taking a pre-trained model, replacing its head. Then training both the new head and
a portion (or all) of the pre-trained model’s layers with a very small learning rate.
• The task is related but not identical to the pre-trained model’s original task; you
want to achieve higher performance than simple feature extraction.
Fine-Tuning

Steps:
1. Pick a pretrained model
2. Replace its output layer
3. Freeze earlier layers
4. Train new head
5. Slowly unfreeze more layers
6. Use a small learning rate
7. Train & evaluate
Fine-Tuning

Choose a Pre-Trained Model


• Select a model that was trained on a very large and general dataset, ideally for a
task somewhat similar to yours.
Modify the Output Layer
• The original pre-trained model’s final layer (or “head”) is designed for its original
task (e. G. , classifying 1000 ImageNet categories).
• You’ll typically remove this layer and replace it with new layers tailored to your
specific number of output classes or prediction type.
Freeze Early Layers (Feature Extraction Phase)
• In the initial phase of fine-tuning, it’s common practice to “freeze” the weights of
the majority of the pre-trained model’s layers, especially the earlier ones.
• Because these early layers have learned highly generic and useful features (like
edges and textures in images) that are likely relevant to almost any similar task.
Fine-Tuning

Unfreeze Some Layers (Gradual Fine-Tuning)


• Deeper layers capture task-specific features (faces, objects in images).
• Unfreeze some deeper layers and train them as well.
Set Lower Learning Rate
• Fine-tuning requires small adjustments, not drastic weight updates.
• Use a smaller learning rate than usual (e.g., 10x smaller).
• Example: If original training used lr=0.001, fine-tuning may use lr=0.0001.
Train the Model
• Start by training only the new classification head.
• Then fine-tune selected layers with a smaller learning rate.
Evaluate and Adjust
• Evaluate on validation data.
• Adjust which layers to unfreeze, batch size and learning rate.
Fine-Tuning
# 2. Load Pretrained ResNet50 (without top classifier layer)
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
# 3. Freeze base model initially (feature extraction mode)
base_model.trainable = False
# 4. Add new classification head
model = [Link]([
base_model,
layers.GlobalAveragePooling2D(),
[Link](256, activation='relu'),
[Link](0.5),
[Link](num_classes, activation='softmax')
])

# 5. Compile model
[Link](optimizer=[Link](learning_rate=1e-3),
loss='categorical_crossentropy', metrics=['accuracy'])

# 6. Train only the new head


history = [Link](x_train_resized, y_train,
validation_data=(x_test_resized, y_test),
epochs=5, batch_size=32)
Fine-Tuning
# 7. Unfreeze some layers for fine-tuning
base_model.trainable = True

# Freeze earlier layers, unfreeze last few blocks


for layer in base_model.layers[:-10]: # unfreezing last 10 layers
[Link] = False

# Re-compile with lower learning rate for fine-tuning


[Link](optimizer=[Link](learning_rate=1e-5),
loss='categorical_crossentropy',
metrics=['accuracy'])

# 8. Fine-tune the model


history_finetune = [Link](x_train_resized, y_train,
validation_data=(x_test_resized, y_test),
epochs=5, batch_size=32)
# 9. Evaluate final model
test_loss, test_acc = [Link](x_test_resized, y_test, verbose=2)
print(f"Test Accuracy after fine-tuning: {test_acc:.4f}")
Concept Description When to Use Pros Cons
Potentially achieves
Building a neural the absolute best
You have a very Requires massive
network model from performance if data
large, diverse. Well- datasets, significant
Training from random initialization is abundant and
labeled dataset; the computational
Scratch and training all its compute is
task is entirely resources, long
parameters on your unlimited; full
novel. training times.
specific dataset. control over model
architecture.
Using a pre-trained
May not achieve
model as a fixed
Your dataset is small Fastest and simplest optimal performance
feature extractor.
Feature Extraction to medium-sized; form of transfer if the new task’s
The pre-trained
(using a Pre- the task is similar to learning; features differ
layers are frozen.
trained Model) the pre-trained significantly reduces significantly from
Only a new
model’s original data needs;. what the pre-trained
classification head is
model learned;
trained on your data.
More complex than
Your dataset is
Taking a pre-trained pure feature
medium to large; the Optimal balance of
model, replacing its extraction; requires
task is related but performance, data
head. Then training careful
not identical to the efficiency. Training
both the new head hyperparameter
pre-trained model’s time; highly
Fine-Tuning and a portion (or all) tuning (especially
original task; you adaptable to specific
of the pre-trained learning rate);
want to achieve tasks; leverages
model’s layers with higher risk of
higher performance existing knowledge
Analysis of pre trained models
Parameter No. of
Network Year Salient Feature Accuracy FLOPs
s Layers
AlexNet 2012 First deep CNN for ImageNet 84.7% 62M 1.5B 8
VGG-16 2014 Fixed-size kernels (3×3) 92.3% 138M 19.6B 16
Inception-v3 2014 Wider, parallel kernels 93.3% 6.4M 2B 48
Shortcut (residual)
ResNet-152 2015 95.5% 60.3M 11B 152
connections
DenseNet-
2016 Dense skip connections 95.6% 20M 4.3B 201
201
MobileNet- Depthwise separable convs
2018 90.3% 3.4M 0.3B 53
v2 (lightweight)
EfficientNet- Compound scaling (depth,
2019 97.0% 66M 37B 813
B7 width, res.)
24
Vision
Transformer-based vision (Transform
Transformer 2020 97.2% 307M ~55B
model er encoder
(ViT-L/16)
layers)
Normalization

• Data normalization is a crucial preprocessing step in deep learning that transforms


the range of features to a standard scale typically between 0 and 1.
• This is achieved by adjusting each feature's values based on its minimum and
maximum values.
• This process ensures that all features contribute equally to the model's learning
process, preventing features with larger scales from dominating the learning
process.
why normalization is needed in deep learning
• Improved Model Performance
• Stabilizes Training
• Faster Convergence
• Better Generalization
• Acts as a Regularizer
Normalization Techniques
Batch Normalization

• Batch Norm is a normalization technique done between the layers of a Neural


Network instead of in the raw data.
• It is done along mini-batches instead of the full data set.
• It serves to speed up training and use higher learning rates, making learning easier.

Where It’s Applied


•Usually applied after convolution layers and before activation functions (e.g., ReLU).
•Works best in CNNs with large mini-batches.
How it works
How it works
Data Augmentation

• Dataset augmentation is a technique used in machine learning and deep learning to


artificially increase the size and diversity of a dataset by applying various
transformations to the original data.
• In CNNs, data augmentation is a technique to artificially expand the training dataset by
creating modified versions of images.

When should you use data


augmentation?
[Link] prevent models from overfitting.
[Link] initial training set is too small.
[Link] improve the model accuracy.
[Link] Reduce the operational cost of
labeling and cleaning the raw dataset.
Data Augmentation Techniques for Images

1. Geometric Transformations
Rotation: rotate image by a random angle (e.g., ±30°).
Translation (Shift): move the image horizontally or vertically.
Flipping: horizontal or vertical flip.
Scaling / Zoom: zoom in/out randomly.
Shearing: slanting the image (like italic text).
Cropping / Random Crop: cut random parts of an image.
2. Color & Intensity Transformations
Brightness Adjustment: simulate different lighting conditions.
Contrast Adjustment: vary sharpness between light and dark regions.
Saturation & Hue: change color richness and tone.
Color Jitter: randomly changes brightness, contrast, saturation, hue together.
Grayscale Conversion: removes color → useful for reducing complexity.
Data Augmentation Techniques for Images
from [Link] import ImageDataGenerator

datagen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
brightness_range=[0.5,1.5]
)

# Generate augmented images


aug_iter = [Link](img, batch_size=1)

[Link](figsize=(10,5))
for i in range(4):
batch = next(aug_iter)
[Link](1,4,i+1)
[Link](batch[0].astype("uint8"))
[Link]("off")
[Link]()
rules update in CNN
• rules update in CNN", referring to how weights and parameters get updated during
training in a Convolutional Neural Network (CNN)
rules of update in CNN training:
optimization and loss functions in classifiers

loss functions: Refer Module-2 slides from 18-19

Optimization: Refer Module-2 slides from 49-58


Dropout: Refer Module-2 slide 47

You might also like