Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: Error(s) in loading state_dict for ResNet:

I am loading my model using the following code.

def load_model(checkpoint_path):
  '''
  Function that loads a checkpoint and rebuilds the model
  '''

  checkpoint = torch.load(checkpoint_path, map_location = 'cpu')

  if checkpoint['architecture'] == 'resnet18':
    model = models.resnet18(pretrained=True)

  # Freezing the parameters
    for param in model.parameters():
        param.requires_grad = False


  else:
    print('Wrong Architecture!')
    return None

  model.class_to_idx = checkpoint['class_to_idx']

  classifier = nn.Sequential(OrderedDict([
                            ('fc1', nn.Linear(512, 1024)),
                            ('relu1', nn.ReLU()),
                            ('dropout', nn.Dropout(0.2)),
                            ('fc2', nn.Linear(1024, 102))
                            ]))


  model.fc = classifier

  model.load_state_dict(checkpoint['state_dict'])

  return model

And while running

# Load your model to this variable
model = load_model('checkpoint.pt')

I get the following error,

RuntimeError Traceback (most recent call last) in () 1 # Load your model to this variable ----> 2 model = load_model('checkpoint.pt') 3 4 # If you used something other than 224x224 cropped images, set the correct size here 5 image_size = 224

<ipython-input-11-81aef50793cb> in load_model(checkpoint_path)
     30   model.fc = classifier
     31 
---> 32   model.load_state_dict(checkpoint['state_dict'])
     33 
     34   return model

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in 
load_state_dict(self, state_dict, strict)
    719         if len(error_msgs) > 0:
    720             raise RuntimeError('Error(s) in loading state_dict for 
{}:\n\t{}'.format(
--> 721                                self.__class__.__name__, 
"\n\t".join(error_msgs)))
    722 
    723     def parameters(self):

RuntimeError: Error(s) in loading state_dict for ResNet:
  Unexpected key(s) in state_dict: "bn1.num_batches_tracked", 
"layer1.0.bn1.num_batches_tracked", "layer1.0.bn2.num_batches_tracked", 
"layer1.1.bn1.num_batches_tracked", "layer1.1.bn2.num_batches_tracked", 
"layer2.0.bn1.num_batches_tracked", "layer2.0.bn2.num_batches_tracked", 
"layer2.0.downsample.1.num_batches_tracked", 
"layer2.1.bn1.num_batches_tracked", "layer2.1.bn2.num_batches_tracked", 
"layer3.0.bn1.num_batches_tracked", "layer3.0.bn2.num_batches_tracked", 
"layer3.0.downsample.1.num_batches_tracked", 
"layer3.1.bn1.num_batches_tracked", "layer3.1.bn2.num_batches_tracked", 
"layer4.0.bn1.num_batches_tracked", "layer4.0.bn2.num_batches_tracked", 
"layer4.0.downsample.1.num_batches_tracked", 
"layer4.1.bn1.num_batches_tracked", "layer4.1.bn2.num_batches_tracked".
like image 606
Kavin Raju S Avatar asked Dec 02 '22 10:12

Kavin Raju S


1 Answers

I was using Pytorch 0.4.1 but Jupyter Notebook which I loaded uses 0.4.0. So I added strict=False attribute to load_state_dict().

model.load_state_dict(checkpoint['state_dict'], strict=False)
like image 53
Kavin Raju S Avatar answered Dec 20 '22 12:12

Kavin Raju S