Can't Load pre-trained model in Colab UnpicklingError

Hi, I’m having a weird error. I train my model (I’m using Colab) as usual with learn= create_cnn(data, models.resnet50, metrics=[accuracy]) then fit the model and save it. After that I use learn.export() to get the file export.pkl when I try to do inference I used learn=load_learner(path_to_pkl_file) and the following error appears:

UnpicklingError                           Traceback (most recent call last)
<ipython-input-84-b9e031f97a6b> in <module>()
      1 pkl = Path('fer2013/')
----> 2 learner= load_learner(pkl)

/usr/local/lib/python3.6/dist-packages/fastai/ in load_learner(path, fname, test)
    467 def load_learner(path:PathOrStr, fname:PathOrStr='export.pkl', test:ItemList=None):
    468     "Load a `Learner` object saved with `export_state` in `path/fn` with empty data, optionally add `test` and load on `cpu`."
--> 469     state = torch.load(open(Path(path)/fname, 'rb'))
    470     model = state.pop('model')
    471     src = LabelLists.load_state(path, state.pop('data'))

/usr/local/lib/python3.6/dist-packages/torch/ in load(f, map_location, pickle_module)
    365         f = open(f, 'rb')
    366     try:
--> 367         return _load(f, map_location, pickle_module)
    368     finally:
    369         if new_fd:

/usr/local/lib/python3.6/dist-packages/torch/ in _load(f, map_location, pickle_module)
--> 528     magic_number = pickle_module.load(f)
    529     if magic_number != MAGIC_NUMBER:
    530         raise RuntimeError("Invalid magic number; corrupt file?")

UnpicklingError: unpickling stack underflow
1 Like

I got the same error. Let me know if you were able to find a solution.

The only thing that works for me was to save the .pkl file in my drive and the access from my drive to do the inference. But I still don’t know why and when I download the .pkl to my computer the upload the file to colab the error appears again

First I save the pkl file:


Then I copy the file to my drive:

! cp export.pkl gdrive/'My Drive'/ 

Finally I import the file from my drive:

pkl = Path(os.path.join('gdrive','My Drive'))
learner= load_learner(pkl)

This is the code to access to your files in your drive:

from google.colab import drive

# This will prompt for authorization.