Ajuste fino: descongelamento e taxas de aprendizado baixas
Os alunos descongelarão as camadas anteriores após o treinamento inicial da cabeça, aplicarão uma taxa de aprendizado menor para evitar destruir as características pré-treinadas e observarão ganhos de acurácia.
Ajuste fino: descongelamento e taxas de aprendizado baixas é uma aula grátis de Machine Learning Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Machine Learning Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Machine Learning Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Why Fine-Tune After Feature Extraction?
Feature extraction adapts only the classification head, leaving the backbone frozen. Fine-tuning goes further by also updating some or all backbone layers, allowing the model to adapt its representations to your specific data distribution.
Fine-tuning is most beneficial when your domain differs from ImageNet — for example, satellite images, medical scans, or industrial defect photos. The ImageNet features partially transfer, but adapting them yields measurably higher accuracy. The risk is catastrophic forgetting: if you fine-tune with a high learning rate, the new data overwrites the carefully pre-learned features, causing performance to collapse.
The Two-Phase Fine-Tuning Strategy
The standard fine-tuning recipe has two phases. Phase 1: Freeze the backbone completely and train only the new classification head for several epochs until it converges. This ensures the head starts from a reasonable state before backbone gradients mix in.
Phase 2: Unfreeze all or some backbone layers and continue training with a very low learning rate (typically 10–100× smaller than phase 1). This gently nudges the pre-learned features toward your domain without destroying them. Skipping phase 1 and fine-tuning from the start with a high LR is the most common mistake that causes poor results.
import torchvision.models as models
import torch.nn as nn
import torch.optim as optim
model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
# --- Phase 1: Freeze backbone, train head ---
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(model.fc.in_features, num_classes)
optimizer = optim.Adam(model.fc.parameters(), lr=1e-3)
# Train for 5-10 epochs...
# --- Phase 2: Unfreeze and fine-tune with low LR ---
for param in model.parameters():
param.requires_grad = True
optimizer = optim.Adam(model.parameters(), lr=1e-5) # 100x lowerSelective Unfreezing: Layer-by-Layer
Rather than unfreezing the entire backbone at once, you can unfreeze it layer by layer starting from the top (closest to the output). This is because later layers contain the most task-specific features, while early layers learn universal features (edges, textures) that rarely need updating.
For ResNet-50, the typical selective unfreezing order is: (1) fc (already trainable), (2) layer4, (3) layer3, and finally (4) layer1 and layer2 if needed. Each step requires evaluating whether the additional unfreezing improves validation accuracy, since more trainable parameters increase overfitting risk on small datasets.
import torchvision.models as models
import torch.nn as nn
model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
# Freeze everything first
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(model.fc.in_features, num_classes)
# After phase 1 training, selectively unfreeze layer4
for param in model.layer4.parameters():
param.requires_grad = True
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f'Trainable (fc + layer4): {trainable:,}')
# Much fewer parameters to fine-tune than the full 25MDifferential Learning Rates
Differential learning rates assign different learning rates to different parts of the network. The new head trains with the highest rate (1e-3), recently unfrozen backbone layers with a moderate rate (1e-4), and early backbone layers with the lowest rate (1e-5 or frozen entirely).
PyTorch's optimiser accepts a list of parameter groups, each with its own lr. This is the standard technique in transfer learning literature and is used in fast.ai's discriminative learning rates. The intuition: parts of the network with more general features need fewer adjustments than task-specific layers.
import torch.optim as optim
import torchvision.models as models
model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
model.fc = nn.Linear(model.fc.in_features, num_classes)
# Unfreeze everything
for p in model.parameters():
p.requires_grad = True
# Differential learning rates per layer group
optimizer = optim.Adam([
{'params': model.fc.parameters(), 'lr': 1e-3}, # Head: highest LR
{'params': model.layer4.parameters(), 'lr': 1e-4}, # Top block
{'params': model.layer3.parameters(), 'lr': 5e-5}, # Middle
{'params': list(model.layer1.parameters()) +
list(model.layer2.parameters()), 'lr': 1e-5} # Early layers
])Monitoring for Catastrophic Forgetting
Catastrophic forgetting occurs when fine-tuning with a large learning rate overwrites the pre-learned features, causing validation accuracy to drop below even the feature-extraction baseline. You will see it as a sharp spike in training loss early in phase 2, followed by slow recovery.
To prevent it: (1) always start phase 2 with a very low learning rate, (2) monitor validation accuracy every epoch and stop immediately if it drops below the phase 1 best, (3) use a learning rate scheduler that gradually warms up the backbone LR, and (4) save the best model checkpoint during phase 1 so you can restore it if phase 2 fails.
import torch
best_val_acc = 0.0
best_state = None
for epoch in range(20):
train_one_epoch(model, train_loader, optimizer, criterion, device)
val_acc = evaluate(model, val_loader, device)
if val_acc > best_val_acc:
best_val_acc = val_acc
best_state = {k: v.clone() for k, v in model.state_dict().items()}
# Stop if validation drops more than 2% below best
if val_acc < best_val_acc - 0.02:
print('Early stop: possible catastrophic forgetting')
model.load_state_dict(best_state) # Restore best
breakLearning Rate Schedulers for Fine-Tuning
A fixed learning rate is rarely optimal throughout fine-tuning. Learning rate schedulers adjust the LR during training to improve convergence. For fine-tuning, two schedulers work especially well: CosineAnnealingLR smoothly decays the LR from initial value to near zero following a cosine curve, and ReduceLROnPlateau reduces the LR whenever validation metric stops improving.
ReduceLROnPlateau with patience=2 is a safe default: if validation loss does not improve for 2 consecutive epochs, the LR is multiplied by factor=0.1. This often recovers progress when training plateaus.
import torch.optim as optim
from torch.optim.lr_scheduler import ReduceLROnPlateau, CosineAnnealingLR
optimizer = optim.Adam(model.parameters(), lr=1e-4)
# Option 1: Reduce on plateau
scheduler = ReduceLROnPlateau(optimizer, mode='max', factor=0.1,
patience=2, verbose=True)
# Call after each validation: scheduler.step(val_acc)
# Option 2: Cosine annealing over T_max epochs
scheduler = CosineAnnealingLR(optimizer, T_max=20, eta_min=1e-7)
# Call after each epoch: scheduler.step()
print('Initial LR:', optimizer.param_groups[0]['lr'])Batch Size and Regularisation During Fine-Tuning
Fine-tuning with a larger batch size reduces gradient noise, which is beneficial when making small adjustments to pre-learned features. However, very large batches can degrade generalisation for fine-tuning (the 'sharp minima' problem). A batch size of 32-64 is a good starting point.
Add weight decay (L2 regularisation) to the optimiser: optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-4). Weight decay prevents any single weight from growing too large, which is especially important for fine-tuning where the pre-trained weights already have sensible magnitudes that we do not want to perturb too aggressively.
import torch.optim as optim
# Fine-tuning optimiser with weight decay
optimizer = optim.AdamW(
model.parameters(),
lr=1e-4,
weight_decay=1e-4 # L2 regularisation
)
# AdamW separates weight decay from gradient adaptation
# (standard Adam incorrectly applies decay to adaptive gradient scaling)
# AdamW is preferred for fine-tuning transformer models especially
print('Optimizer:', optimizer)Data Augmentation During Fine-Tuning
Data augmentation applies random transformations to training images at each epoch, effectively multiplying the dataset size. This is especially important during fine-tuning when you have few images per class. Common augmentations for natural images: random horizontal flip, random rotation (±15°), random crop, colour jitter (brightness, contrast, saturation).
Apply augmentation only during training, not during validation or inference. Use a separate transform for the validation set that only applies deterministic preprocessing (resize, centre crop, normalise). PyTorch's transforms.Compose makes this easy to manage.
from torchvision import transforms
train_transform = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.7, 1.0)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
transforms.RandomRotation(degrees=15),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
val_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])Practical Fine-Tuning Results
How much improvement does fine-tuning give over feature extraction? On a typical custom dataset with ~1000 images per class, feature extraction might yield 88% accuracy while fine-tuning reaches 92-95%. The improvement depends heavily on how different your data is from ImageNet.
Track these four numbers to understand what is happening: phase 1 val accuracy (feature extraction baseline), phase 2 initial val accuracy (should not drop below phase 1 if LR is correct), phase 2 best val accuracy (fine-tuning benefit), and test accuracy (final, reported only once at the very end to avoid test set leakage).
# Example fine-tuning progression
results = {
'Phase 1 (frozen backbone, 10 epochs)': '88.2%',
'Phase 2 initial (unfroze layer4, epoch 1)': '88.5%',
'Phase 2 best (epoch 15)': '92.7%',
'Phase 2 (unfroze layer3 too, epoch 20)': '93.1%',
'Final test accuracy': '92.8%', # Reported only at the end
}
for stage, acc in results.items():
print(f'{stage}: {acc}')
# Key takeaway: fine-tuning added ~4.5% over feature extractionFine-Tuning ViT vs CNN Backbones
Fine-tuning ViT (Vision Transformer) models requires some additional care compared to CNNs. ViT models contain Layer Normalisation rather than Batch Normalisation, so the model.eval() / model.train() modes primarily affect dropout rather than normalisation statistics — this makes fine-tuning slightly simpler.
ViT models also benefit from a lower peak learning rate during fine-tuning (1e-5 to 5e-5 vs 1e-4 for CNNs) because the attention weights are sensitive to large updates. The AdamW optimiser is strongly preferred for ViT fine-tuning. With careful fine-tuning, ViT-B/16 can surpass CNN baselines on many tasks.
import torchvision.models as models
import torch.nn as nn
import torch.optim as optim
# Fine-tuning ViT-B/16
vit = models.vit_b_16(weights=models.ViT_B_16_Weights.IMAGENET1K_V1)
vit.heads = nn.Linear(768, num_classes)
# Very low LR for ViT fine-tuning
optimizer = optim.AdamW(
vit.parameters(),
lr=2e-5,
weight_decay=0.01
)
# Warm-up scheduler (common for transformers)
from torch.optim.lr_scheduler import LinearLR, CosineAnnealingLR, SequentialLR
warmup = LinearLR(optimizer, start_factor=0.01, total_iters=5)
cosine = CosineAnnealingLR(optimizer, T_max=25)
scheduler = SequentialLR(optimizer, schedulers=[warmup, cosine], milestones=[5])Quick Check
Test your understanding of fine-tuning strategies from this lesson.
Lesson Recap
In this lesson you learned: two-phase fine-tuning trains the head first (frozen backbone), then unfreezes the backbone with a very low learning rate to avoid catastrophic forgetting, differential learning rates assign higher rates to the head and lower rates to deeper backbone layers, and selective unfreezing from the top layers down gives the best accuracy-efficiency trade-off. Next up we apply these techniques to a real medical imaging challenge with scarce labels.
Perguntas Frequentes
A aula “Ajuste fino: descongelamento e taxas de aprendizado baixas” é grátis?
Sim — o texto completo de “Ajuste fino: descongelamento e taxas de aprendizado baixas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Machine Learning Academy, atualize para CoddyKit PRO. O curso de Machine Learning Academy inclui 4 aulas no total.
O que vou aprender em “Ajuste fino: descongelamento e taxas de aprendizado baixas”?
Os alunos descongelarão as camadas anteriores após o treinamento inicial da cabeça, aplicarão uma taxa de aprendizado menor para evitar destruir as características pré-treinadas e observarão ganhos d… Você pratica Machine Learning Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Machine Learning Academy?
Nenhuma experiência prévia é necessária. Machine Learning Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Ajuste fino: descongelamento e taxas de aprendizado baixas”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Machine Learning Academy?
Sim. Cada aula de Machine Learning Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Modelos pré-treinados em torchvision: ResNet, EfficientNet e ViT
- Extração de características: congelamento da rede-base
- Ajuste fino: descongelamento e taxas de aprendizado baixas
- Adaptação de domínio: imagens médicas com poucos rótulos