Save and load model

Hello,

I want to load a model that I trained using FastAI but I am not able to. This is how I saved the model:
learn.save('model')

It saved a model called model.pth.

Now, I am trying to load the model using the following command:
learn = load_learner('model.pth', cpu=False)

All this loads is a dictionary. I even tried loading through PyTorch but it didn’t help.

What should I do to load the model?

Hi. Please search for “Learner.save” in the fastai docs. The function you are looking for is right next to it. :slightly_smiling_face:

Hey,

Thanks for the reply. So I checked the page and, I might be wrong here, but it seems that I need an object of type Learner to load the model. Am I right?

If that’s so, then how will I load the model without a value for dls argument?

1 Like

Sorry, I must have misunderstood your question. learn.save/learn.load are useful when you want to make a checkpoint in training to come back to. They require that the Learner already exist or be exactly recreated before loading. learn.save does not save the model structure, only the weights.

learn.export and load_learner, according to fastai docs, also seem to be a matched pair. Can you mix learn.save with load_learner? I have no idea! But I doubt it because the functions are specified in different pairs, for different purposes. I would never attempt it.

I do not understand your question about dls. None of the four functions mentioned requires dls as a parameter. You are free to specify the Dataloaders of your choice when (re)creating the Learner, and even change it after the Learner is created.

HTH, Malcolm :slightly_smiling_face:

2 Likes

So what do you recommend I should do? Re-train the model and save it in a different way or load it in a different way? WHat should I do to load a trained model?

Regarding this:

YOu siggested to go through Learner.load. To load a Learner, I need a value for dls. Because it gives me th error when I don’t declare dls.

Ok, let’s back up. What exactly are you trying to accomplish with saving and loading? For inference, to resume a training session, etc. My recommendation depends on your goals and intentions. In general though, you have to save and load in the same way.

With the dls issue, please post all the code that leads up to it and the stack trace. As preformated text, not as a screenshot. I will do my best to help.

If possible, I want to save the model and weights like keras does as an h5 file. Then I want to load the model in a variable for inference.

If it works like PyTorch, then I want to save the trained weights in a file and then, I want to load them in a model variable like we do in pytorch and use that model for inference.

I am guessing for re-training and using the model for inference, we load it the same way.

Here’s the colab for this

Hi Sarvagya. I was not able to get access to your colab link to look in detail.

Because your purpose is to reload the whole model for inference, I think you should use learn.export()/load_learner(). These functions save the entire model, not just the weights. However, see the warning in the docs that you will have to include any custom code in the destination.

The simplest course would be to retrain the model and do learn.export. But you might be able to recover the weights that you already saved with learn.save. You would need to run the original notebook up to the point that training starts to rebuild the Learner. Then learn.load() followed by learn.export(). From there, load_learner will let you bring the trained model into a new environment.

:slightly_smiling_face:

1 Like

This is wierd. I sent you a working link. Let me see what I can do.

Perfect. Let me try this.

What happened with the link is that google needed you to give permission for me to access the file on your Google drive. It offered to send you an email to ask for permission. I did that but realized I could reply even without the notebook.

So never mind on the link. Good luck with export and import! :slightly_smiling_face: