Unpickling stack underflow

Hi, I’m meeting a problem loading the pre-trained model of Resnet-50.
I just simply load the model and meet the following problem. I can’t find a solution to solve it.

import torchvision
res= torchvision.models.resnet50(pretrained=True)
Traceback (most recent call last):
File “”, line 1, in
File “/home/hzha3251/.local/lib/python3.6/site-packages/torchvision/models/resnet.py”, line 261, in resnet50
**kwargs)
File “/home/hzha3251/.local/lib/python3.6/site-packages/torchvision/models/resnet.py”, line 223, in _resnet
progress=progress)
File “/home/hzha3251/.local/lib/python3.6/site-packages/torch/hub.py”, line 506, in load_state_dict_from_url
return torch.load(cached_file, map_location=map_location)
File “/home/hzha3251/.local/lib/python3.6/site-packages/torch/serialization.py”, line 529, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File “/home/hzha3251/.local/lib/python3.6/site-packages/torch/serialization.py”, line 692, in _legacy_load
magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: unpickling stack underflow

This error might be thrown, if pickle ends unexpectedly, e.g. if the downloaded file is corrupt.
Did you try to rerun the code?
If you still see this error, could you please delete the cached files?
They should be located in

/home/USER/.cache/torch/checkpoints

by default.

1 Like

Yeah, this deals my problem. Thanks very much

Hi!
I faced the same issue. I work in venv in RHEL. Can you please tell me where can I find .cache directory?
I am using torch 1.5.0.
Error comes from here: /path/to/my/venv/lib64/python3.6/site-packages/torch/serialization.py

Is the .cache folder not in the home folder I’ve posted above?

Sorry, I have .cache directory, but there is no “torch” directory in it (only matplotlib and pip).

Did you change the default cache location?
Could you post the error message, please?
Are you seeing the same error after trying to re-download the model?

I found bug in my code… I tried to load not .pt object with torch.load().
Everything works fine now.
Thank you!)

File “/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py”, line 217, in _resnet
progress=progress)
File “/opt/conda/lib/python3.6/site-packages/torch/hub.py”, line 499, in load_state_dict_from_url
return torch.load(cached_file, map_location=map_location)
File “/opt/conda/lib/python3.6/site-packages/torch/serialization.py”, line 426, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File “/opt/conda/lib/python3.6/site-packages/torch/serialization.py”, line 603, in _load
magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: unpickling stack underflow

thank you for suggest! this deals my problem
the main reason is “e.g. if the pretrain model downloaded file is corrupt.”
step1: vim the last error file “/opt/conda/lib/python3.6/site-packages/torch/serialization.py”
step2: find the error code line,and add “print(”#####", f)" before the error line.
step3: run your code again, then remove the wrong pretrain file “ rm /root/.cache/torch/checkpoints/resnet152-b121ed2d.pth”

Hello, bro, excuse me, I met the same problem as you, how did you solve it at last