Course Content
Transfer Learning
Leveraging pre-trained models for specialized tasks
Introduction
Transfer learning allows you to leverage pre-trained models trained on large datasets (like ImageNet) to solve new tasks with less data and compute.
Instead of training a model from scratch, you reuse learned features, which:
β
Reduces training time.
β
Improves model performance on smaller datasets.
β
Requires fewer computational resources.
Why Use Transfer Learning?
- Training deep networks from scratch requires large labeled datasets and high compute.
- Pre-trained models capture generic patterns (edges, textures) useful for many tasks.
- Transfer learning is widely used in computer vision and NLP applications.
Approaches to Transfer Learning
1οΈβ£ Feature Extraction: Use the pre-trained model as a fixed feature extractor, replacing the top layer(s) with your classifier.
2οΈβ£ Fine-Tuning: Unfreeze some deeper layers and retrain them on your dataset to adapt learned features.
Example: Transfer Learning for Image Classification with Keras
We will use MobileNetV2, a lightweight pre-trained CNN, to classify images on a custom dataset.
1οΈβ£ Install and Import Libraries
pip install tensorflow
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
2οΈβ£ Load and Prepare Data
Prepare your dataset using ImageDataGenerator
for augmentation and rescaling.
train_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_generator = train_datagen.flow_from_directory(
'path_to_dataset',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='training'
)
val_generator = train_datagen.flow_from_directory(
'path_to_dataset',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='validation'
)
3οΈβ£ Load Pre-Trained Model
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze base model layers
base_model.trainable = False
# Add custom top layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
outputs = Dense(train_generator.num_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=outputs)
4οΈβ£ Compile and Train the Model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(
train_generator,
epochs=10,
validation_data=val_generator
)
5οΈβ£ Fine-Tuning (Optional)
After initial training, you can unfreeze some layers and continue training with a low learning rate for fine-tuning.
base_model.trainable = True
model.compile(optimizer=tf.keras.optimizers.Adam(1e-5),
loss='categorical_crossentropy',
metrics=['accuracy'])
history_finetune = model.fit(
train_generator,
epochs=5,
validation_data=val_generator
)
Conclusion
β
Transfer learning allows efficient use of pre-trained models for new tasks.
β
It helps you build performant models even with limited data and compute.
β
You can implement feature extraction and fine-tuning using Keras easily.
Whatβs Next?
β
Experiment with other pre-trained models like ResNet, Inception, or EfficientNet.
β
Apply transfer learning for NLP tasks using models like BERT.
β
Integrate transfer learning into your pipelines for real-world projects.
Join our SuperML Community to share your transfer learning projects and continue growing as a deep learning practitioner.
Happy Learning! π