Freezing layers yolov8
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?