Callback Utilities

Additional functionality for fastai callbacks

Log Values From Callbacks

fastxtend adds the ability to log additional values from callbacks via LogDispatch callback and Learner._log_values & Learner._log_dict.

To log additional values to a supported logger, import LogDispatch and then pass values as arguments to self.learn._log_values or as a dictionary to self.learn._log_dict in after_batch for training values and after_epoch for validation values.

from fastxtend.callback.utils import *

def ExampleCallback(Callback)
    def after_batch(self):
        self.learn._log_values(train_value1=value1, train_value2=value2)

    def after_epoch(self):
        self.learn._log_dict({'valid/value1':value1, 'valid/value2':value2})

LogDispatch will update a train and valid dictionary using the argument names as keys, and will call each supported logger’s update method after Recorder runs.

If imported LogDispatch will automatically be added to the default fastai callbacks.

Supported loggers:

fastxtend uses LogDispatch across multiple callbacks, such as EMAWarmupCallback and ProgressiveResize, to log additional information.

PRs to add additional supported loggers are welcome.

Weights and Biases Features

LogDispatch.log_wandb_table allows logging a Weights and Biases Table in a callback.

def ExampleCallback(Callback)
    def after_fit(self):
        self.learn.log_dispatch.log_wandb_table(name='pandas_df', dataframe=pandas_df)

LogDispatch.log_wandb_summary allows logging Weights and Biases Summary Metrics in a callback.

def ExampleCallback(Callback)
    def after_fit(self):
        self.learn.log_dispatch.log_wandb_summary(name='summary_metric', summary=summary_metric)

source

LogDispatch

 LogDispatch (after_create=None, before_fit=None, before_epoch=None,
              before_train=None, before_batch=None, after_pred=None,
              after_loss=None, before_backward=None,
              after_cancel_backward=None, after_backward=None,
              before_step=None, after_cancel_step=None, after_step=None,
              after_cancel_batch=None, after_batch=None,
              after_cancel_train=None, after_train=None,
              before_validate=None, after_cancel_validate=None,
              after_validate=None, after_cancel_epoch=None,
              after_epoch=None, after_cancel_fit=None, after_fit=None)

A default callback for dispatching additional values to loggers


source

LogDispatch.log_wandb_table

 LogDispatch.log_wandb_table (name:str, **kwargs)

Log wandb.Table to Weights and Biases. See wandb.Table for details


source

LogDispatch.log_wandb_summary

 LogDispatch.log_wandb_summary (name:str, summary:Callable)

Log Summary Metrics to Weights and Biases. See wandb.summary for details


source

Learner._log_values

 Learner._log_values (**kwargs)

Update additional logging values from arguments using LogDispatch.


source

Learner._log_dict

 Learner._log_dict (value_dict:dict)

Update additional logging values from a dictionary using LogDispatch.

Callback Scheduler

CallbackScheduler is a utlitity method for callback developers to modifying callback values on a schedule.

fastxtend uses it in EMAWarmupCallback to schedule the EMA decay rate.


source

CallbackScheduler

 CallbackScheduler ()

A mixin for scheduling values in a Callback


source

CallbackScheduler.setup_schedule

 CallbackScheduler.setup_schedule (n_epoch:int, dls_len:int,
                                   start_value:Union[int,float],
                                   final_value:Union[int,float],
                                   start:Union[int,float],
                                   finish:Union[int,float], schedule:Calla
                                   ble[...,fastai.callback.schedule._Annea
                                   ler]=<function SchedCos>,
                                   ndigits:Union[int,float,NoneType]=None,
                                   callback_name:str='CallbackScheduler',
                                   resume:bool=False)

Setup the schedule for schedule_step. Call during Callback.begin_fit

Type Default Details
n_epoch int Number of training epochs. From a callback pass n_epoch
dls_len int Length of the training dataset
start_value Numeric Initial scheduling value
final_value Numeric Final scheduling value
start Numeric Start schedule in percent of training steps (float) or epochs (int, index 0)
finish Numeric Finish schedule in percent of training steps (float) or epochs (int, index 0)
schedule Callable[…, _Annealer] SchedCos Schedule type. Any fastai schedule annealer
ndigits Numeric | None None Round return value to ndigits if set using Python’s round.
callback_name str CallbackScheduler Name of scheduler for warning & error messages
resume bool False Whether training has resumed or not

source

CallbackScheduler.schedule_step

 CallbackScheduler.schedule_step (value:Union[int,float], pct_train:float)

Takes a scheduling step and returns updated value

Type Details
value Numeric Value to schedule. Value is returned as the passed in type
pct_train float Training progress in percent. From a callback pass self.pct_train