2023-04-14 20:36:16 +08:00
|
|
|
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
|
2021-08-15 03:17:51 +08:00
|
|
|
"""
|
|
|
|
Callback utils
|
|
|
|
"""
|
|
|
|
|
2022-08-21 09:47:37 +08:00
|
|
|
import threading
|
|
|
|
|
2021-08-01 06:18:07 +08:00
|
|
|
|
|
|
|
class Callbacks:
|
|
|
|
""""
|
|
|
|
Handles all registered callbacks for YOLOv5 Hooks
|
|
|
|
"""
|
2022-04-05 04:47:00 +08:00
|
|
|
|
2021-12-14 22:47:49 +08:00
|
|
|
def __init__(self):
|
|
|
|
# Define the available callbacks
|
|
|
|
self._callbacks = {
|
|
|
|
'on_pretrain_routine_start': [],
|
|
|
|
'on_pretrain_routine_end': [],
|
|
|
|
'on_train_start': [],
|
|
|
|
'on_train_epoch_start': [],
|
|
|
|
'on_train_batch_start': [],
|
|
|
|
'optimizer_step': [],
|
|
|
|
'on_before_zero_grad': [],
|
|
|
|
'on_train_batch_end': [],
|
|
|
|
'on_train_epoch_end': [],
|
|
|
|
'on_val_start': [],
|
|
|
|
'on_val_batch_start': [],
|
|
|
|
'on_val_image_end': [],
|
|
|
|
'on_val_batch_end': [],
|
|
|
|
'on_val_end': [],
|
|
|
|
'on_fit_epoch_end': [], # fit = train + val
|
|
|
|
'on_model_save': [],
|
|
|
|
'on_train_end': [],
|
2021-12-23 21:23:50 +08:00
|
|
|
'on_params_update': [],
|
2022-03-31 22:52:34 +08:00
|
|
|
'teardown': [],}
|
2022-01-23 10:37:21 +08:00
|
|
|
self.stop_training = False # set True to interrupt training
|
2021-08-01 06:18:07 +08:00
|
|
|
|
|
|
|
def register_action(self, hook, name='', callback=None):
|
|
|
|
"""
|
|
|
|
Register a new action to a callback hook
|
|
|
|
|
|
|
|
Args:
|
2022-04-07 22:15:01 +08:00
|
|
|
hook: The callback hook name to register the action to
|
|
|
|
name: The name of the action for later reference
|
|
|
|
callback: The callback to fire
|
2021-08-01 06:18:07 +08:00
|
|
|
"""
|
|
|
|
assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
|
|
|
|
assert callable(callback), f"callback '{callback}' is not callable"
|
|
|
|
self._callbacks[hook].append({'name': name, 'callback': callback})
|
|
|
|
|
|
|
|
def get_registered_actions(self, hook=None):
|
|
|
|
""""
|
|
|
|
Returns all the registered actions by callback hook
|
|
|
|
|
|
|
|
Args:
|
2022-04-07 22:15:01 +08:00
|
|
|
hook: The name of the hook to check, defaults to all
|
2021-08-01 06:18:07 +08:00
|
|
|
"""
|
2022-04-07 22:15:01 +08:00
|
|
|
return self._callbacks[hook] if hook else self._callbacks
|
2021-08-01 06:18:07 +08:00
|
|
|
|
2022-08-21 09:47:37 +08:00
|
|
|
def run(self, hook, *args, thread=False, **kwargs):
|
2021-08-01 06:18:07 +08:00
|
|
|
"""
|
2022-08-21 09:47:37 +08:00
|
|
|
Loop through the registered actions and fire all callbacks on main thread
|
2021-08-01 06:18:07 +08:00
|
|
|
|
2021-09-08 00:32:15 +08:00
|
|
|
Args:
|
2022-04-07 22:15:01 +08:00
|
|
|
hook: The name of the hook to check, defaults to all
|
|
|
|
args: Arguments to receive from YOLOv5
|
2022-08-21 09:47:37 +08:00
|
|
|
thread: (boolean) Run callbacks in daemon thread
|
2022-04-07 22:15:01 +08:00
|
|
|
kwargs: Keyword Arguments to receive from YOLOv5
|
2021-08-01 06:18:07 +08:00
|
|
|
"""
|
|
|
|
|
2021-09-08 00:32:15 +08:00
|
|
|
assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
|
|
|
|
for logger in self._callbacks[hook]:
|
2022-08-21 09:47:37 +08:00
|
|
|
if thread:
|
|
|
|
threading.Thread(target=logger['callback'], args=args, kwargs=kwargs, daemon=True).start()
|
|
|
|
else:
|
|
|
|
logger['callback'](*args, **kwargs)
|