places365 icon indicating copy to clipboard operation
places365 copied to clipboard

UnicodeDecodeError

Open xlxlcyRuSReXxlxl opened this issue 8 years ago • 18 comments

Hi all! Running run_placesCNN_basic.py in this repository I run into this following error: Traceback (most recent call last): File "", line 4, in File "/usr/local/lib/python3.5/dist-packages/torch/serialization.py", line 231, in load return _load(f, map_location, pickle_module) File "/usr/local/lib/python3.5/dist-packages/torch/serialization.py", line 379, in _load result = unpickler.load() UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 875: ordinal not in range(128)

Any advice? Thanks in advance!

xlxlcyRuSReXxlxl avatar Sep 08 '17 08:09 xlxlcyRuSReXxlxl

No idea. maybe you could try useGPU = 1. Model is cached using python2.7.

zhoubolei avatar Sep 08 '17 13:09 zhoubolei

i met the same problem! Did you solve it?

LCorleone avatar Sep 11 '17 10:09 LCorleone

i use python 3.6, i think the version may be the key!

LCorleone avatar Sep 11 '17 10:09 LCorleone

Indeed, it is the pickle library in python which cannot correctly load the CNN model trained in python2.7 into python3.6. Please let me know if there is some good solution for that.

zhoubolei avatar Sep 11 '17 16:09 zhoubolei

yep, i just use virtualenv to create a pure environment for python2.7 and all is ok. thank you for your reply! this is an excellent work!

LCorleone avatar Sep 13 '17 01:09 LCorleone

I solved it with a small change in torch code in serialization.py lines 376-377 by adding the encoding there: _sys_info = pickle_module.load(f,encoding='latin1') unpickler = pickle_module.Unpickler(f,encoding='latin1')

I know it is not the cleanest solution, but it works :) To make it cleaner, it should be at least wrapped it by Try - Catch. But I do use Torch for this purpose only

paffell avatar Sep 13 '17 07:09 paffell

I solved in the same way as paffel did, and it works pretty well.

xlxlcyRuSReXxlxl avatar Sep 15 '17 10:09 xlxlcyRuSReXxlxl

Thanks @paffell for the trick. For those of you who don't want to modify PyTorch's source code, you can do the following in your code just before loading the model:

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)

This is for the run_placesCNN_unified.py example. For run_placesCNN_basic.py, replace the model_file in the last line with model_weight. Note that your pickle namespace will be polluted after this operation. A better idea would be to copy the module first and modify the methods of this copy.

soravux avatar Oct 03 '17 15:10 soravux

Thanks @soravux for cleaning the solution. I started to use it as well :)

paffell avatar Oct 05 '17 10:10 paffell

Thanks @soravux, it works for me too.

Fangyh09 avatar Apr 04 '18 03:04 Fangyh09

@soravux Thanks so much!!! it's work for me

Qinxianshen avatar Oct 27 '18 12:10 Qinxianshen

https://discuss.pytorch.org/t/unicodedecodeerror-ascii-codec-cant-decode-byte-0xc3-in-position-918-ordinal-not-in-range-128/15356/7

mrgloom avatar Jun 14 '19 13:06 mrgloom

UnpicklingError: invalid load key, '\x5c'.

Getting the same error.Can anyone help?

sakshijain20 avatar May 25 '20 13:05 sakshijain20

_pickle.UnpicklingError: invalid load key, '\x04'.

Getting the same error. Tried Soravux's method, still report error.

zhangzhengde0225 avatar Sep 09 '20 08:09 zhangzhengde0225

I solved in the same way as paffel did, and it works pretty well.

will you please mention the line numbers and the file name. Thanks.

sanhok99 avatar Sep 05 '22 16:09 sanhok99

您的邮件已收到,我会尽快回复,祝好!Your email has been received, I will reply as soon as possible!

LCorleone avatar Sep 05 '22 16:09 LCorleone

The hack suggested in this issue, i.e., the following line:

pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")

completely breaks toch.load since version 1.11 because in that version, PyTorch subclasses pickle.Unpickler but after this, pickle.Unpickler is a function and not a type which leads to the following error:

[...]
  File "lib/python3.9/site-packages/torch/serialization.py", line 820, in _legacy_load
    class UnpicklerWrapper(pickle_module.Unpickler):  # type: ignore[name-defined]
TypeError: the first argument must be callable

To load models saved in Python2, since PyTorch 1.1.0 (released May 2019), do

torch.load(...., encoding='latin1')

carandraug avatar Mar 07 '23 14:03 carandraug

您好,我是陆鑫益,您的邮件已经收到,我会尽快回复,谢谢。

LCorleone avatar Mar 07 '23 14:03 LCorleone