Load_learner failed when model built in fastaiv1 was loaded in fastai2

Hi all,
I built a (resnet18)model in fastai1 and saved this in pkl file. I used export function to save the model in fastai1.
Now I am trying to load this model saved (a pkl file) in fastai2 using load_learner(also tried load_model) function but i am getting below error.


Attribute Error: Can’t get attribute ‘Flattened Loss’ on <module ‘fastai.layers’ from …\site-packages\fastai\layers.py

Note: I was able to load the model in fastai1 environment.

Fastai2 is not backwards compatible with fastai1. In general, you should use the same environment (versions) when loading a model as you used when training it.

1 Like

I understand that fastai2 is completely rewritten from scratch.
But is there no way to load models as the model used is resnet18? I think the model needs just weights and biases right to load it in any environment?

Export saves not just the model but the whole learner, so this will not work in fastai2 I think. You could try saving only the model parameters and load them in a resnet18 in fastai2, that could work!


I can confirm that worked for a language model that I trained in fastai v1 and fine-tuned in fastai v2. No guarantee that it’ll work for ResNets too, but you can give it a shot.

When you use learn.save('your_model') in fastai v1, a .pth file will be saved which only contains your model weights. You should then be able to load this model into fastai v2 using learn.load('your_model'). Note that you’ll need to work with the same classes in both versions, otherwise the number of classes and the model’s classification head won’t be compatible.

However, I agree with Hannes that it’s probably not recommended to do that. E.g. I found out that after loading a model from v1 to v2, using learn.lr_find() before learn.fit_one_cycle() produces an error. In my case I wanted to avoid having to re-train an entire language model which required quite some resources the first time I trained it. But if your dataset is reasonably small I would consider re-training it from scratch in v2.