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':
            # 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

This assignment:


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

with torch.no_grad():

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

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