3d_pose_baseline_pytorch icon indicating copy to clipboard operation
3d_pose_baseline_pytorch copied to clipboard

'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

Open Ssrzbao opened this issue 7 years ago • 23 comments
trafficstars

I have download the pretrianed model gt_ckpt_best.pth.tar. But when I run the command python main.py --load gt_ckpt_best.pth.tar --test, the error 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) has occurred.

Ssrzbao avatar Oct 15 '18 07:10 Ssrzbao

What coding format is this trained model?

Ssrzbao avatar Oct 15 '18 07:10 Ssrzbao

@Ssrzbao Did you use Pyhton2 ? The data is saved using torch in python2.7, it will cause error when loading using Python3

weigq avatar Oct 15 '18 08:10 weigq

Hi @weigq I am using Python3 and got this error. Is there a way around for Python3 or would I need to use 2.7?

ahwaleed avatar Oct 17 '18 07:10 ahwaleed

@ahwaleed You`d best use 2.7, I have tested on python3, where some errors occur.

weigq avatar Oct 17 '18 08:10 weigq

Thanks @weigq. Using python 2.7 worked!

ahwaleed avatar Oct 18 '18 07:10 ahwaleed

import pickle
from functools import partial
...        
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
ckpt = torch.load(opt.load, pickle_module=pickle)

worked for me with python 3

tobiascz avatar Nov 28 '18 12:11 tobiascz

dear @tobiascz ,

import pickle from functools import partial ...
pickle.load = partial(pickle.load, encoding="latin1") pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1") ckpt = torch.load(opt.load, pickle_module=pickle)

where did you add them?

NguyenDangBinh avatar Jan 27 '19 09:01 NguyenDangBinh

dear @tobiascz ,

import pickle from functools import partial ... pickle.load = partial(pickle.load, encoding="latin1") pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1") ckpt = torch.load(opt.load, pickle_module=pickle)

where did you add them?

the imports at the very start. And the stuff after ... exactly where you load your model ckpt in the original file. the two lines with pickle.load and pickle.Unpickler are basically used in ckpt=torch.load(opt.load, pickle_module=pickle) to change the encoding. Hope this helps

tobiascz avatar Jan 27 '19 23:01 tobiascz

dear @tobiascz ,

  • The imports are at the begin. ok
  • where can i add 3 line pickle in the original file on main.py?

NguyenDangBinh avatar Jan 28 '19 09:01 NguyenDangBinh

# load ckpt
if opt.load:
    print(">>> loading ckpt from '{}'".format(opt.load))
    ckpt = torch.load(opt.load)
    start_epoch = ckpt['epoch']
    err_best = ckpt['err']
    glob_step = ckpt['step']
    lr_now = ckpt['lr']
    model.load_state_dict(ckpt['state_dict'])
    optimizer.load_state_dict(ckpt['optimizer'])
    print(">>> ckpt loaded (epoch: {} | err: {})".format(start_epoch, err_best))

This is the code block. Replace the ckpt=torch.load with the code I provided.

    # load ckpt
    if opt.load:
        print(">>> loading ckpt from '{}'".format(opt.load))
        pickle.load = partial(pickle.load, encoding="latin1")
        pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
        ckpt = torch.load(opt.load, pickle_module=pickle)
        start_epoch = ckpt['epoch']
        err_best = ckpt['err']
        glob_step = ckpt['step']
        lr_now = ckpt['lr']
        model.load_state_dict(ckpt['state_dict'])
        optimizer.load_state_dict(ckpt['optimizer'])
        print(">>> ckpt loaded (epoch: {} | err: {})".format(start_epoch, err_best))

tobiascz avatar Jan 28 '19 09:01 tobiascz

dear @tobiascz , I run by GPU. I replaced it like picture below, but it still the same error. error

NguyenDangBinh avatar Jan 28 '19 11:01 NguyenDangBinh

I save the folder like that error

NguyenDangBinh avatar Jan 28 '19 11:01 NguyenDangBinh

Your code seems to be fine. maybe try loading it with and without the pickle stuff and compare them side by side in the debugger to verify if the pickle part actually worked?

tobiascz avatar Jan 28 '19 12:01 tobiascz

error like error1

NguyenDangBinh avatar Jan 28 '19 14:01 NguyenDangBinh

I think the pickle which is not run. error2

NguyenDangBinh avatar Jan 28 '19 14:01 NguyenDangBinh

error3

NguyenDangBinh avatar Jan 28 '19 14:01 NguyenDangBinh

dear @tobiascz, May you help me?

NguyenDangBinh avatar Feb 01 '19 10:02 NguyenDangBinh

@NguyenDangBinh It's hard for me to help you. The problem seems to be a very specific one. I would try to make a new conda environment with python 3.6 and install torch version 1 in there. I noticed that your environment is called 'tensorflow-gpu' and in this project you want to use pytorch. I had some issues when installing both pytorch and tensorflow side by side in one environment or globally. So I would try to create a new env with pytorch and without tensorflow.

tobiascz avatar Feb 01 '19 11:02 tobiascz

@tobiascz , @NguyenDangBinh :

  1. It seems PyTorch's load() function also handles unpickling arguments directly (https://github.com/pytorch/pytorch/blob/6a6983ed7f14f8335a5b5614928713cb79658281/torch/serialization.py#L301), so you probably do not have to monkey patch the pickle module but just can call torch.load(opt.load, encoding='latin1').
  2. If this does not work, maybe try unpickling your file directly first, this makes debugging easier since you remove the indirection.

jjedele avatar Feb 01 '19 12:02 jjedele

Also, are you sure it's encoded in 'latin1'? When I google for the error, some answers hint at UTF8 (e.g. https://stackoverflow.com/questions/21393758/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe5-in-position-0-ordinal)

So maybe also give encoding='utf-8' a try.

jjedele avatar Feb 01 '19 12:02 jjedele

@NguyenDangBinh It's hard for me to help you. The problem seems to be a very specific one. I would try to make a new conda environment with python 3.6 and install torch version 1 in there. I noticed that your environment is called 'tensorflow-gpu' and in this project you want to use pytorch. I had some issues when installing both pytorch and tensorflow side by side in one environment or globally. So I would try to create a new env with pytorch and without tensorflow.

yes, it should be python 2.7

NguyenDangBinh avatar Apr 10 '19 10:04 NguyenDangBinh

I meet the same error. My pytorch version is pytorch-1.0.1 (py3.6_cuda10.0.130_cudnn7.4.2_2) and python version is 3.6.8. The following is the error:

>>> loading data Traceback (most recent call last): File "main.py", line 286, in <module> main(option) File "main.py", line 85, in main stat_3d = torch.load(os.path.join(opt.data_dir, 'stat_3d.pth.tar')) File "/home/pangziwei/anaconda3/envs/pytorch_cuda10/lib/python3.6/site-packages/torch/serialization.py", line 368, in load return _load(f, map_location, pickle_module) File "/home/pangziwei/anaconda3/envs/pytorch_cuda10/lib/python3.6/site-packages/torch/serialization.py", line 542, in _load result = unpickler.load() UnicodeDecodeError: 'ascii' codec can't decode byte 0x91 in position 24: ordinal not in range(128) I think it is caused by python3 encoding. So I open the source code of "serialization.py", and changes as below:

_sys_info = pickle_module.load(f, encoding='iso-8859-1') unpickler = pickle_module.Unpickler(f, encoding='iso-8859-1') unpickler.persistent_load = persistent_load

And it works.

YinRui1991 avatar Jun 03 '19 07:06 YinRui1991

I have download the pretrianed model gt_ckpt_best.pth.tar. But when I run the command python main.py --load gt_ckpt_best.pth.tar --test, the error 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) has occurred.

@Ssrzbao Can you please share the pre-trained model you downloaded? (gt_ckpt_best.pth.tar). The current url to the drive is broken.

danielajisafe avatar Jul 03 '20 19:07 danielajisafe