I’m using the code above to load both: the initial imagenet pretrained weights, and my checkpoint .pth.
The imagenet pretrained provided by pytorch is working, but my checkpoint is giving this error
What am I doing wrong?
TIA,
Virilo
P.S. I have read in this forum and StackOverflow other people with the same problem. But these cases were related to the fastai version, or to inconsistency in the base_arch parameter
I tried in this way. Now it reads the ‘model’ inside the state dictionary.
But it seems that layers names have been modified during the saving:
File "imet-fastai-starter.py", line 180, in load_checkpoint
model.load_state_dict(state['model'])
File "/opt/anaconda/anaconda3/envs/fp16/lib/python3.6/site-packages/torch/nn/modules/module.py", line 769, in load_state_dict
self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for ResNet:
Missing key(s) in state_dict: "conv1.weight", "bn1.weight", "bn1.bias", "bn1.running_mean", "bn1.running_var", "layer1.0.conv1.weight", "layer1.0.bn1.weight", "layer1.0.bn1.bias", ...
... Unexpected key(s) in state_dict: "0.0.weight", "0.1.weight", "0.1.bias", "0.1.running_mean", "0.1.running_var", "0.1.num_batches_tracked", ...
BTW, I’d like to load also the optimizer data. There must be important weights, like the ones related to momentum, because of I’d like to continue the training
Hi, did you load a saved fastai learner to a pytorch model in the end? I’m trying to load a saved fastai learner into pytorch but I’m facing the same problem that the keys are different:
learner = cnn_learner(data, models.resnet34, metrics=accuracy, bn_final=True)
learner.fit(...)
learner.save('model_path')
model = models.resnet34() # this is a pytorch model object implemented in fastai
state_dict = torch.load('model_path')
model.load_state_dict(state_dict)