Loading PKL model on another computer

I saved my model using learner.export().

Then, I tried to load this model on another computer. My goal is using it in train mode there.

learner = load_learner(path)

But I got FileNotFoundError. It looks like the model file tries to load files as they were defined in the original computer.

What is the correct way to load model on another computer?

Thanks

Can you give the complete error? path should not contain export.pkl.

My errors is:

learner = load_learner(model_path,'export.pkl')

Result:

/Users/maya/.pyenv/versions/3.6.7/lib/python3.6/site-packages/torch/serialization.py:454: SourceChangeWarning: source code of class 'torch.nn.modules.loss.CrossEntropyLoss' has changed. 
you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
...

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~/.pyenv/versions/3.6.7/lib/python3.6/pathlib.py in mkdir(self, mode, parents, exist_ok)
   1247         try:
-> 1248             self._accessor.mkdir(self, mode)
   1249         except FileNotFoundError:

~/.pyenv/versions/3.6.7/lib/python3.6/pathlib.py in wrapped(pathobj, *args)
    386         def wrapped(pathobj, *args):
--> 387             return strfunc(str(pathobj), *args)
    388         return staticmethod(wrapped)

FileNotFoundError: [Errno 2] No such file or directory: '/home/ubuntu/forms/shared/models'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
~/.pyenv/versions/3.6.7/lib/python3.6/pathlib.py in mkdir(self, mode, parents, exist_ok)
   1247         try:
-> 1248             self._accessor.mkdir(self, mode)
   1249         except FileNotFoundError:

~/.pyenv/versions/3.6.7/lib/python3.6/pathlib.py in wrapped(pathobj, *args)
    386         def wrapped(pathobj, *args):
--> 387             return strfunc(str(pathobj), *args)
    388         return staticmethod(wrapped)

FileNotFoundError: [Errno 2] No such file or directory: '/home/ubuntu/forms/shared'
...

The missing folder contains training data on the server, but I don’t have it in my local environment

You can only save the model if you want.

# Save model
torch.save(learn.model.state_dict(), path)

# Load model
learn.model.load_state_dict(torch.load(PATH))

I kind of tried reproducing your error, but I was not able to do that. So cannot comment on your problem completely.

1 Like

I solved the problem by loading stage-2.pth file like in this example.

data2 = ImageDataBunch.single_from_classes(path, classes, tfms=get_transforms(), size=224).normalize(imagenet_stats)
learn = create_cnn(data2, models.resnet34)
learn.load('stage-2')```

Thank you for taking the time and effort, I’ll try this approach.