Reference for ultralytics/utils/callbacks/base.py
Improvements
This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏
Summary
on_pretrain_routine_starton_pretrain_routine_endon_train_starton_train_epoch_starton_train_batch_startoptimizer_stepon_before_zero_gradon_train_batch_endon_train_epoch_endon_fit_epoch_endon_model_saveon_train_endon_params_updateteardownon_val_starton_val_batch_starton_val_batch_endon_val_endon_predict_starton_predict_batch_starton_predict_batch_endon_predict_postprocess_endon_predict_endon_export_starton_export_endget_default_callbacksadd_integration_callbacks
function ultralytics.utils.callbacks.base.on_pretrain_routine_start
def on_pretrain_routine_start(trainer)
Called before the pretraining routine starts.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_pretrain_routine_start(trainer):
"""Called before the pretraining routine starts."""
pass
function ultralytics.utils.callbacks.base.on_pretrain_routine_end
def on_pretrain_routine_end(trainer)
Called after the pretraining routine ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_pretrain_routine_end(trainer):
"""Called after the pretraining routine ends."""
pass
function ultralytics.utils.callbacks.base.on_train_start
def on_train_start(trainer)
Called when the training starts.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_start(trainer):
"""Called when the training starts."""
pass
function ultralytics.utils.callbacks.base.on_train_epoch_start
def on_train_epoch_start(trainer)
Called at the start of each training epoch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_epoch_start(trainer):
"""Called at the start of each training epoch."""
pass
function ultralytics.utils.callbacks.base.on_train_batch_start
def on_train_batch_start(trainer)
Called at the start of each training batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_batch_start(trainer):
"""Called at the start of each training batch."""
pass
function ultralytics.utils.callbacks.base.optimizer_step
def optimizer_step(trainer)
Called when the optimizer takes a step.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef optimizer_step(trainer):
"""Called when the optimizer takes a step."""
pass
function ultralytics.utils.callbacks.base.on_before_zero_grad
def on_before_zero_grad(trainer)
Called before the gradients are set to zero.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_before_zero_grad(trainer):
"""Called before the gradients are set to zero."""
pass
function ultralytics.utils.callbacks.base.on_train_batch_end
def on_train_batch_end(trainer)
Called at the end of each training batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_batch_end(trainer):
"""Called at the end of each training batch."""
pass
function ultralytics.utils.callbacks.base.on_train_epoch_end
def on_train_epoch_end(trainer)
Called at the end of each training epoch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_epoch_end(trainer):
"""Called at the end of each training epoch."""
pass
function ultralytics.utils.callbacks.base.on_fit_epoch_end
def on_fit_epoch_end(trainer)
Called at the end of each fit epoch (train + val).
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_fit_epoch_end(trainer):
"""Called at the end of each fit epoch (train + val)."""
pass
function ultralytics.utils.callbacks.base.on_model_save
def on_model_save(trainer)
Called when the model is saved.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_model_save(trainer):
"""Called when the model is saved."""
pass
function ultralytics.utils.callbacks.base.on_train_end
def on_train_end(trainer)
Called when the training ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_train_end(trainer):
"""Called when the training ends."""
pass
function ultralytics.utils.callbacks.base.on_params_update
def on_params_update(trainer)
Called when the model parameters are updated.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_params_update(trainer):
"""Called when the model parameters are updated."""
pass
function ultralytics.utils.callbacks.base.teardown
def teardown(trainer)
Called during the teardown of the training process.
Args
| Name | Type | Description | Default |
|---|---|---|---|
trainer | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef teardown(trainer):
"""Called during the teardown of the training process."""
pass
function ultralytics.utils.callbacks.base.on_val_start
def on_val_start(validator)
Called when the validation starts.
Args
| Name | Type | Description | Default |
|---|---|---|---|
validator | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_val_start(validator):
"""Called when the validation starts."""
pass
function ultralytics.utils.callbacks.base.on_val_batch_start
def on_val_batch_start(validator)
Called at the start of each validation batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
validator | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_val_batch_start(validator):
"""Called at the start of each validation batch."""
pass
function ultralytics.utils.callbacks.base.on_val_batch_end
def on_val_batch_end(validator)
Called at the end of each validation batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
validator | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_val_batch_end(validator):
"""Called at the end of each validation batch."""
pass
function ultralytics.utils.callbacks.base.on_val_end
def on_val_end(validator)
Called when the validation ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
validator | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_val_end(validator):
"""Called when the validation ends."""
pass
function ultralytics.utils.callbacks.base.on_predict_start
def on_predict_start(predictor)
Called when the prediction starts.
Args
| Name | Type | Description | Default |
|---|---|---|---|
predictor | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_predict_start(predictor):
"""Called when the prediction starts."""
pass
function ultralytics.utils.callbacks.base.on_predict_batch_start
def on_predict_batch_start(predictor)
Called at the start of each prediction batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
predictor | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_predict_batch_start(predictor):
"""Called at the start of each prediction batch."""
pass
function ultralytics.utils.callbacks.base.on_predict_batch_end
def on_predict_batch_end(predictor)
Called at the end of each prediction batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
predictor | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_predict_batch_end(predictor):
"""Called at the end of each prediction batch."""
pass
function ultralytics.utils.callbacks.base.on_predict_postprocess_end
def on_predict_postprocess_end(predictor)
Called after the post-processing of the prediction ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
predictor | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_predict_postprocess_end(predictor):
"""Called after the post-processing of the prediction ends."""
pass
function ultralytics.utils.callbacks.base.on_predict_end
def on_predict_end(predictor)
Called when the prediction ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
predictor | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_predict_end(predictor):
"""Called when the prediction ends."""
pass
function ultralytics.utils.callbacks.base.on_export_start
def on_export_start(exporter)
Called when the model export starts.
Args
| Name | Type | Description | Default |
|---|---|---|---|
exporter | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_export_start(exporter):
"""Called when the model export starts."""
pass
function ultralytics.utils.callbacks.base.on_export_end
def on_export_end(exporter)
Called when the model export ends.
Args
| Name | Type | Description | Default |
|---|---|---|---|
exporter | required |
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef on_export_end(exporter):
"""Called when the model export ends."""
pass
function ultralytics.utils.callbacks.base.get_default_callbacks
def get_default_callbacks()
Get the default callbacks for Ultralytics training, validation, prediction, and export processes.
Returns
| Type | Description |
|---|---|
dict | Dictionary of default callbacks for various training events. Each key represents an event during the |
Examples
>>> callbacks = get_default_callbacks()
>>> print(list(callbacks.keys())) # show all available callback events
['on_pretrain_routine_start', 'on_pretrain_routine_end', ...]
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef get_default_callbacks():
"""Get the default callbacks for Ultralytics training, validation, prediction, and export processes.
Returns:
(dict): Dictionary of default callbacks for various training events. Each key represents an event during the
training process, and the corresponding value is a list of callback functions executed when that
event occurs.
Examples:
>>> callbacks = get_default_callbacks()
>>> print(list(callbacks.keys())) # show all available callback events
['on_pretrain_routine_start', 'on_pretrain_routine_end', ...]
"""
return defaultdict(list, deepcopy(default_callbacks))
function ultralytics.utils.callbacks.base.add_integration_callbacks
def add_integration_callbacks(instance)
Add integration callbacks to the instance's callbacks dictionary.
This function loads and adds various integration callbacks to the provided instance. The specific callbacks added depend on the type of instance provided. All instances receive HUB callbacks, while Trainer instances also receive additional callbacks for various integrations like ClearML, Comet, DVC, MLflow, Neptune, Ray Tune, TensorBoard, and Weights & Biases.
Args
| Name | Type | Description | Default |
|---|---|---|---|
instance | Trainer | Predictor | Validator | Exporter | The object instance to which callbacks will be added. The type of instance determines which callbacks are loaded. | required |
Examples
>>> from ultralytics.engine.trainer import BaseTrainer
>>> trainer = BaseTrainer()
>>> add_integration_callbacks(trainer)
Source code in ultralytics/utils/callbacks/base.py
View on GitHubdef add_integration_callbacks(instance):
"""Add integration callbacks to the instance's callbacks dictionary.
This function loads and adds various integration callbacks to the provided instance. The specific callbacks added
depend on the type of instance provided. All instances receive HUB callbacks, while Trainer instances also receive
additional callbacks for various integrations like ClearML, Comet, DVC, MLflow, Neptune, Ray Tune, TensorBoard, and
Weights & Biases.
Args:
instance (Trainer | Predictor | Validator | Exporter): The object instance to which callbacks will be added. The
type of instance determines which callbacks are loaded.
Examples:
>>> from ultralytics.engine.trainer import BaseTrainer
>>> trainer = BaseTrainer()
>>> add_integration_callbacks(trainer)
"""
from .hub import callbacks as hub_cb
from .platform import callbacks as platform_cb
# Load Ultralytics callbacks
callbacks_list = [hub_cb, platform_cb]
# Load training callbacks
if "Trainer" in instance.__class__.__name__:
from .clearml import callbacks as clear_cb
from .comet import callbacks as comet_cb
from .dvc import callbacks as dvc_cb
from .mlflow import callbacks as mlflow_cb
from .neptune import callbacks as neptune_cb
from .raytune import callbacks as tune_cb
from .tensorboard import callbacks as tb_cb
from .wb import callbacks as wb_cb
callbacks_list.extend([clear_cb, comet_cb, dvc_cb, mlflow_cb, neptune_cb, tune_cb, tb_cb, wb_cb])
# Add the callbacks to the callbacks dictionary
for callbacks in callbacks_list:
for k, v in callbacks.items():
if v not in instance.callbacks[k]:
instance.callbacks[k].append(v)