Can't get attribute '_ConstantFunc' on <module 'fastai2.learner' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fastai2/'>

I use fastai2 and paperspace to create a model like this:

dls = ImageDataLoaders.from_folder(path, train='elephants', valid_pct=0.2, item_tfms=RandomResizedCrop(224, min_scale=0.5))
learn = cnn_learner(dls, resnet18,
                    loss_func=F.cross_entropy, metrics=accuracy)

I then download the model from paperspace and put it on my local machine. I try to replace the model in the bear_classifier notebook in the bear_voila repository. When it runs the line
learn_inf = load_learner(path/'export_el_reijer.pkl', cpu=True)
I get the error
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/ in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
700 unpickler = pickle_module.Unpickler(f, **pickle_load_args)
701 unpickler.persistent_load = persistent_load
–> 702 result = unpickler.load()
704 deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

AttributeError: Can't get attribute '_ConstantFunc' on <module 'fastai2.learner' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fastai2/'>

Now this is correct, I am missing the _Constantfunc in the on my computer. But pip3 install --upgrade fastai2 does not give me _Constantfunc, it says my fastai2 is up to date. I am reluctant to add it manually, is there something I am doing wrong in this process?

One of the version is not the latest, from your error message where you are loading the module.

Thanks for the quick answer. For some reason when I updated from pip, I did get fastai2 version 0.0.17, but in my local I still had fastai2 0.0.15. After I manually removed this I did manage to get the newest version, but am now running into other issues. I’ll keep debugging.

Edit: what worked for me is looking for all installations on my local drive and manually removing them, and then reinstalling fastai2

Did manually removing and reinstalling fastai2 fix the missing _Constantfunc ?

yes! I was running code on macOS, and I had copies in both my .Local folder and /Library. After I deleted both I ran pip3 install fastai2 from terminal. This didnt quite fix it, and I had to run pip install fastai2 from my notebook as well and then everything worked fine. This error really is due to you having an older version, mine was 0.0.15 and I needed 0.0.17

1 Like