Could I freeze a model before loading pre-trained weights into it?

Hello, everyone!
If I firstly freeze a model like this:

for param in model.parameters(recurse=True):

and then load a set of pre-trained weights into this model, will this model remains frozen? In other words, will the loaded weights refresh the “requires_grad” flag of the model params?
Thanks! :slight_smile:

Checking this the old fashion way suggests the model remains frozen :slight_smile:

# load a model and unfreeze its weights, for good measure
net = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)
for param in net.parameters():
    param.requires_grad = True

# save the weights to disk, "")

# freeze the weights of the model
for param in net.parameters():
    param.requires_grad = False
# load the weights from disk into model

# count number of unfrozen weights
n_unfrozen = 0
for param in net.parameters():
    if param.requires_grad:
        n_unfrozen += 1


EDIT: However, as a practical matter of code readability by a later you who may not remember the answer here for sure, it may be worth it just redundantly going through the weights after loading and asserting that they are frozen, just so it’s clear to the reader that they’re frozen.