You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
5.5 KiB
213 lines
5.5 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
2 years ago
|
"""
|
||
|
Base callbacks
|
||
|
"""
|
||
2 years ago
|
|
||
2 years ago
|
from collections import defaultdict
|
||
|
from copy import deepcopy
|
||
2 years ago
|
|
||
|
# Trainer callbacks ----------------------------------------------------------------------------------------------------
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_pretrain_routine_start(trainer):
|
||
2 years ago
|
"""Called before the pretraining routine starts."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_pretrain_routine_end(trainer):
|
||
2 years ago
|
"""Called after the pretraining routine ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_train_start(trainer):
|
||
2 years ago
|
"""Called when the training starts."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_train_epoch_start(trainer):
|
||
2 years ago
|
"""Called at the start of each training epoch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_train_batch_start(trainer):
|
||
2 years ago
|
"""Called at the start of each training batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def optimizer_step(trainer):
|
||
2 years ago
|
"""Called when the optimizer takes a step."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_before_zero_grad(trainer):
|
||
2 years ago
|
"""Called before the gradients are set to zero."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_train_batch_end(trainer):
|
||
2 years ago
|
"""Called at the end of each training batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_train_epoch_end(trainer):
|
||
2 years ago
|
"""Called at the end of each training epoch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_fit_epoch_end(trainer):
|
||
2 years ago
|
"""Called at the end of each fit epoch (train + val)."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_model_save(trainer):
|
||
2 years ago
|
"""Called when the model is saved."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_train_end(trainer):
|
||
2 years ago
|
"""Called when the training ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_params_update(trainer):
|
||
2 years ago
|
"""Called when the model parameters are updated."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def teardown(trainer):
|
||
2 years ago
|
"""Called during the teardown of the training process."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
# Validator callbacks --------------------------------------------------------------------------------------------------
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_val_start(validator):
|
||
2 years ago
|
"""Called when the validation starts."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_val_batch_start(validator):
|
||
2 years ago
|
"""Called at the start of each validation batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_val_batch_end(validator):
|
||
2 years ago
|
"""Called at the end of each validation batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_val_end(validator):
|
||
2 years ago
|
"""Called when the validation ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
# Predictor callbacks --------------------------------------------------------------------------------------------------
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_predict_start(predictor):
|
||
2 years ago
|
"""Called when the prediction starts."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_predict_batch_start(predictor):
|
||
2 years ago
|
"""Called at the start of each prediction batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_predict_batch_end(predictor):
|
||
2 years ago
|
"""Called at the end of each prediction batch."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_predict_postprocess_end(predictor):
|
||
2 years ago
|
"""Called after the post-processing of the prediction ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
def on_predict_end(predictor):
|
||
2 years ago
|
"""Called when the prediction ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
# Exporter callbacks ---------------------------------------------------------------------------------------------------
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def on_export_start(exporter):
|
||
2 years ago
|
"""Called when the model export starts."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
|
def on_export_end(exporter):
|
||
2 years ago
|
"""Called when the model export ends."""
|
||
2 years ago
|
pass
|
||
|
|
||
|
|
||
2 years ago
|
default_callbacks = {
|
||
2 years ago
|
# Run in trainer
|
||
2 years ago
|
'on_pretrain_routine_start': [on_pretrain_routine_start],
|
||
|
'on_pretrain_routine_end': [on_pretrain_routine_end],
|
||
|
'on_train_start': [on_train_start],
|
||
|
'on_train_epoch_start': [on_train_epoch_start],
|
||
|
'on_train_batch_start': [on_train_batch_start],
|
||
|
'optimizer_step': [optimizer_step],
|
||
|
'on_before_zero_grad': [on_before_zero_grad],
|
||
|
'on_train_batch_end': [on_train_batch_end],
|
||
|
'on_train_epoch_end': [on_train_epoch_end],
|
||
|
'on_fit_epoch_end': [on_fit_epoch_end], # fit = train + val
|
||
|
'on_model_save': [on_model_save],
|
||
|
'on_train_end': [on_train_end],
|
||
|
'on_params_update': [on_params_update],
|
||
|
'teardown': [teardown],
|
||
2 years ago
|
|
||
|
# Run in validator
|
||
2 years ago
|
'on_val_start': [on_val_start],
|
||
|
'on_val_batch_start': [on_val_batch_start],
|
||
|
'on_val_batch_end': [on_val_batch_end],
|
||
|
'on_val_end': [on_val_end],
|
||
2 years ago
|
|
||
|
# Run in predictor
|
||
2 years ago
|
'on_predict_start': [on_predict_start],
|
||
|
'on_predict_batch_start': [on_predict_batch_start],
|
||
2 years ago
|
'on_predict_postprocess_end': [on_predict_postprocess_end],
|
||
2 years ago
|
'on_predict_batch_end': [on_predict_batch_end],
|
||
|
'on_predict_end': [on_predict_end],
|
||
2 years ago
|
|
||
|
# Run in exporter
|
||
2 years ago
|
'on_export_start': [on_export_start],
|
||
|
'on_export_end': [on_export_end]}
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def get_default_callbacks():
|
||
2 years ago
|
"""
|
||
|
Return a copy of the default_callbacks dictionary with lists as default values.
|
||
|
|
||
|
Returns:
|
||
|
(defaultdict): A defaultdict with keys from default_callbacks and empty lists as default values.
|
||
|
"""
|
||
2 years ago
|
return defaultdict(list, deepcopy(default_callbacks))
|
||
|
|
||
|
|
||
2 years ago
|
def add_integration_callbacks(instance):
|
||
2 years ago
|
"""
|
||
|
Add integration callbacks from various sources to the instance's callbacks.
|
||
|
|
||
|
Args:
|
||
|
instance (Trainer, Predictor, Validator, Exporter): An object with a 'callbacks' attribute that is a dictionary
|
||
|
of callback lists.
|
||
|
"""
|
||
2 years ago
|
from .clearml import callbacks as clearml_cb
|
||
|
from .comet import callbacks as comet_cb
|
||
2 years ago
|
from .dvc import callbacks as dvc_cb
|
||
2 years ago
|
from .hub import callbacks as hub_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 tensorboard_cb
|
||
|
from .wb import callbacks as wb_cb
|
||
|
|
||
2 years ago
|
for x in clearml_cb, comet_cb, hub_cb, mlflow_cb, neptune_cb, tune_cb, tensorboard_cb, wb_cb, dvc_cb:
|
||
2 years ago
|
for k, v in x.items():
|
||
2 years ago
|
if v not in instance.callbacks[k]: # prevent duplicate callbacks addition
|
||
|
instance.callbacks[k].append(v) # callback[name].append(func)
|