Load Learner - AttributeError: Can't get attribute 'FlattenedLoss' on <module 'fastai.layers'

@muellerzr First of all, thank you for the great work you put into the project!

The change: #2843) has the potential problem, that it can break the loading of a pickled model. This happens when the model was trained with a fastai version < 2.0.15 and then is loaded with the fastai >= 2.0.15.

If the model can be trained quickly, it can just be retrained with the newest fastai version.
However, some models may take days to train or one just wants to use a given model in production with the latest fastai version.

To be able to load a model, that was trained with fastai version < 2.0.15 with fastai version 2.0.15, I temporarily re-added the moved losses to the layers module like this:

import fastai.losses
import fastai.layers

fastai.layers.BaseLoss = fastai.losses.BaseLoss
fastai.layers.CrossEntropyLossFlat = fastai.losses.CrossEntropyLossFlat
fastai.layers.BCEWithLogitsLossFlat = fastai.losses.BCEWithLogitsLossFlat
fastai.layers.BCELossFlat = fastai.losses.BCELossFlat
fastai.layers.MSELossFlat = fastai.losses.MSELossFlat
fastai.layers.L1LossFlat = fastai.losses.L1LossFlat
fastai.layers.LabelSmoothingCrossEntropy = fastai.losses.LabelSmoothingCrossEntropy
fastai.layers.LabelSmoothingCrossEntropyFlat = fastai.losses.LabelSmoothingCrossEntropyFlat

When the model should be retrained and pickled again, one needs to make sure to use the moved losses in fastai.losses module.

EDIT:
I also ran into this error AttributeError: 'TypeDispatch' object has no attribute 'owner' so I ended up pinning some previous versions:
conda install -y -c fastai fastcore=1.0.12
conda install -y -c pytorch -c fastai fastai=2.0.13

3 Likes