Git Product home page Git Product logo

Comments (18)

soravux avatar soravux commented on May 27, 2024 107

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.

from places365.

paffell avatar paffell commented on May 27, 2024 26

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

from places365.

zhoubolei avatar zhoubolei commented on May 27, 2024 5

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.

from places365.

xlxlcyRuSReXxlxl avatar xlxlcyRuSReXxlxl commented on May 27, 2024 3

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

from places365.

zhoubolei avatar zhoubolei commented on May 27, 2024

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

from places365.

LCorleone avatar LCorleone commented on May 27, 2024

i met the same problem! Did you solve it?

from places365.

LCorleone avatar LCorleone commented on May 27, 2024

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

from places365.

LCorleone avatar LCorleone commented on May 27, 2024

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!

from places365.

paffell avatar paffell commented on May 27, 2024

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

from places365.

Fangyh09 avatar Fangyh09 commented on May 27, 2024

Thanks @soravux, it works for me too.

from places365.

Qinxianshen avatar Qinxianshen commented on May 27, 2024

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

from places365.

mrgloom avatar mrgloom commented on May 27, 2024

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

from places365.

sakshijain20 avatar sakshijain20 commented on May 27, 2024

UnpicklingError: invalid load key, '\x5c'.

Getting the same error.Can anyone help?

from places365.

zhangzhengde0225 avatar zhangzhengde0225 commented on May 27, 2024

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

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

from places365.

sanhok99 avatar sanhok99 commented on May 27, 2024

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.

from places365.

LCorleone avatar LCorleone commented on May 27, 2024

from places365.

carandraug avatar carandraug commented on May 27, 2024

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')

from places365.

LCorleone avatar LCorleone commented on May 27, 2024

from places365.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.