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.
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.
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.
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
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.
A pretrained neural network works in a similar way. It already contains useful knowledge that can be adapted to another task.
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.
The model must learn useful visual features and the new classification task.
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
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.
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.
Instead of starting from:
Random Weights
↓
No learned features
↓
Start learning
we start from:
Pretrained Weights
↓
Useful learned features
↓
Adapt to the new task
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()
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.
Training From Scratch vs Transfer Learning
The model must learn useful features and the new task from the beginning.
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.
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.
Check Your Understanding
base_model.trainable = False freezes
the pretrained model's weights during training.