Learner.load()

As explained on the documentation
https://docs.fast.ai/basic_train.html#Saving-and-loading-models

Saving and loading models

Simply call Learner.save and Learner.load to save and load models. Only the parameters are saved, not the actual architecture (so you’ll need to create your model in the same way before loading weights back in).

So you save like:

learn.save("trained_model")
or
learn.save("trained_model", return_path=True)

But then load by attaching it to a learner architecture

learn = cnn_learner(data, models.resnet18).load("trained_model")

or
learn = cnn_learner(data, models.resnet18)
learn.load("trained_model")

Note that here we also need to attach the databunch (data) to the learner


https://docs.fast.ai/tutorial.inference.html

A very different thing is to export the learner with everything it needs like

learn.export()
or
learn.export('trained_model.pkl')

Once everything is ready for inference, we just have to call learn.export to save all the information of our Learner object for inference: the stuff we need in the DataBunch (transforms, classes, normalization…), the model with its weights and all the callbacks our Learner was using. Everything will be in a file named export.pkl in the folder learn.path. If you deploy your model on a different machine, this is the file you’ll need to copy.

And then importing it with

learn = load_learner(path)
or
learn = load_learner(path, 'trained_model.pkl')

To create the Learner for inference, you’ll need to use the load_learner function. Note that you don’t have to specify anything: it remembers the classes, the transforms you used or the normalization in the data, the model, its weigths… The only argument needed is the folder where the ‘export.pkl’ file is.


Here you can see a similar conversation

1 Like