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