pythonfast-ai

fast.ai course lesson 1: object has no attribute 'fine_tune'


I'm following the official course

Notebook For Lesson 1: https://www.kaggle.com/code/jhoward/is-it-a-bird-creating-a-model-from-your-own-data

Tutorial docs for the fast.ai vision library: https://docs.fast.ai/tutorial.vision.html

this code:

    dls = DataBlock(
        blocks=[ImageBlock, CategoryBlock],
        get_items=get_image_files,
        splitter=RandomSplitter(valid_pct=0.2, seed=42),
        get_y=parent_label,
        item_tfms=[Resize(192, method='squish')]
    ).dataloaders(path, bs=32)

    learn = vision_learner(dls, resnet18, metrics=error_rate)
    learn.fine_tune(3)

results in this error:

Traceback (most recent call last):
  File "/Projects/fastai/l1-image-classification/l1_image_classification/build_model.py", line 39, in <module>
    build_model()
  File "/Projects/fastai/l1-image-classification/l1_image_classification/build_model.py", line 25, in build_model
    learn.fine_tune(3)
    ^^^^^^^^^^^^^^^
  File "/Users/sys/Library/Caches/pypoetry/virtualenvs/l1-image-classification-ApaWH_9y-py3.11/lib/python3.11/site-packages/fastcore/basics.py", line 496, in __getattr__
    if attr is not None: return getattr(attr,k)
                                ^^^^^^^^^^^^^^^
  File "/Users/sys/Library/Caches/pypoetry/virtualenvs/l1-image-classification-ApaWH_9y-py3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Sequential' object has no attribute 'fine_tune'

What am I doing wrong?


Solution

  • Here's the code for the fine_tune method in the fastai library: https://github.com/fastai/fastai/blob/b273fbb32d075ef1d6fd372687b5f56564cead9a/fastai/callback/schedule.py#L161

    # %% ../../nbs/14_callback.schedule.ipynb 60
    @patch
    @delegates(Learner.fit_one_cycle)
    def fine_tune(self:Learner, epochs, base_lr=2e-3, freeze_epochs=1, lr_mult=100,
                  pct_start=0.3, div=5.0, **kwargs):
        "Fine tune with `Learner.freeze` for `freeze_epochs`, then with `Learner.unfreeze` for `epochs`, using discriminative LR."
        self.freeze()
        self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
        base_lr /= 2
        self.unfreeze()
        self.fit_one_cycle(epochs, slice(base_lr/lr_mult, base_lr), pct_start=pct_start, div=div, **kwargs)
    

    Notice I call it a "method", but it's not defined on a class. Wonder what's up with that?

    You see that @patch decorator?

    Check out the definition of the patch function in the fastcore library: https://github.com/fastai/fastcore/blob/master/nbs/01_basics.ipynb

    TLDR:

    Try adding a from fastai.callback.schedule import fine_tune at the top of your file. Importing that function will "patch" it onto the Sequential class.