How to load PyTorch .pth weights?
I am using deep fashion dataset pre-trained weights with resnet 50 for attribute prediction.
But I am not sure how to load it in fastai. I am a newbie to fastai and learning right now. Any type of leads/ notebooks to refer will be deeply appreciated. Thank you.
learner.load(deep_fashion_model.pth)
should get you there, assuming your learner.model
has the same architecture
It looks like they use resnet or vgg so cnn_learner
(as used here: https://dev.fast.ai/tutorial.vision) will allow you to create a learner with a model of the correct architecture to load in your pre-trained weights
Hi, tried learner, loaded vgg 16 pretrained model (latest.pth) got these errors.
learn = cnn_learner(data, models.vgg16_bn, metrics=accuracy)
learn = learn.load(â/content/latestâ)
error(s) in loading state_dict for Sequential:
Missing key(s) in state_dict: â0.0.0.weightâ, â0.0.0.biasâ, â0.0.1.weightâ, â0.0.1.biasâ, â0.0.1.running_meanâ, â0.0.1.running_varâ, â0.0.3.weightâ, â0.0.3.biasâ, â0.0.4.weightâ, â0.0.4.biasâ, â0.0.4.running_meanâ, â0.0.4.running_varâ, â0.0.7.weightâ, â0.0.7.biasâ, â0.0.8.weightâ, â0.0.8.biasâ, â0.0.8.running_meanâ, â0.0.8.running_varâ, â0.0.10.weightâ, â0.0.10.biasâ, â0.0.11.weightâ, â0.0.11.biasâ, â0.0.11.running_meanâ, â0.0.11.running_varâ, â0.0.14.weightâ, â0.0.14.biasâ, â0.0.15.weightâ, â0.0.15.biasâ, â0.0.15.running_meanâ, â0.0.15.running_varâ, â0.0.17.weightâ, â0.0.17.biasâ, â0.0.18.weightâ, â0.0.18.biasâ, â0.0.18.running_meanâ, â0.0.18.running_varâ, â0.0.20.weightâ, â0.0.20.biasâ, â0.0.21.weightâ, â0.0.21.biasâ, â0.0.21.running_meanâ, â0.0.21.running_varâ, â0.0.24.weightâ, â0.0.24.biasâ, â0.0.25.weightâ, â0.0.25.biasâ, â0.0.25.running_meanâ, â0.0.25.running_varâ, â0.0.27.weightâ, â0.0.27.biasâ, â0.0.28.weightâ, â0.0.28.biasâ, â0.0.28.running_meanâ, â0.0.28.running_varâ, â0.0.30.weightâ, â0.0.30.biasâ, â0.0.31.weightâ, â0.0.31.biasâ, â0.0.31.running_meanâ, â0.0.31.running_varâ, â0.0.34.weightâ, â0.0.34.biasâ, â0.0.35.weightâ, â0.0.35.biasâ, â0.0.35.running_meanâ, â0.0.35.running_varâ, â0.0.37.weightâ, â0.0.37.biasâ, â0.0.38.weightâ, â0.0.38.biasâ, â0.0.38.running_meanâ, â0.0.38.running_varâ, â0.0.40.weightâ, â0.0.40.biasâ, â0.0.41.weightâ, â0.0.41.biasâ, â0.0.41.running_meanâ, â0.0.41.running_varâ, â1.2.weightâ, â1.2.biasâ, â1.2.running_meanâ, â1.2.running_varâ, "1âŚ
Unexpected key(s) in state_dict: âmetaâ, âstate_dictâ, âoptimizerâ.
Is there any other way to proceed with this?
Thanks
This could be from how the weights are stored away. IE fastai
expects a fastai
model, which has a opt
and model
key. Check out here where I show a snippet of code (look for def transfer_learn
). And while itâs for v2, it works in v1 as well (a few things need to be changed for v1, specifically with the DataLoaders
).
Hi, Tried your ipynb, I faced with the given error,
learn = transfer_learn(learn, â/home/jupyter/Deepfashion/fastai_set/models/latestâ)
KeyError Traceback (most recent call last)
in
----> 1 learn = transfer_learn(learn, â/home/jupyter/Deepfashion/fastai_set/models/latestâ)
in transfer_learn(learn, name, device)
5 if (learn.model_dir/name).with_suffix(â.pthâ).exists(): model_path = (learn.model_dir/name).with_suffix(â.pthâ)
6 else: model_path = name
----> 7 new_state_dict = torch.load(model_path, map_location=device)[âmodelâ]
8 learn_state_dict = learn.model.state_dict()
9 for name, param in learn_state_dict.items():
KeyError: âmodelâ
So given how it looks here, (sorry just now saw how this was saved), it looks like you should key into the state_dict
of that model to actually grab the proper keys. If youâre coming from the same type of vgg it should copy over without throwing many headaches (if you get about 5 keys mismatching, disregard thatâs just the custom head fastai has)