Course contentsShow
← Machine Learning and Deep Learning
Lesson 944 of 3,53822. Computer Vision TasksFree lesson

Implementing Transfer Learning in PyTorch

Loading pretrained weights, modifying final layers, and setting up training loops for fine-tuning.

Implementing Transfer Learning in PyTorch

You'll learn how to load a pretrained model, modify its final layer, and set up training to adapt it to your custom task.

The Core Idea

Transfer learning means starting with a model trained on millions of images (like ImageNet) and adapting it to your problem. In PyTorch, this involves three key steps: loading pretrained weights, replacing the classification head to match your number of classes, and setting up a training loop that respects which layers you want to update.

Think of it like hiring an experienced chef (pretrained model) to work in your new restaurant. The chef already knows cooking fundamentals—you just need to teach them your specific menu (final layer) and recipes (fine-tuning).

Loading Pretrained Weights

PyTorch's torchvision.models provides pretrained architectures:

import torch
import torch.nn as nn
from torchvision import models

# Load ResNet with ImageNet weights
model = models.resnet50(pretrained=True)

Modifying the Final Layer

The pretrained model outputs 1000 classes (ImageNet). Replace this to match your task:

# ResNet's final layer is called 'fc'
num_classes = 10  # Your custom dataset
model.fc = nn.Linear(model.fc.in_features, num_classes)

Setting Up the Training Loop

Freeze early layers for feature extraction, or train all layers for full fine-tuning:

# Freeze all layers except the final one
for param in model.parameters():
    param.requires_grad = False

# Unfreeze final layer
for param in model.fc.parameters():
    param.requires_grad = True

# Use a smaller learning rate when fine-tuning
optimizer = torch.optim.Adam(model.fc.parameters(), lr=0.001)

Now train as usual with your data, loss function, and optimizer.

Key Takeaway: Transfer learning in PyTorch requires loading pretrained weights, replacing the final classification layer to match your task, and carefully choosing which parameters to train during fine-tuning.