vis4d.engine.callbacks

Callback modules.

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

Base class for Callbacks.

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

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.

setup(trainer, pl_module, stage)[source]

Setup callback.

Return type:

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.

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.

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

Callback for EMA.

on_train_batch_end(trainer, pl_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

None

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

Callback for model evaluation.

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

Init callback.

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.

  • output_dir (str, Optional) – Output directory for saving the evaluation results.

setup(trainer, pl_module, stage)[source]

Setup callback.

Return type:

None

on_validation_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a validation batch.

Return type:

None

on_validation_epoch_end(trainer, pl_module)[source]

Wait for on_validation_epoch_end PL hook to call ‘evaluate’.

Return type:

None

on_test_batch_end(trainer, pl_module, 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, pl_module)[source]

Hook to run at the end of a testing epoch.

Return type:

None

run_eval()[source]

Run evaluation for the given evaluator.

Return type:

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

process()[source]

Process the evaluator.

Return type:

None

evaluate(metric)[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

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

Callback for logging.

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

Init callback.

on_train_epoch_start(trainer, pl_module)[source]

Hook to run at the start of a training epoch.

Return type:

None

on_train_batch_start(trainer, pl_module, batch, batch_idx)[source]

Hook to run at the start of a training batch.

Return type:

None

on_train_batch_end(trainer, pl_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

None

on_validation_epoch_start(trainer, pl_module)[source]

Hook to run at the start of a validation epoch.

Return type:

None

on_validation_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)[source]

Wait for on_validation_batch_end PL hook to call ‘process’.

Return type:

None

on_test_epoch_start(trainer, pl_module)[source]

Hook to run at the start of a testing epoch.

Return type:

None

on_test_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Return type:

None

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

Callback for model visualization.

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

Init callback.

Parameters:
  • visualizer (Visualizer) – Visualizer.

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

  • 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.

  • save_prefix (str) – Output directory prefix for distinguish different visualizations.

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

setup(trainer, pl_module, stage)[source]

Setup callback.

Return type:

None

on_train_batch_end(trainer, pl_module, outputs, batch, batch_idx)[source]

Hook to run at the end of a training batch.

Return type:

None

on_validation_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a validation batch.

Return type:

None

on_test_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)[source]

Hook to run at the end of a testing batch.

Return type:

None

save(cur_iter, stage)[source]

Save the visualizer state.

Return type:

None

class LRSchedulerCallback[source]

Callback to configure learning rate during training.

__init__()[source]

Initialize the callback.

on_train_batch_end(trainer, pl_module, outputs, batch, batch_idx)[source]

Hook on training batch end.

Return type:

None

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

Callback for switching the mode of YOLOX training.

__init__(*args, switch_epoch, **kwargs)[source]

Init callback.

Parameters:

switch_epoch (int) – Epoch to switch the mode.

on_train_epoch_end(trainer, pl_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.

on_test_epoch_start(trainer, pl_module)[source]

Hook to run at the beginning of a testing epoch.

Return type:

None

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

Callback for syncing random resize during YOLOX training.

__init__(*args, size_list, interval, **kwargs)[source]

Init callback.

on_train_batch_start(trainer, pl_module, batch, batch_idx)[source]

Hook to run at the start of a training batch.

Return type:

None

Modules

base

Base module for callbacks.

ema

Callback for updating EMA model.

evaluator

This module contains utilities for callbacks.

logging

This module contains utilities for callbacks.

scheduler

Callback to configure learning rate during training.

util

PyTorch Lightning callbacks utilities.

visualizer

This module contains utilities for callbacks.

yolox_callbacks

YOLOX-specific callbacks.