Skip to content

Transfer Learning with Frozen Layers

📚 This guide explains how to freeze YOLOv5 🚀 layers when transfer learning. Transfer learning is a useful way to quickly retrain a model on new data without having to retrain the entire network. Instead, part of the initial weights are frozen in place, and the rest of the weights are used to compute loss and are updated by the optimizer. This requires less resources than normal training and allows for faster training times, though it may also results in reductions to final trained accuracy. UPDATED 25 September 2022.

Before You Start

Clone repo and install requirements.txt in a Python>=3.7.0 environment, including PyTorch>=1.7. Models and datasets download automatically from the latest YOLOv5 release.

git clone  # clone
cd yolov5
pip install -r requirements.txt wandb  # install (add W&B for logging)

Freeze Backbone

All layers that match the freeze list in will be frozen by setting their gradients to zero before training starts.

To see a list of module names:

for k, v in model.named_parameters():

# Output

Looking at the model architecture we can see that the model backbone is layers 0-9:

so we can define the freeze list to contain all modules with 'model.0.' - 'model.9.' in their names:

python --freeze 10

Freeze All Layers

To freeze the full model except for the final output convolution layers in Detect(), we set freeze list to contain all modules with 'model.0.' - 'model.23.' in their names:

python --freeze 24


We train YOLOv5m on VOC on both of the above scenarios, along with a default model (no freezing), starting from the official COCO pretrained --weights

$ --batch 48 --weights --data voc.yaml --epochs 50 --cache --img 512 --hyp hyp.finetune.yaml

Accuracy Comparison

The results show that freezing speeds up training, but reduces final accuracy slightly. A full W&B Report of the runs can be found at this link:

W B Chart 06_11_2020, 18_06_07

W B Chart 06_11_2020, 18_05_51

Screenshot 2020-11-06 at 18 08 13

GPU Utilization Comparison

Interestingly, the more modules are frozen the less GPU memory is required to train, and the lower GPU utilization. This indicates that larger models, or models trained at larger --image-size may benefit from freezing in order to train faster.

W B Chart 06_11_2020, 18_11_49

W B Chart 06_11_2020, 18_12_05


YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):



If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.