128 lines
4.5 KiB
Python
128 lines
4.5 KiB
Python
from abc import ABC
|
|
from typing import Callable, List
|
|
|
|
from pytorch_lightning.callbacks import Callback
|
|
|
|
|
|
class TrainerCallbackHookMixin(ABC):
|
|
|
|
# this is just a summary on variables used in this abstract class,
|
|
# the proper values/initialisation should be done in child class
|
|
callbacks: List[Callback] = []
|
|
get_model: Callable = ...
|
|
|
|
def setup(self, stage: str):
|
|
"""Called in the beginning of fit and test"""
|
|
for callback in self.callbacks:
|
|
callback.setup(self, stage)
|
|
|
|
def teardown(self, stage: str):
|
|
"""Called at the end of fit and test"""
|
|
for callback in self.callbacks:
|
|
callback.teardown(self, stage)
|
|
|
|
def on_init_start(self):
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
|
for callback in self.callbacks:
|
|
callback.on_init_start(self)
|
|
|
|
def on_init_end(self):
|
|
"""Called when the trainer initialization ends, model has not yet been set."""
|
|
for callback in self.callbacks:
|
|
callback.on_init_end(self)
|
|
|
|
def on_fit_start(self):
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
|
for callback in self.callbacks:
|
|
callback.on_fit_start(self)
|
|
|
|
def on_fit_end(self):
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
|
for callback in self.callbacks:
|
|
callback.on_fit_end(self)
|
|
|
|
def on_sanity_check_start(self):
|
|
"""Called when the validation sanity check starts."""
|
|
for callback in self.callbacks:
|
|
callback.on_sanity_check_start(self, self.get_model())
|
|
|
|
def on_sanity_check_end(self):
|
|
"""Called when the validation sanity check ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_sanity_check_end(self, self.get_model())
|
|
|
|
def on_epoch_start(self):
|
|
"""Called when the epoch begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_epoch_start(self, self.get_model())
|
|
|
|
def on_epoch_end(self):
|
|
"""Called when the epoch ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_epoch_end(self, self.get_model())
|
|
|
|
def on_train_start(self):
|
|
"""Called when the train begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_train_start(self, self.get_model())
|
|
|
|
def on_train_end(self):
|
|
"""Called when the train ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_train_end(self, self.get_model())
|
|
|
|
def on_batch_start(self):
|
|
"""Called when the training batch begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_batch_start(self, self.get_model())
|
|
|
|
def on_batch_end(self):
|
|
"""Called when the training batch ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_batch_end(self, self.get_model())
|
|
|
|
def on_validation_batch_start(self):
|
|
"""Called when the validation batch begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_validation_batch_start(self, self.get_model())
|
|
|
|
def on_validation_batch_end(self):
|
|
"""Called when the validation batch ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_validation_batch_end(self, self.get_model())
|
|
|
|
def on_test_batch_start(self):
|
|
"""Called when the test batch begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_test_batch_start(self, self.get_model())
|
|
|
|
def on_test_batch_end(self):
|
|
"""Called when the test batch ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_test_batch_end(self, self.get_model())
|
|
|
|
def on_validation_start(self):
|
|
"""Called when the validation loop begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_validation_start(self, self.get_model())
|
|
|
|
def on_validation_end(self):
|
|
"""Called when the validation loop ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_validation_end(self, self.get_model())
|
|
|
|
def on_test_start(self):
|
|
"""Called when the test begins."""
|
|
for callback in self.callbacks:
|
|
callback.on_test_start(self, self.get_model())
|
|
|
|
def on_test_end(self):
|
|
"""Called when the test ends."""
|
|
for callback in self.callbacks:
|
|
callback.on_test_end(self, self.get_model())
|
|
|
|
def on_keyboard_interrupt(self):
|
|
"""Called when the training is interrupted by KeyboardInterrupt."""
|
|
for callback in self.callbacks:
|
|
callback.on_keyboard_interrupt(self, self.get_model())
|