Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/utils/callbacks.py
Views: 475
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license1"""2Callback utils3"""456class Callbacks:7""""8Handles all registered callbacks for YOLOv5 Hooks9"""1011def __init__(self):12# Define the available callbacks13self._callbacks = {14'on_pretrain_routine_start': [],15'on_pretrain_routine_end': [],1617'on_train_start': [],18'on_train_epoch_start': [],19'on_train_batch_start': [],20'optimizer_step': [],21'on_before_zero_grad': [],22'on_train_batch_end': [],23'on_train_epoch_end': [],2425'on_val_start': [],26'on_val_batch_start': [],27'on_val_image_end': [],28'on_val_batch_end': [],29'on_val_end': [],3031'on_fit_epoch_end': [], # fit = train + val32'on_model_save': [],33'on_train_end': [],34'on_params_update': [],35'teardown': [],36}3738def register_action(self, hook, name='', callback=None):39"""40Register a new action to a callback hook4142Args:43hook The callback hook name to register the action to44name The name of the action for later reference45callback The callback to fire46"""47assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"48assert callable(callback), f"callback '{callback}' is not callable"49self._callbacks[hook].append({'name': name, 'callback': callback})5051def get_registered_actions(self, hook=None):52""""53Returns all the registered actions by callback hook5455Args:56hook The name of the hook to check, defaults to all57"""58if hook:59return self._callbacks[hook]60else:61return self._callbacks6263def run(self, hook, *args, **kwargs):64"""65Loop through the registered actions and fire all callbacks6667Args:68hook The name of the hook to check, defaults to all69args Arguments to receive from YOLOv570kwargs Keyword Arguments to receive from YOLOv571"""7273assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"7475for logger in self._callbacks[hook]:76logger['callback'](*args, **kwargs)777879