DEEP LEARNING • LESSON 14

What Is Transfer Learning?

Transfer Learning is a technique where we take knowledge learned by an existing model and reuse it for a new task. Instead of training a deep learning model completely from scratch, we start with a pretrained model and adapt it to our problem.

THE SIMPLEST DEFINITION

Transfer Learning means reusing what a model has already learned.

A pretrained model has already learned useful patterns from a large dataset. We can reuse those patterns and teach the model how to solve a new, related problem.

01

Why Do We Need Transfer Learning?

Training a deep learning model from scratch can require a large amount of data, computing power, and training time.

Imagine that we want to build an image classifier. A neural network needs to learn many basic visual patterns before it can understand the actual objects in an image.

Training from scratch means the model starts with random weights and has to learn everything from the beginning.

Transfer Learning gives us a better starting point:

Random Model
      ↓
Learn edges
      ↓
Learn shapes
      ↓
Learn textures
      ↓
Learn objects
      ↓
Learn the new task

With Transfer Learning:

Pretrained Model
      ↓
Already learned useful features
      ↓
New Dataset
      ↓
Learn the new task
02

Think of It Like Human Learning

Transfer Learning becomes easier to understand if we compare it with how humans learn.

Suppose you already know how to identify common animals. You understand things like shapes, legs, eyes, fur, ears, and body structure.

If you now need to learn how to distinguish between two specific dog breeds, you don't start learning what an animal is from zero.

Existing Knowledge New Problem Faster Learning

A pretrained neural network works in a similar way. It already contains useful knowledge that can be adapted to another task.

03

Example — Cat and Dog Classification

Suppose we want to build a model that predicts whether an image contains a cat or a dog.

A model trained from scratch has to learn many visual patterns before it can recognize the animals.

TRAIN FROM SCRATCH Start with random weights

The model must learn useful visual features and the new classification task.

TRANSFER LEARNING Start with pretrained weights

The model can reuse visual features it has already learned.

For example, a pretrained image model may already know how to detect edges, curves, textures, and shapes.

Pretrained Image Model
        ↓
Edges
Shapes
Textures
Patterns
        ↓
Cat / Dog Dataset
        ↓
New Classification Layer
        ↓
Cat or Dog
04

Example — Plant Disease Detection

Suppose we want to build a model that detects whether a plant leaf is healthy or diseased.

We could train a large neural network from scratch, but that would require a lot of data and computation.

Instead, we can start with a pretrained image model.

Pretrained CNN
       ↓
Already knows visual patterns
       ↓
Plant Leaf Images
       ↓
New Classification Layer
       ↓
Healthy / Diseased

The model does not need to relearn basic visual concepts from zero. We mainly need to adapt it to the new task.

05

What Is a Pretrained Model?

A pretrained model is a model that has already been trained on a large dataset.

During that training, the model learns useful patterns and stores them in its weights.

Pretrained model = a model that already has learned useful knowledge from previous training.

Instead of starting from:

Random Weights
      ↓
No learned features
      ↓
Start learning

we start from:

Pretrained Weights
      ↓
Useful learned features
      ↓
Adapt to the new task
06

Transfer Learning With Python

TensorFlow and Keras provide many pretrained models. One example is MobileNetV2.

We can load MobileNetV2 with weights learned from ImageNet and use it as the starting point for our own model.

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.applications import MobileNetV2


# Load a pretrained model
base_model = MobileNetV2(
    weights="imagenet",
    include_top=False,
    input_shape=(224, 224, 3)
)


# Freeze the pretrained model
base_model.trainable = False


# Add our own layers
model = keras.Sequential([
    base_model,

    layers.GlobalAveragePooling2D(),

    layers.Dense(
        128,
        activation="relu"
    ),

    layers.Dense(
        1,
        activation="sigmoid"
    )
])


model.summary()
07

Understand the Python Code

Loading the Pretrained Model

base_model = MobileNetV2(
    weights="imagenet",
    include_top=False,
    input_shape=(224, 224, 3)
)

weights="imagenet" tells Keras to load weights that were already learned from the ImageNet dataset.

This is the main idea behind Transfer Learning: we are not starting from random weights.

Freezing the Model

base_model.trainable = False

This tells TensorFlow not to change the pretrained model's weights while we train the new layers.

Think of it as saying:

Keep the knowledge you already learned.
Teach only the new task.

Adding Our Own Layers

layers.GlobalAveragePooling2D(),

layers.Dense(
    128,
    activation="relu"
),

layers.Dense(
    1,
    activation="sigmoid"
)

These layers are added for our new classification task.

08

Training From Scratch vs Transfer Learning

FROM SCRATCH Random weights

The model must learn useful features and the new task from the beginning.

TRANSFER LEARNING Pretrained weights

The model starts with useful learned features and adapts them to the new task.

Transfer Learning is especially useful when your new dataset is relatively small and a suitable pretrained model already exists.

REMEMBER THIS

Don't teach the model everything again if it already knows something useful.

Transfer Learning starts with a pretrained model, reuses its learned features, and adapts those features to a new task. This can reduce training time, data requirements, and computational cost compared with training a model completely from scratch.

QUICK CHECK

Check Your Understanding

What is Transfer Learning? Reusing knowledge learned by a pretrained model for a new task.
What is a pretrained model? A model that has already been trained on a dataset and has learned useful patterns.
Why is Transfer Learning useful? It can reduce the amount of data, training time, and computing resources needed.
What does this code do? base_model.trainable = False freezes the pretrained model's weights during training.
LESSON 14 • TRANSFER LEARNING

Next: Why Transfer Learning Is Useful

Now that you understand the basic idea of Transfer Learning, the next lesson explains when and why it is useful in real-world deep learning projects.