我正在使用以下代码加载模型。
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
跑步时
# Load your model to this variable
model = load_model('checkpoint.pt')
出现以下错误,
RuntimeError跟踪(最近的调用) 持续) 在()中 1#将模型加载到此变量 ----> 2个模型= load_model('checkpoint.pt') 3 4#如果您使用的不是224x224裁剪图像,请设置 正确的尺寸在这里 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".
答案 0 :(得分:1)
我使用的是Pytorch 4.0.1,但是我加载的Jupyter Notebook使用的是4.0.0。所以我在 load_state_dict()中添加了 strict = False 属性。
model.load_state_dict(checkpoint['state_dict'], strict=False)