Pre-trained models: VGG, ResNet, etc.
Description
Pre-trained models like VGG, ResNet, Inception, and MobileNet are deep neural networks that have already been trained on large datasets such as ImageNet
. These models can be reused for similar tasks through a process called Transfer Learning, where the learned features are repurposed instead of training a model from scratch.
Fine-tuning takes transfer learning further by allowing some layers (typically the last few) to be retrained on the new task, allowing the model to adapt better to the target dataset.
Transfer learning significantly reduces training time and improves performance, especially when the target dataset is small.
Popular pre-trained models:
- VGG: Deep and simple architecture using only 3x3 convolutions
- ResNet: Introduces residual connections (skip connections) to train very deep networks
- Inception: Uses multi-scale convolutions in parallel paths for better feature extraction
- MobileNet: Lightweight model optimized for mobile and embedded devices
Examples
Using a pre-trained ResNet50 model with fine-tuning in Keras:
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
# Load the ResNet50 model without the top classification layer
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze initial layers for feature extraction
for layer in base_model.layers[:100]:
layer.trainable = False
# Add custom classification head
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(128, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# Final model
model = Model(inputs=base_model.input, outputs=predictions)
# Compile
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
This example shows partial fine-tuning of ResNet50. For full fine-tuning, unfreeze more layers depending on your dataset and compute resources.
Real-World Applications
Image Classification
Transfer learning using models like VGG or ResNet helps classify objects in images with high accuracy and speed.
Medical Diagnosis
Fine-tuned models can classify medical scans for early disease detection (e.g., tumors, pneumonia).
Object Detection
Backbones like ResNet are used in object detection models such as Faster R-CNN and YOLO.
Mobile AI
Lightweight models like MobileNet are ideal for on-device inference in mobile and IoT applications.
Resources
Recommended Books
- Deep Learning by Ian Goodfellow et al.
- Deep Learning with Python by François Chollet
- CS231n Notes: Transfer Learning
Interview Questions
What is transfer learning and how is it used?
Transfer learning is a technique where a model trained on one task is repurposed for a different but related task. For example, models trained on ImageNet can be fine-tuned for medical image classification or face recognition with fewer labeled examples.
What are the benefits of using pre-trained models?
- Reduced training time
- Better generalization with fewer data
- Leverages state-of-the-art research models
- Useful when computational resources are limited
When should you fine-tune a pre-trained model instead of freezing it?
Fine-tuning is preferred when the target dataset is large enough and significantly different from the source dataset. This allows the model to adapt more precisely to the new task.
How do you choose which layers to fine-tune?
Generally, the last few layers closer to the output are fine-tuned because they are more task-specific. The earlier layers are frozen as they capture general features like edges and textures.