Skip to content

Reference for ultralytics/utils/callbacks/base.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/callbacks/base.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.utils.callbacks.base.on_pretrain_routine_start

on_pretrain_routine_start(trainer)

Called before the pretraining routine starts.

Source code in ultralytics/utils/callbacks/base.py
10
11
12
def on_pretrain_routine_start(trainer):
    """Called before the pretraining routine starts."""
    pass





ultralytics.utils.callbacks.base.on_pretrain_routine_end

on_pretrain_routine_end(trainer)

Called after the pretraining routine ends.

Source code in ultralytics/utils/callbacks/base.py
15
16
17
def on_pretrain_routine_end(trainer):
    """Called after the pretraining routine ends."""
    pass





ultralytics.utils.callbacks.base.on_train_start

on_train_start(trainer)

Called when the training starts.

Source code in ultralytics/utils/callbacks/base.py
20
21
22
def on_train_start(trainer):
    """Called when the training starts."""
    pass





ultralytics.utils.callbacks.base.on_train_epoch_start

on_train_epoch_start(trainer)

Called at the start of each training epoch.

Source code in ultralytics/utils/callbacks/base.py
25
26
27
def on_train_epoch_start(trainer):
    """Called at the start of each training epoch."""
    pass





ultralytics.utils.callbacks.base.on_train_batch_start

on_train_batch_start(trainer)

Called at the start of each training batch.

Source code in ultralytics/utils/callbacks/base.py
30
31
32
def on_train_batch_start(trainer):
    """Called at the start of each training batch."""
    pass





ultralytics.utils.callbacks.base.optimizer_step

optimizer_step(trainer)

Called when the optimizer takes a step.

Source code in ultralytics/utils/callbacks/base.py
35
36
37
def optimizer_step(trainer):
    """Called when the optimizer takes a step."""
    pass





ultralytics.utils.callbacks.base.on_before_zero_grad

on_before_zero_grad(trainer)

Called before the gradients are set to zero.

Source code in ultralytics/utils/callbacks/base.py
40
41
42
def on_before_zero_grad(trainer):
    """Called before the gradients are set to zero."""
    pass





ultralytics.utils.callbacks.base.on_train_batch_end

on_train_batch_end(trainer)

Called at the end of each training batch.

Source code in ultralytics/utils/callbacks/base.py
45
46
47
def on_train_batch_end(trainer):
    """Called at the end of each training batch."""
    pass





ultralytics.utils.callbacks.base.on_train_epoch_end

on_train_epoch_end(trainer)

Called at the end of each training epoch.

Source code in ultralytics/utils/callbacks/base.py
50
51
52
def on_train_epoch_end(trainer):
    """Called at the end of each training epoch."""
    pass





ultralytics.utils.callbacks.base.on_fit_epoch_end

on_fit_epoch_end(trainer)

Called at the end of each fit epoch (train + val).

Source code in ultralytics/utils/callbacks/base.py
55
56
57
def on_fit_epoch_end(trainer):
    """Called at the end of each fit epoch (train + val)."""
    pass





ultralytics.utils.callbacks.base.on_model_save

on_model_save(trainer)

Called when the model is saved.

Source code in ultralytics/utils/callbacks/base.py
60
61
62
def on_model_save(trainer):
    """Called when the model is saved."""
    pass





ultralytics.utils.callbacks.base.on_train_end

on_train_end(trainer)

Called when the training ends.

Source code in ultralytics/utils/callbacks/base.py
65
66
67
def on_train_end(trainer):
    """Called when the training ends."""
    pass





ultralytics.utils.callbacks.base.on_params_update

on_params_update(trainer)

Called when the model parameters are updated.

Source code in ultralytics/utils/callbacks/base.py
70
71
72
def on_params_update(trainer):
    """Called when the model parameters are updated."""
    pass





ultralytics.utils.callbacks.base.teardown

teardown(trainer)

Called during the teardown of the training process.

Source code in ultralytics/utils/callbacks/base.py
75
76
77
def teardown(trainer):
    """Called during the teardown of the training process."""
    pass





ultralytics.utils.callbacks.base.on_val_start

on_val_start(validator)

Called when the validation starts.

Source code in ultralytics/utils/callbacks/base.py
83
84
85
def on_val_start(validator):
    """Called when the validation starts."""
    pass





ultralytics.utils.callbacks.base.on_val_batch_start

on_val_batch_start(validator)

Called at the start of each validation batch.

Source code in ultralytics/utils/callbacks/base.py
88
89
90
def on_val_batch_start(validator):
    """Called at the start of each validation batch."""
    pass





ultralytics.utils.callbacks.base.on_val_batch_end

on_val_batch_end(validator)

Called at the end of each validation batch.

Source code in ultralytics/utils/callbacks/base.py
93
94
95
def on_val_batch_end(validator):
    """Called at the end of each validation batch."""
    pass





ultralytics.utils.callbacks.base.on_val_end

on_val_end(validator)

Called when the validation ends.

Source code in ultralytics/utils/callbacks/base.py
 98
 99
100
def on_val_end(validator):
    """Called when the validation ends."""
    pass





ultralytics.utils.callbacks.base.on_predict_start

on_predict_start(predictor)

Called when the prediction starts.

Source code in ultralytics/utils/callbacks/base.py
106
107
108
def on_predict_start(predictor):
    """Called when the prediction starts."""
    pass





ultralytics.utils.callbacks.base.on_predict_batch_start

on_predict_batch_start(predictor)

Called at the start of each prediction batch.

Source code in ultralytics/utils/callbacks/base.py
111
112
113
def on_predict_batch_start(predictor):
    """Called at the start of each prediction batch."""
    pass





ultralytics.utils.callbacks.base.on_predict_batch_end

on_predict_batch_end(predictor)

Called at the end of each prediction batch.

Source code in ultralytics/utils/callbacks/base.py
116
117
118
def on_predict_batch_end(predictor):
    """Called at the end of each prediction batch."""
    pass





ultralytics.utils.callbacks.base.on_predict_postprocess_end

on_predict_postprocess_end(predictor)

Called after the post-processing of the prediction ends.

Source code in ultralytics/utils/callbacks/base.py
121
122
123
def on_predict_postprocess_end(predictor):
    """Called after the post-processing of the prediction ends."""
    pass





ultralytics.utils.callbacks.base.on_predict_end

on_predict_end(predictor)

Called when the prediction ends.

Source code in ultralytics/utils/callbacks/base.py
126
127
128
def on_predict_end(predictor):
    """Called when the prediction ends."""
    pass





ultralytics.utils.callbacks.base.on_export_start

on_export_start(exporter)

Called when the model export starts.

Source code in ultralytics/utils/callbacks/base.py
134
135
136
def on_export_start(exporter):
    """Called when the model export starts."""
    pass





ultralytics.utils.callbacks.base.on_export_end

on_export_end(exporter)

Called when the model export ends.

Source code in ultralytics/utils/callbacks/base.py
139
140
141
def on_export_end(exporter):
    """Called when the model export ends."""
    pass





ultralytics.utils.callbacks.base.get_default_callbacks

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 in the dictionary represents an event during the training process, and the corresponding value is a list of callback functions that are 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', ...]
Source code in ultralytics/utils/callbacks/base.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def 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 in the dictionary represents an
            event during the training process, and the corresponding value is a list of callback functions that are
            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))





ultralytics.utils.callbacks.base.add_integration_callbacks

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.

Parameters:

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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:
        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)
    """
    # Load HUB callbacks
    from .hub import callbacks as hub_cb

    callbacks_list = [hub_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)





📅 Created 1 year ago ✏️ Updated 7 months ago