Where in the FastAI codebase is the code which pops off the last layer of a pre-trained model?

Question is in the title. I looked inside the code for fine_tune, and although I’m not an experienced Python developer, nothing stood out to me:

@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)

I’m curious what method(s) one would call if they wanted to manually remove this last layer themselves.

Look at the cnn_learner here

Make sense, I see cnn_learner calls create_cnn_model, which in turn calls create_body and create_head. Thanks!

2 Likes