Error while loading saved model post update of pytorch

I was using a older version of pytorch as I saved a resnet18 model with 2 neurons in classification using torch.save(model, ‘…/models/resnet.pth’). I was also able to load it back to memory by simply using torch.load (’…/models/resnet.pth’). Recently I upgraded my pytorch version and I am having issues when loading the model. The error is one as below

/home/uavws/anaconda3/lib/python3.6/site-packages/torch/serialization.py:316: SourceChangeWarning: source code of class ‘torchvision.models.resnet.ResNet’ has changed. you can retrieve the original source code by accessing the object’s source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
Traceback (most recent call last):
File “hgg_lgg_tsne_plot.py”, line 34, in
trained_model=torch.load(’/media/uavws/Varghese/pyt/resnet18/coronal_zscore_complete_network.pth’)
File “/home/uavws/anaconda3/lib/python3.6/site-packages/torch/serialization.py”, line 261, in load
return _load(f, map_location, pickle_module)
File “/home/uavws/anaconda3/lib/python3.6/site-packages/torch/serialization.py”, line 409, in _load
result = unpickler.load()
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 875: ordinal not in range(128)

Please help

What version of torchvision were you using before? It sounds like from the error that torchvision.models.resnet.ResNet changed source code.

I trained the model by using python 2.7 version of pytorch using “pip”. During testing I used a python 3.6 version of pytorch which was installed using conda. I guess the torchvision is different in the pip and conda installation procedure.

From the code, it looks like the loading process is bugging out when the python pickler calls load(). The pickler used for saving and loading must be the same. I think the problem is that you switched python versions: I’m not sure if the pickler for python 2 is the same as for python 3.

File “/home/uavws/anaconda3/lib/python3.6/site-packages/torch/serialization.py”, line 409, in _load
result = unpickler.load()

If you’re willing to dive into python code, if you change unpickler = pickle_module.Unpickler(pickle_file) to unpickler = pickle_module.Unpickler(pickle_file, encoding='bytes') in /home/uavws/anaconda3/lib/python3.6/site-packages/torch/serialization.py, this might work.
image

Thank you richard. I am forced to make this comments atleast 20 words long