How to freeze part of the pretrained resnet50 using pytorch

Hi how can I freeze part of a pretrained resnet50 and trained the remaining part of the net using my custom dataset?

You could iterate the parameters you would like to freeze and set their .requires_grad attribute to False:

for name, param in model.layer1.named_parameters():
    param.requires_grad = False

for name, param in model.layer2.named_parameters():
    param.requires_grad = False


Depending how the model was created you could also directly use submodules as model.submodule.prameters().