8

Fast Ai uses a very unconventional style of from fastai import * etc.

I for one do not like it so was painstakingly identifying each import in the chapter 2 of the fastai book but ran into the error

AttributeError: 'Learner' object has no attribute 'fine_tune'

However, when I then go and do

from fastbook import *

it works. This is a very odd behavior in that something is done to the cnn_learner class or the module that contains it making it have the fine_tune method if the above import is done.

I would like to avoid this style of coding, so what should I do to load the correct version of Learner?

2 Answers 2

12

I just faced the exact same issue. After looking at one of their tutorial I saw that the cnn learner is not imported from the expected package.

from fastai.vision.all import cnn_learner
# rather than
from fastai.vision.learner import cnn_learner

calling the fine_tune method then works as expected !

4

Fastai does a lot of monkey patching. Not only to its own imports but also to other libraries such as pathlib or torch. I personally don't like this style of coding either but it is what it is.

I would highly recommend creating a separate environment (e.g. through conda), install fastai there and use their from ... import *. I have tried to work around these imports in the past but since you don't know (unless you dig into source) where/what has been monkey patched, you will be running into missing attribute and similar errors all over the place.

Also, it doesn't play nice with some other libraries. I remember having hard time making it work with opencv due to package dependencies, where installing opencv broke some of the fastai's functionality (which I have only found later) due to overriding something that has been patched by fastai in some external library.

1
  • Reminds me of why I switched to Python from Perl Dec 8, 2020 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.