Pretrained knowledge, selective adaptation

Transfer Learning: Choose What Learns

Freeze a real ResNet, choose the fine-tuning depth and see every trainable parameter. Turn your choices into PyTorch code.

12 min to exploreMachine Learning · Computer Vision · Computer Vision with PyTorch
01 / UNDERSTAND

How much of a pretrained network should your task be allowed to change?

A pretrained feature extractor can support a new classifier. Fine-tuning allows selected weights to adapt too. Trainable parameter counts, BatchNorm behavior and validation evidence help you describe and evaluate the configuration.

02 / EXPLORE

Make a prediction.
Then move a control.

The fine-tuning studio

Keep the knowledge.
Choose what can change.

Freeze and unfreeze a real ResNet, then count exactly what your optimizer can update.

Exact architecture counts
Trainable parameters1,539Eligible for optimizer updates
Frozen parameters11,176,512Weights still used in the forward pass
Trainable share0.014%Of 11,178,051 total parameters
Blue · trainableGray · frozen

The same forward path. A different update plan.

Choose which parameter groups can learn.

ResNet-18 · 224 × 224 RGB
Global average pooling: spatial features → one value per channel. Zero learned parameters.

Every stage processes the image. Only selected groups can update their parameters.

Training a small classifier can reuse millions of pretrained feature-extractor parameters.

BatchNorm running statistics stay fixed in this configuration.

Inspect the parameter accounting

Convolution weights + BatchNorm affine parameters + projection shortcuts + classifier. Running statistics are excluded. Torchvision ResNets use no convolution biases and include a classifier bias.

Exact counts for the currently selected architecture and class count
GroupMain conv weightsMain BN parametersShortcut parametersTotalTrainable
Your configuration in PyTorch
Updates as you change the controls · call the mode helper every epoch
Parameter counts are exact. Accuracy, runtime and memory use require an actual training experiment.Explore the architecture →

Architecture counts are exact for the selected torchvision ResNet. The forward trace illustrates processing; it does not run image inference.

03 / SEE THE CODE

The idea,
in Python.

A compact implementation of the core concept. Open the code when you are ready to connect the visual to the algorithm.

Open the core implementation Python ↗
Standalone example · illustrative valuesDownload .py
import torch
from torch import nn
from torchvision import models

# Downloads ImageNet weights on the first run.
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
model.fc = nn.Linear(model.fc.in_features, 3)
trainable_groups = ["fc"]

for name, parameter in model.named_parameters():
    group = "stem" if name.split(".")[0] in {"conv1", "bn1"} else name.split(".")[0]
    parameter.requires_grad = group in trainable_groups

def set_training_mode():
    # Call at the START of EVERY training epoch.
    model.train()
    # Freeze BN buffers separately from affine weight/bias parameters.
    for module in model.modules():
        if isinstance(module, nn.modules.batchnorm._BatchNorm):
            module.eval()

set_training_mode()
parameters = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.AdamW(parameters, lr=1e-4, weight_decay=1e-2)
print("Total:", sum(p.numel() for p in model.parameters()))
print("Trainable:", sum(p.numel() for p in parameters))
# Use model.eval() and torch.inference_mode() for validation/inference.
# Freezing weights does not remove their forward computation.
04 / TRY IT

Explain the change.
Test your reasoning.

  1. Start with a three-class ResNet-18 head. Explain why only 1,539 parameters are trainable.
  2. Unfreeze Stage 4. Why does one stage account for roughly 75% of the adapted model?
  3. Create two configurations with the same trainable groups but different BatchNorm statistics behavior. Explain what changes, then design a validation comparison.
← All visual labsNext: ResNet: Inside the Residual Block

Find your next idea.

Search across courses, research, projects, and resources.

Ideas, connected.esc to close