Yolov5: Transfer Learning - Freezing Parameters

Created on 9 Aug 2020  路  22Comments  路  Source: ultralytics/yolov5

Does yolov5 support transfer learning?

While training models, is there a possibility to use pretrained weights and modify last few layers?

Stale documentation question

Most helpful comment

@ska6845 I've been asked this multiple times, so I've added a section to train.py that handles freezing parameters:
https://github.com/ultralytics/yolov5/blob/e71fd0ec0bbccb2ea38936a86defb0a31ff1153c/train.py#L76-L83

You can add any parameters you want to this list, with full or partial names, to freeze them before training starts. This code freezes all weights, leaving only biases with active gradients:

    # Freeze
    model.info()
    freeze = ['.weight', ]  # parameter names to freeze (full or partial)
    if any(freeze):
        for k, v in model.named_parameters():
            if any(x in k for x in freeze):
                print('freezing %s' % k)
                v.requires_grad = False
    model.info()

Output:

Model Summary: 191 layers, 7.46816e+06 parameters, 7.46816e+06 gradients
freezing model.0.conv.conv.weight
freezing model.0.conv.bn.weight
freezing model.1.conv.weight
freezing model.1.bn.weight
...
Model Summary: 191 layers, 7.46816e+06 parameters, 11453 gradients

All 22 comments

Hello @shubhamag01, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom model or data training question, please note Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

  • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
  • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
  • Custom data training, hyperparameter evolution, and model exportation to any destination.

For more information please visit https://www.ultralytics.com.

@shubhamag01 this is the default behavior when a pretrained model is specified:
python train.py --weights yolov5s.pt

@glenn-jocher is it possible to train, removing last few layers and using pretrained model and add new layers and train freezing rest of untouched layers

@shubhamag01 you can do whatever you want

@glenn-jocher is there a difference between

python3 train.py --data coco1cls.data --cfg yolov3-spp.cfg --weights weights/yolov3-spp.pt

and

python3 train.py --data coco1cls.data --cfg yolov3-spp.cfg --weights weights/yolov3-spp.pt --transfer

I assumed that pre-trained weights was the idea behind transfer learning, then I found the tutorial on Transfer Learning it with --transfer command specified.
Thanks in advance.

@karen-gishyan your argument does not exist in train.py. See the argparser arguments at the end of train.py for a list of available arguments.

thanks @glenn-jocher.

@glenn-jocher I had made a custom yolov5 model and i ran python train.py --img 640 --batch 16 --epochs 100 --data '../data.yaml' --cfg ./models/custom_yolov5s.yaml --weights yolov5s.pt --nosave --cache, I have modified last few layers of yolov5s.yaml and made custom_yolov5s.yaml . Now I want the freeze the layers which are not being modified for yolov5s and want to train my model on remaining. How do I do that?

@ska6845 I've been asked this multiple times, so I've added a section to train.py that handles freezing parameters:
https://github.com/ultralytics/yolov5/blob/e71fd0ec0bbccb2ea38936a86defb0a31ff1153c/train.py#L76-L83

You can add any parameters you want to this list, with full or partial names, to freeze them before training starts. This code freezes all weights, leaving only biases with active gradients:

    # Freeze
    model.info()
    freeze = ['.weight', ]  # parameter names to freeze (full or partial)
    if any(freeze):
        for k, v in model.named_parameters():
            if any(x in k for x in freeze):
                print('freezing %s' % k)
                v.requires_grad = False
    model.info()

Output:

Model Summary: 191 layers, 7.46816e+06 parameters, 7.46816e+06 gradients
freezing model.0.conv.conv.weight
freezing model.0.conv.bn.weight
freezing model.1.conv.weight
freezing model.1.bn.weight
...
Model Summary: 191 layers, 7.46816e+06 parameters, 11453 gradients

@glenn-jocher TODO: update this to act correctly with optimizer parameter grouping (pg0-gp2):
https://github.com/ultralytics/yolov5/blob/e71fd0ec0bbccb2ea38936a86defb0a31ff1153c/train.py#L89-L91

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Removing TODO as this fix is incorporated in PR #1239.

Layer freezing functionality now operates correctly in all cases. To freeze layers, simply add their names to the freeze list in train.py:
https://github.com/ultralytics/yolov5/blob/187f7c2ed16b9eb9754b28e7b0aa397f908155aa/train.py#L83-L90

@glenn-jocher I wanted to freeze the backbone part of yolov5l configuration. Could you please tell me how to do it? And also will freezing the layers help in any way except decreased time in training. I am using coco pretrain for the logo-detection problem.
Thanks

@pngmafia freezing layers will reduce your mAP. You can add the names of the parameters you'd like to freeze to the freeze list. You can verify which layers are frozen by printing the model info:

print(model.info(verbose=True))

@glenn-jocher I don't mind a decrease in mAP. But I need recall to be high. And also is there a way to give recall more weight than mAP?
Thanks

@pngmafia recall is not a universal metric, it depends on your conf. If you want maximum recall, all you need to do is set conf_thres to zero. Then you will have 100% recall.

@glenn-jocher can't I use the fitness function in utils. Use different weights for those four metrics? P R mAP:.5 and mAP:.5:.95. I see its 0 0 0.1 and 0.9 now.

@pngmafia sure, you can customize the hyperparameter evolution fitness function as you see fit. See hyperparameter evolution tutorial in https://github.com/ultralytics/yolov5#tutorials

@glenn-jocher Does changing those weights to 0 0.8 0.1 0.1 give me better recall? As compared to 0 0 0.1 and 0.9. Considering I use the same confidence threshold for both the experiments.

@pngmafia hyperparameter evolution maximizes the fitness function here:
https://github.com/ultralytics/yolov5/blob/187f7c2ed16b9eb9754b28e7b0aa397f908155aa/utils/general.py#L926-L930

Normal training minimizes loss on your training dataset, and is unrelated to hyperparameter evolution.

@glenn-jocher so this fitness function is just used only when we train with --evolve option. If I'm training normally on my dataset then, It really doesn't matter what weights I give in that function right?

@pngmafia that's correct.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

we1pingyu picture we1pingyu  路  3Comments

nanometer34688 picture nanometer34688  路  3Comments

ShreshthSaxena picture ShreshthSaxena  路  4Comments

xinxin342 picture xinxin342  路  3Comments

dereyly picture dereyly  路  4Comments