解决Torch.load()错误信息: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8d in position 0: ordinal not in range(128)

时间:2022-10-08 16:04:38

使用PyTorch跑pretrained预训练模型的时候,发现在加载数据的时候会报错,具体错误信息如下:

File “main.py”, line 238, in main_worker
checkpoint = torch.load(args.resume)
File “/home/kangyuhao/anaconda3/lib/python3.6/site-packages/torch/serialization.py”, line 387, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File “/home/kangyuhao/anaconda3/lib/python3.6/site-packages/torch/serialization.py”, line 574, in _load
result = unpickler.load()
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x8d in position 0: ordinal not in range(128)

发现是字符编码出了问题。经过搜索相关问题,发现是因为pretrained的模型是在python2下训练的,而自己的环境则是python3,由于两者字符编码不兼容,使得预训练的模型不能加载进来。

一种解决方法是换回python2的环境。

另一种解决方法如下,在源代码中添加下面的代码:

from functools import partial
import pickle pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
model = torch.load(model_file, map_location=lambda storage, loc: storage, pickle_module=pickle)

把其中最后一行的model_file替换成自己的pretrained模型文件即可。其原理在于修改了pickle的编码方式,使得torch.load以python2的方法加载pickle.

实测可行!

参考:https://github.com/CSAILVision/places365/issues/25