Hooks

class classy_vision.hooks.CheckpointHook(checkpoint_folder: str, input_args: Any, phase_types: Optional[Collection[str]] = None, checkpoint_period: int = 1)

Hook to checkpoint a model’s task.

Saves the checkpoints in checkpoint_folder.

__init__(checkpoint_folder: str, input_args: Any, phase_types: Optional[Collection[str]] = None, checkpoint_period: int = 1) → None

The constructor method of CheckpointHook.

Parameters
  • checkpoint_folder – Folder to store checkpoints in

  • input_args – Any arguments to save about the runtime setup. For example, it is useful to store the config that was used to instantiate the model.

  • phase_types – If phase_types is specified, only checkpoint on those phase types. Each item in phase_types must be either “train” or “test”.

  • checkpoint_period – Checkpoint at the end of every x phases (default 1)

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Checkpoint the task every checkpoint_period phases.

We do not necessarily checkpoint the task at the end of every phase.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the start of training.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

class classy_vision.hooks.ClassyHook

Base class for hooks.

Hooks allow to inject behavior at different places of the training loop, which are listed below in the chronological order.

on_start -> on_phase_start -> on_forward -> on_loss_and_meter ->

on_update -> on_phase_end -> on_end

Deriving classes should call super().__init__() and store any state in self.state. Any state added to this property should be serializable. E.g. -

class MyHook(ClassyHook):
    def __init__(self, a, b):
        super().__init__()
        self.state.a = [1,2,3]
        self.state.b = "my_hook"
        # the following line is not allowed
        # self.state.my_lambda = lambda x: x^2
__init__()

Initialize self. See help(type(self)) for accurate signature.

get_classy_state() → Dict[str, Any]

Get the state of the ClassyHook.

The returned state is used for checkpointing.

Returns

A state dictionary containing the state of the hook.

classmethod name() → str

Returns the name of the class.

abstract on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the end of training.

abstract on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called each time forward pass is done in the model.

abstract on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called each time after a loss has been computed and meters are updated.

abstract on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the end of each phase (epoch).

abstract on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the start of each phase.

abstract on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the start of training.

abstract on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called each time after parameters have been updated by the optimizer.

set_classy_state(state_dict: Dict[str, Any]) → None

Set the state of the ClassyHook.

Parameters

state_dict – The state dictionary. Must be the output of a call to get_classy_state().

This is used to load the state of the hook from a checkpoint.

class classy_vision.hooks.ClassyHookFunctions

Enumeration of all the hook functions in the ClassyHook class.

on_end = 7
on_forward = 3
on_loss_and_meter = 4
on_phase_end = 6
on_phase_start = 2
on_start = 1
on_update = 5
class classy_vision.hooks.ExponentialMovingAverageModelHook(decay: float, consider_bn_buffers: bool = True, device: str = 'cpu')

Hook which keeps a track of the exponential moving average (EMA) of the model’s parameters and applies the EMA params to the model during the test phases.

Saving the state in cpu will save gpu memory, but will make training slower since the model parameters will need to be moved to cpu before the averaging.

Note

This hooks stores two additional copies of the model’s parameters, which will increase memory usage significantly.

__init__(decay: float, consider_bn_buffers: bool = True, device: str = 'cpu') → None

The constructor method of ExponentialMovingAverageModelHook.

Parameters
  • decay – EMA decay factor, should be in [0, 1]. A decay of 0 corresponds to always using the latest value (no EMA) and a decay of 1 corresponds to not updating weights after initialization.

  • consider_bn_buffers – Whether to apply EMA to batch norm buffers

  • device – Device to store the model state.

get_model_state_iterator(model: torch.nn.modules.module.Module) → Iterable[Tuple[str, Any]]

Get an iterator over the model state to apply EMA to.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the end of each phase (epoch).

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the start of each phase.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called at the start of training.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Called each time after parameters have been updated by the optimizer.

set_model_state(task: classy_vision.tasks.classy_task.ClassyTask, use_ema: bool) → None

Depending on use_ema, set the appropriate state for the model.

class classy_vision.hooks.LossLrMeterLoggingHook(log_freq: Optional[int] = None)

Logs the loss, optimizer LR, and meters. Logs at the end of a phase.

__init__(log_freq: Optional[int] = None) → None

The constructor method of LossLrMeterLoggingHook.

Parameters

log_freq – if specified, also logs every log_freq batches.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Log metrics every log_freq batches, if log_freq is not None.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Log the loss, optimizer LR, and meters for the phase.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Log the LR every log_freq batches, if log_freq is not None.

class classy_vision.hooks.TensorboardPlotHook(tb_writer)

Hook for writing the losses, learning rates and meters to tensorboard <https ://www.tensorflow.org/tensorboard>_.

Global steps are counted in terms of the number of samples processed.

__init__(tb_writer) → None

The constructor method of TensorboardPlotHook.

Parameters
  • tb_writer`Tensorboard SummaryWriter <https://tensorboardx.

  • readthedocs.io/en/latest/tensorboard.html#tensorboardX.

  • instance (SummaryWriter>`_) –

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Add the losses and learning rates to tensorboard.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Initialize losses and learning_rates.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Store the observed learning rates.

class classy_vision.hooks.ModelComplexityHook

Logs the number of paramaters and forward pass FLOPs of the model.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Measure number of parameters and number of FLOPs.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

class classy_vision.hooks.ModelTensorboardHook(tb_writer)

Shows the model graph in TensorBoard <https ://www.tensorflow.org/tensorboard>_.

__init__(tb_writer) → None

The constructor method of ModelTensorboardHook.

Parameters
  • tb_writer`Tensorboard SummaryWriter <https://tensorboardx.

  • readthedocs.io/en/latest/tensorboard.html#tensorboardX.

  • instance (SummaryWriter>`_) –

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Plot the model on Tensorboard.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

class classy_vision.hooks.ProfilerHook
Hook to profile a model and to show model runtime information, such as

the time breakdown in milliseconds of forward/backward pass.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Profile the forward pass.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

class classy_vision.hooks.ProgressBarHook

Displays a progress bar to show progress in processing batches.

__init__() → None

The constructor method of ProgressBarHook.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Clear the progress bar at the end of the phase.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Create and display a progress bar with 0 progress.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Update the progress bar with the batch size.

class classy_vision.hooks.TimeMetricsHook(log_freq: Optional[int] = None)

Computes and prints performance metrics. Logs at the end of a phase.

__init__(log_freq: Optional[int] = None) → None
Parameters

log_freq – if specified, logs every log_freq batches also.

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Log metrics every log_freq batches, if log_freq is not None.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Log metrics at the end of a phase if log_freq is None.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Initialize start time and reset perf stats

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

class classy_vision.hooks.VisdomHook(server: str, port: str, env: str = 'main', title_suffix: str = '')

Plots metrics on to Visdom.

Visdom is a flexible tool for creating, organizing, and sharing visualizations

of live, rich data. It supports Python.

__init__(server: str, port: str, env: str = 'main', title_suffix: str = '') → None
Parameters
  • server – host name of the visdom server

  • port – port of visdom server, such as 8097

  • env – environment of visdom

  • title_suffix – suffix that will be appended to the title

on_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_forward(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_loss_and_meter(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_phase_end(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Plot the metrics on visdom.

on_phase_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_start(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.

on_update(task: classy_vision.tasks.classy_task.ClassyTask, local_variables: Dict[str, Any]) → None

Derived classes can set their hook functions to this.

This is useful if they want those hook functions to not do anything.