Member-only story

FastAI’s callbacks for better CNN training — meet SaveModelCallback.

Less Wright
3 min readJun 25, 2019

--

FastAI has a very flexible callback system that let’s you greatly customize your training process. However, some of the pre-built and useful callbacks are not as easy to find without a deep dive into the documentation and to my knowledge, aren’t covered in the regular courses.

A common question is thus, “how do I automatically save my best model if it happens in the middle of a training run?” and the answer is to use the SaveModelCallback.

SaveModelCallback in action…

The purpose of this callback, as the name implies, is to automatically save a new ‘best loss’ model automatically during training. At the end of training, it then conveniently loads the top model so it’s ready for you to continue with.

You can specify what criterion defines the ‘best model’, but per a forum post by Jeremy Howard, the rule of thumb is to use ‘error_rate’ as the criterion.

To access the callbacks, use the following:

from fastai.callbacks import *

To use, you add it in the list of callbacks to your learner. This can be done at fit time, or at the learner creation time:

learn.fit_one_cycle(4, max_lr=(4e-3),callbacks=[SaveModelCallback(learn,

monitor=’error_rate’,

--

--

Less Wright
Less Wright

Written by Less Wright

PyTorch, Deep Learning, Object detection, Stock Index investing and long term compounding.

Responses (2)