Initalizing ResNet18 with Imageet pretrained weights

This assignment:

self.model.state_dict()[key]=value

will not work and you should copy_ the value into the parameter directly via:

with torch.no_grad():
    model.layer.parameter.copy_(value)

or manipulate the state_dict separately and load it into the model:

sd = model.state_dict()
...
sd[key] = value
...
model.load_state_dict(sd)
1 Like