Initalizing ResNet18 with Imageet pretrained weights

I am trying to initialize my original PyTorch ResNet18 model with pre-trained weights. I’m targeting all the layers but the last weight and bias. My code in the following does not work. I am not getting any errors but the model is not getting pre-trained. Is this incorrect? I am not bale to find the bug in there.
I have different types for model state dictionary tensors than the pretrained weights:

pretrained tensors are of type <class ‘torch.nn.parameter.Parameter’>
model tensors are of type <class ‘torch.Tensor’>

        if self.dataset=='cub200':
            pdb.set_trace()
            # load pretrained resnet18 weights
            state_dict = load_state_dict_from_url(model_urls[self.model_name] , progress=True) #<class 'collections.OrderedDict'>
            #update the model state_dict
            for key, value in state_dict.items():
                if key !='fc.weight' and key!='fc.bias':
                    assert self.model.state_dict()[key].shape == value.shape
                    self.model.state_dict()[key]=value
                else:
                    continue

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