Pretrained Models • Feature Extraction vs. Fine-Tuning • Applications in Vision and NLP
Transfer Learning is one of the most powerful techniques in deep learning.
It allows us to use knowledge learned from one task and apply it to another task.
This makes training faster, easier, and much more accurate — especially when we have limited data.
In this chapter, you will learn about pretrained models, feature extraction vs fine-tuning, and how Transfer Learning is used in computer vision and natural language processing (NLP).
1. What Is Transfer Learning?
Transfer Learning means taking a model that has already learned useful features from a large dataset, and using it for a new task.
Simple Example
Imagine you know how to ride a bicycle.
Learning to ride a motorcycle becomes easier because you already understand:
- Balance
- Steering
- Movement
Similarly, a deep learning model trained on millions of images already knows:
- Edges
- Shapes
- Textures
- Patterns
So it can learn new tasks more quickly.
2. Pretrained Models
A pretrained model is a neural network that has already been trained on a huge dataset (like ImageNet with 14 million images).
Popular Pretrained Models
- VGG16 / VGG19
- ResNet50, ResNet101
- InceptionV3
- MobileNet, MobileNetV2
- BERT (for NLP)
- GPT (for text generation)
These models have learned very strong features that can be reused.
Why Pretrained Models Are Useful
- Save time (no need to train from scratch)
- Require less data
- Achieve high accuracy quickly
- Useful for beginners and experts
3. Feature Extraction vs. Fine-Tuning
Transfer Learning can be done in two main ways.
A. Feature Extraction
In this method:
- We freeze the pretrained model
- We only use it to extract features
- We add a new classifier layer for our task
Simple Explanation
The model becomes a "smart camera lens" that extracts features.
You only train the last part (classifier).
When to Use
- When you have small datasets
- When training time must be short
Code Example (Keras)
from tensorflow.keras.applications import VGG16
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Load pretrained model (freeze layers)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False
model = Sequential([
base_model,
Flatten(),
Dense(64, activation='relu'),
Dense(3, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())B. Fine-Tuning
In this method:
- We unfreeze some layers of the pretrained model
- Train them again on the new dataset
- The model learns features specific to your task
Simple Explanation
Feature extraction = using old knowledge
Fine-tuning = improving old knowledge based on new experiences
When to Use
- When you have medium-sized datasets
- When you need higher accuracy
Code Example
base_model.trainable = True # unfreeze some layers
model.compile(
optimizer=tf.keras.optimizers.Adam(1e-5),
loss='categorical_crossentropy',
metrics=['accuracy']
)4. Applications of Transfer Learning in Vision
Transfer Learning is extremely useful in computer vision, where training from scratch requires millions of images.
Examples
A. Medical Imaging
Pretrained CNNs detect:
- Tumors
- Lung infections
- Retina blood vessel issues (used in diabetic retinopathy detection)
B. Object Detection
Models detect:
- Cars
- People
- Traffic signs
- Animals
C. Face Recognition
Used in:
- Phones
- Security cameras
- Attendance systems
D. Agriculture
Identify:
- Diseased plants
- Crop types
- Soil quality
Why It Works So Well
Image features like edges and textures are universal.
So a model trained on millions of natural images can detect important features even in new applications.
5. Applications of Transfer Learning in NLP
Transfer Learning changed the entire field of Natural Language Processing.
Today's most powerful language models use Transfer Learning, especially Transformer models.
Examples
A. Text Classification
Using pretrained BERT or DistilBERT to:
- Classify tweets
- Detect spam
- Analyze customer reviews
B. Question Answering
Models like BERT answer questions from articles or books.
C. Translation
Pretrained Transformer models translate between:
- English ↔ Urdu
- English ↔ Arabic
- English ↔ Chinese
D. Chatbots & Assistants
Models such as GPT learn from billions of sentences and can:
- Write essays
- Answer questions
- Generate code
Why Transfer Learning Works in NLP
Basic language patterns (grammar, structure, meaning) are universal.
Pretrained models learn these patterns and apply them to new tasks easily.
6. Benefits of Transfer Learning
| Benefit | Explanation |
|---|---|
| Faster training | You skip learning basic features |
| Higher accuracy | Model already has strong knowledge |
| Less data needed | Even small datasets become useful |
| Works with limited hardware | No need for huge GPUs |
| Useful in real-world apps | Vision, NLP, audio, robotics |
7. Simple Complete Example (Vision Transfer Learning)
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.applications import MobileNetV2
# Load pretrained mobile-friendly model
base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
base.trainable = False # feature extraction
model = Sequential([
base,
Flatten(),
Dense(32, activation='relu'),
Dense(2, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()