skills/cv/frozen-batchnorm-finetuning/SKILL.md
Unfreezes backbone layers for fine-tuning while keeping BatchNorm layers frozen to preserve pretrained running statistics.
npx skillsauth add wenmin-wu/ds-skills cv-frozen-batchnorm-finetuningInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
When fine-tuning a pretrained backbone on a small medical/domain-specific dataset, unfreezing all layers including BatchNorm can destabilize training. BN layers maintain running mean/variance from ImageNet — updating these with a small batch from a different domain collapses the statistics. The fix: unfreeze convolutional/linear layers for gradient updates but explicitly keep all BatchNorm layers frozen (eval mode). This preserves pretrained normalization while allowing the rest of the network to adapt.
import torch.nn as nn
def unfreeze_with_frozen_bn(model, n_layers=20):
"""Unfreeze last N backbone layers, keep all BatchNorm frozen."""
# First freeze everything
for param in model.parameters():
param.requires_grad = False
# Unfreeze last N layers, skipping BN
layers = list(model.features.children())
for layer in layers[-n_layers:]:
for name, module in layer.named_modules():
if isinstance(module, (nn.BatchNorm2d, nn.BatchNorm1d)):
module.eval() # keep in eval mode
for param in module.parameters():
param.requires_grad = False
else:
for param in module.parameters():
param.requires_grad = True
# Always unfreeze the classifier head
for param in model.classifier.parameters():
param.requires_grad = True
# Also override train() to keep BN in eval mode
class FrozenBNModel(nn.Module):
def train(self, mode=True):
super().train(mode)
for m in self.modules():
if isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
m.eval()
return self
train() to force BN layers into eval mode every forward passmodel.train() re-enables BN by defaultdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF