vis4d.engine.callbacks

Callback modules.

class Callback(epoch_based=True, train_connector=None, test_connector=None)[source]

Base class for Callbacks.

Init callback.

Parameters:
  • epoch_based (bool, optional) – Whether the callback is epoch based. Defaults to False.

  • train_connector (None | CallbackConnector, optional) – Defines which kwargs to use during training for different callbacks. Defaults to None.

  • test_connector (None | CallbackConnector, optional) – Defines which kwargs to use during testing for different callbacks. Defaults to None.

get_test_callback_inputs(outputs, batch)[source]

Returns the data connector results for inference.

It extracts the required data from prediction and datas and passes it to the next component with the provided new key.

Parameters:
  • outputs (DictData) – Outputs of the model.

  • batch (DictData) – Batch data.

Returns:

Data connector results.

Return type:

dict[str, Tensor | DictStrArrNested]

Raises:

AssertionError – If test connector is None.

get_train_callback_inputs(outputs, batch)[source]

Returns the data connector results for training.

It extracts the required data from prediction and datas and passes it to the next component with the provided new key.

Parameters:
  • outputs (DictData) – Outputs of the model.

  • batch (DictData) – Batch data.

Returns:

Data connector results.

Return type:

dict[str, Tensor | DictStrArrNested]

Raises:

AssertionError – If train connector is None.

on_test_batch_end(trainer_state, model, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (Module) – Model that is being trained.

  • outputs (DictData) – Model prediction output.

  • batch (DictData) – Dataloader output data batch.

  • batch_idx (int) – Index of the batch.

  • dataloader_idx (int, optional) – Index of the dataloader. Defaults to 0.

Return type:

None

on_test_epoch_end(trainer_state, model)[source]

Hook to run at the end of a testing epoch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (nn.Module) – Model that is being trained.

Return type:

Optional[Dict[str, Union[float, int, Tensor]]]

on_test_epoch_start(trainer_state, model)[source]

Hook to run at the beginning of a testing epoch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (nn.Module) – Model that is being trained.

Return type:

None

on_train_batch_end(trainer_state, model, loss_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (Module) – Model that is being trained.

  • loss_module (LossModule) – Loss module.

  • outputs (DictData) – Model prediction output.

  • batch (DictData) – Dataloader output data batch.

  • batch_idx (int) – Index of the batch.

Return type:

Optional[Dict[str, Union[float, int, Tensor]]]

on_train_batch_start(trainer_state, model, loss_module, batch, batch_idx)[source]

Hook to run at the start of a training batch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (Module) – Model that is being trained.

  • loss_module (LossModule) – Loss module.

  • batch (DictData) – Dataloader output data batch.

  • batch_idx (int) – Index of the batch.

Return type:

None

on_train_epoch_end(trainer_state, model, loss_module)[source]

Hook to run at the end of a training epoch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (nn.Module) – Model that is being trained.

  • loss_module (LossModule) – Loss module.

Return type:

None

on_train_epoch_start(trainer_state, model, loss_module)[source]

Hook to run at the beginning of a training epoch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (nn.Module) – Model that is being trained.

  • loss_module (LossModule) – Loss module.

Return type:

None

setup()[source]

Setup callback.

Return type:

None

class CheckpointCallback(*args, save_prefix, checkpoint_period=1, **kwargs)[source]

Callback for model checkpointing.

Init callback.

Parameters:
  • save_prefix (str) – Prefix of checkpoint path for saving.

  • checkpoint_period (int, optional) – Checkpoint period. Defaults to 1.

on_train_batch_end(trainer_state, model, loss_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

None

on_train_epoch_end(trainer_state, model, loss_module)[source]

Hook to run at the end of a training epoch.

Return type:

None

setup()[source]

Setup callback.

Return type:

None

class EMACallback(epoch_based=True, train_connector=None, test_connector=None)[source]

Callback for EMA.

Init callback.

Parameters:
  • epoch_based (bool, optional) – Whether the callback is epoch based. Defaults to False.

  • train_connector (None | CallbackConnector, optional) – Defines which kwargs to use during training for different callbacks. Defaults to None.

  • test_connector (None | CallbackConnector, optional) – Defines which kwargs to use during testing for different callbacks. Defaults to None.

on_train_batch_end(trainer_state, model, loss_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

Optional[Dict[str, Union[float, int, Tensor]]]

class EvaluatorCallback(*args, evaluator, metrics_to_eval=None, save_predictions=False, save_prefix=None, **kwargs)[source]

Callback for model evaluation.

Parameters:
  • evaluator (Evaluator) – Evaluator.

  • metrics_to_eval (list[str], Optional) – Metrics to evaluate. If None, all metrics in the evaluator will be evaluated. Defaults to None.

  • save_predictions (bool) – If the predictions should be saved. Defaults to False.

  • save_prefix (str, Optional) – Output directory for saving the evaluation results. Defaults to None.

Init callback.

evaluate()[source]

Evaluate the performance after processing all input/output pairs.

Returns:

A dictionary containing the evaluation results. The

keys are formatted as {metric_name}/{key_name}, and the values are the corresponding evaluated values.

Return type:

MetricLogs

on_test_batch_end(trainer_state, model, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Return type:

None

on_test_epoch_end(trainer_state, model)[source]

Hook to run at the end of a testing epoch.

Return type:

Optional[Dict[str, Union[float, int, Tensor]]]

setup()[source]

Setup callback.

Return type:

None

class LoggingCallback(*args, refresh_rate=50, **kwargs)[source]

Callback for logging.

Init callback.

on_test_batch_end(trainer_state, model, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Return type:

None

on_test_epoch_start(trainer_state, model)[source]

Hook to run at the start of a testing epoch.

Return type:

None

on_train_batch_end(trainer_state, model, loss_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

Optional[Dict[str, Union[float, int, Tensor]]]

on_train_batch_start(trainer_state, model, loss_module, batch, batch_idx)[source]

Hook to run at the start of a training batch.

Return type:

None

on_train_epoch_start(trainer_state, model, loss_module)[source]

Hook to run at the start of a training epoch.

Return type:

None

class TrainerState[source]

State of the trainer.

current_epoch

Current epoch.

Type:

int

num_epochs

Total number of the training epochs.

Type:

int

global_step

Global step.

Type:

int

num_steps

Total number of the training steps.

Type:

int

train_dataloader

Training dataloader.

Type:

DataLoader[DictData] | None

num_train_batches

Number of training batches.

Type:

int | None

test_dataloader

List of test dataloaders.

Type:

list[DataLoader[DictData]] | None

num_test_batches

List of number of test batches.

Type:

list[int] | None

optimizers

List of optimizers.

Type:

NotRequired[list[Optimizer]]

metrics

Metrics for the logging.

Type:

NotRequired[dict[str, float]]

class VisualizerCallback(*args, visualizer, visualize_train=False, show=False, save_to_disk=True, save_prefix=None, **kwargs)[source]

Callback for model visualization.

Init callback.

Parameters:
  • visualizer (Visualizer) – Visualizer.

  • visualize_train (bool) – If the training data should be visualized. Defaults to False.

  • save_prefix (str) – Output directory for saving the visualizations.

  • show (bool) – If the visualizations should be shown. Defaults to False.

  • save_to_disk (bool) – If the visualizations should be saved to disk. Defaults to True.

on_test_batch_end(trainer_state, model, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Return type:

None

on_train_batch_end(trainer_state, model, loss_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

None

setup()[source]

Setup callback.

Return type:

None

class YOLOXModeSwitchCallback(*args, switch_epoch, **kwargs)[source]

Callback for switching the mode of YOLOX training.

Parameters:

switch_epoch (int) – Epoch to switch the mode.

Init callback.

on_train_epoch_end(trainer_state, model, loss_module)[source]

Hook to run at the end of a training epoch.

Return type:

None

class YOLOXSyncNormCallback(epoch_based=True, train_connector=None, test_connector=None)[source]

Callback for syncing the norm states of YOLOX training.

Init callback.

Parameters:
  • epoch_based (bool, optional) – Whether the callback is epoch based. Defaults to False.

  • train_connector (None | CallbackConnector, optional) – Defines which kwargs to use during training for different callbacks. Defaults to None.

  • test_connector (None | CallbackConnector, optional) – Defines which kwargs to use during testing for different callbacks. Defaults to None.

on_test_epoch_start(trainer_state, model)[source]

Hook to run at the beginning of a testing epoch.

Parameters:
  • trainer_state (TrainerState) – Trainer state.

  • model (nn.Module) – Model that is being trained.

Return type:

None

class YOLOXSyncRandomResizeCallback(*args, size_list, interval, **kwargs)[source]

Callback for syncing random resize during YOLOX training.

Init callback.

on_train_batch_start(trainer_state, model, loss_module, batch, batch_idx)[source]

Hook to run at the start of a training batch.

Return type:

None

Modules

vis4d.engine.callbacks.base

Base module for callbacks.

vis4d.engine.callbacks.checkpoint

This module contains utilities for callbacks.

vis4d.engine.callbacks.ema

Callback for updating EMA model.

vis4d.engine.callbacks.evaluator

This module contains utilities for callbacks.

vis4d.engine.callbacks.logging

This module contains utilities for callbacks.

vis4d.engine.callbacks.trainer_state

Trainer state for callbacks.

vis4d.engine.callbacks.visualizer

This module contains utilities for callbacks.

vis4d.engine.callbacks.yolox_callbacks

YOLOX-specific callbacks.