HelloGoodbye
HelloGoodbye

Reputation: 3902

UnicodeDecodeError in pickle.load

I'm having some problems with loading a pkl file using pickle. I'm using windows 7 and Python 3.5.1 64 bit. The pkl file was downloaded from here.

This is my code:

import pickle

# Load model weights and metadata
weightFile = open('vgg16.pkl', 'rb')
d = pickle.load(weightFile)

and when I run it I get the output

"C:\Program Files\Python35\python.exe" C:/work/lasagne/tutorial/lasagne-tutorial2.py
Traceback (most recent call last):
  File "C:/work/lasagne/tutorial/lasagne-tutorial2.py", line 5, in <module>
    d = pickle.load(weightFile)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbc in position 1: ordinal not in range(128)

Process finished with exit code 1

What does this error message mean? It says that there is a byte that the acsii codec can't decode, but isn't a pkl file supposed to be binary (hence not contain ascii characters)?

Am I doing something wrong when loading the file? What can I do to fix the error?

Upvotes: 5

Views: 3826

Answers (1)

HelloGoodbye
HelloGoodbye

Reputation: 3902

The solution was found in this answer. The pickle file was probably encoded with Python 2, and providing pickle.load with the optional argument encoding='latin1' solved the problem.

The code that works looks like this:

import pickle

# Load model weights and metadata
weightFile = open('vgg16.pkl', 'rb')
d = pickle.load(weightFile, encoding='latin1')

Upvotes: 7

Related Questions