How to recover class names from load_learner?

Untill version 1.0.39 models for inference needed to export 2 files:
1 file with data.export()
1 file with model.save()

Then during inference, the list of the class names were recover like this:
empty_data = ImageDataBunch.load_empty()
empty_data.classes --> gives the list of all the classes used during training

Since fastai version 1.0.40 we can export model directly with:
model.export()
This is great because we can do inference using only the “export.pkl” file.

The problem is that I can’t recover the list of class names.
I tried
empty_data = ImageDataBunch.load_empty(“export.pkl”) but I always have an error:
Key_error: ‘x_cls’

When I create the model like this:
model = load_learner(“export.pkl”)
I’m able to make prediction one image at a time with model.predict(img)
It gives the class Name so it means that the names of all the classes are embedded in the file “export.pkl”

How is it possible to get the list of the class names ?

Thanks for your answers and best regards

load_learner gives you the whole Learner object, which has a data attribute.
So learn.data.classes will work.

8 Likes

Great. It works. Thanks a lot Sylvain for the quick answer !!!
What is strange is that if I try:
model.data. and try the autocompletion,it doesn’t propose “classes” attribute.
Nevertheless, you told me the solution :slightly_smiling_face:: …

In a notebook, the auto-completion only works on the first level object: if you type model.data, then data.cl… it will suggest classes in auto-completion, but not when you type model.data.cl…

2 Likes

Thanks for the clarification :grinning:

Not sure this explains what’s going on. When I type ‘data.[tab]cl’, I do not see data.classes autocomplete. But when I type ‘data.classes’, it gives back the list of classes.

There used to be another layer between data and classes but in master it’s now an attribute. So you should see the tab completion work in v1.0.43.

1 Like