Transfer Learning with Pre-trained Models
Keras Basics
2 min read
Published Nov 17 2025
Guide Sections
Guide Comments
Transfer learning is one of the most powerful techniques in modern deep learning.
Instead of training a model from scratch on your dataset, you:
- Take a model pre-trained on a huge dataset (ImageNet)
- Freeze its convolutional base
- Add your own classifier layer(s)
- Train for a short time
- (Optional) fine-tune deeper layers
Benefits:
- Much higher accuracy
- Works with small datasets
- Faster training
- Lower compute cost
In this section we apply transfer learning to Cats vs Dogs using TensorFlow Datasets
Load the Cats vs Dogs Dataset
TensorFlow Datasets (TFDS) provides cats_vs_dogs fully prepared for use.
Install TFDS if needed:
Load dataset:
Labels:
- 0 = cat
- 1 = dog
Build Input Pipeline
Transfer learning models expect consistent image size, so resize all images.
This pipeline:
- Resizes images
- Normalises pixels
- Batches data
- Prefetches for speed
Load a Pre-trained Base Model (MobileNetV2)
We use MobileNetV2 (fast, accurate, lightweight):
Freeze it:
This makes it a fixed feature extractor.
Add Your Own Classification Head
Typical transfer-learning head:
Why this design?
- GlobalAveragePooling2D → reduces feature map dimensions without dense layers
- Dropout → reduces overfitting
- Sigmoid → output probability of "dog"
Compile the Model
Use a small learning rate:
Train the Frozen Model (Feature Extraction Phase)
Typical accuracy: 93–96% after just a few epochs, this is the power of transfer learning.
Fine-Tuning for Extra Accuracy
Once the head is trained, unfreeze part of the base model:
Unfreeze only the newest convolutional layers (recommended):
Recompile with a very low learning rate:
Train again:
Fine-tuning often boosts accuracy by 1–3%.
Data Augmentation (Highly Recommended)
Adding augmentation improves generalisation:
Use it at the start of your model:
Using Other Pre-trained Keras Models
You can replace MobileNetV2 with any Keras Application model:
All follow the same pattern:
include_top=Falseweights="imagenet"- Freeze
- Add classifier head
- Train
- Optionally fine-tune














