ultralytics icon indicating copy to clipboard operation
ultralytics copied to clipboard

Freezing layers yolov8

Open link0ln opened this issue 3 years ago • 0 comments

Search before asking

  • [X] I have searched the YOLOv8 issues and discussions and found no similar questions.

Question

Question with answer! Need to freeze for example backbone. Previouse methods like adding extra code to python script: model = YOLO() freeze = 10 #backbone layers freeze = [f'model.{x}.' for x in range(freeze)] # layers to freeze for k, v in model.model.named_parameters(): v.requires_grad = True # train all layers if any(x in k for x in freeze): print(f'freezing {k}') v.requires_grad = False model.train() Will not work, because model.train() will rewrite all layers requires_grad to True .

Solution is to add above code to ultralytics/yolo/engine/model.py after 206 line before "self.trainer.train()"

Than you can check in separate thread for example: for k, v in model.model.named_parameters(): print(k," ", v.requires_grad)

Additional

Any other solutions?

link0ln avatar Feb 03 '23 13:02 link0ln