Skip to content
Snippets Groups Projects
Commit 5843d2f6 authored by lukas leufen's avatar lukas leufen
Browse files

added docs

parent 0b171807
Branches
Tags
2 merge requests!37include new development,!29Lukas issue030 feat continue training
Pipeline #28880 passed
...@@ -12,10 +12,23 @@ from keras.callbacks import History, ModelCheckpoint ...@@ -12,10 +12,23 @@ from keras.callbacks import History, ModelCheckpoint
class HistoryAdvanced(History): class HistoryAdvanced(History):
"""
This is almost an identical clone of the original History class. The only difference is that attributes epoch and
history are instantiated during the init phase and not during on_train_begin. This is required to resume an already
started but disrupted training from an saved state. This HistoryAdvanced callback needs to be added separately as
additional callback. To get the full history use this object for further steps instead of the default return of
training methods like fit_generator().
hist = HistoryAdvanced()
history = model.fit_generator(generator=.... , callbacks=[hist])
history = hist
def __init__(self, old_epoch=None, old_history=None): If training was started from beginning this class is identical to the returned history class object.
self.epoch = old_epoch or [] """
self.history = old_history or {}
def __init__(self):
self.epoch = []
self.history = {}
super().__init__() super().__init__()
def on_train_begin(self, logs=None): def on_train_begin(self, logs=None):
...@@ -78,20 +91,46 @@ class LearningRateDecay(History): ...@@ -78,20 +91,46 @@ class LearningRateDecay(History):
class ModelCheckpointAdvanced(ModelCheckpoint): class ModelCheckpointAdvanced(ModelCheckpoint):
""" """
IMPORTANT: Always add the model checkpoint advanced as last callback to properly update all tracked callbacks, e.g. Enhance the standard ModelCheckpoint class by additional saves of given callbacks. Specify this callbacks as follow:
fit_generator(callbacks=[..., <last_here>])
lr = CustomLearningRate()
hist = CustomHistory()
callbacks_name = "your_custom_path_%s.pickle"
callbacks = [{"callback": lr, "path": callbacks_name % "lr"},
{"callback": hist, "path": callbacks_name % "hist"}]
ckpt_callbacks = ModelCheckpointAdvanced(filepath=.... , callbacks=callbacks)
Add this ckpt_callbacks as all other additional callbacks to the callback list. IMPORTANT: Always add ckpt_callbacks
as last callback to properly update all tracked callbacks, e.g.
fit_generator(.... , callbacks=[lr, hist, ckpt_callbacks])
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.callbacks = kwargs.pop("callbacks") self.callbacks = kwargs.pop("callbacks")
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def update_best(self, hist): def update_best(self, hist):
"""
Update internal best on resuming a training process. Otherwise best is set to +/- inf depending on the
performance metric and the first trained model (first of the resuming training process) will always saved as
best model because its performance will be better than infinity. To prevent this behaviour and compare the
performance with the best model performance, call this method before resuming the training process.
:param hist: The History object from the previous (interrupted) training.
"""
self.best = hist.history.get(self.monitor)[-1] self.best = hist.history.get(self.monitor)[-1]
def update_callbacks(self, callbacks): def update_callbacks(self, callbacks):
"""
Update all stored callback objects. The argument callbacks needs to follow the same convention like described
in the class description (list of dictionaries). Must be run before resuming a training process.
"""
self.callbacks = callbacks self.callbacks = callbacks
def on_epoch_end(self, epoch, logs=None): def on_epoch_end(self, epoch, logs=None):
"""
Save model as usual (see ModelCheckpoint class), but also save additional callbacks.
"""
super().on_epoch_end(epoch, logs) super().on_epoch_end(epoch, logs)
for callback in self.callbacks: for callback in self.callbacks:
...@@ -100,9 +139,12 @@ class ModelCheckpointAdvanced(ModelCheckpoint): ...@@ -100,9 +139,12 @@ class ModelCheckpointAdvanced(ModelCheckpoint):
if self.save_best_only: if self.save_best_only:
current = logs.get(self.monitor) current = logs.get(self.monitor)
if current == self.best: if current == self.best:
if self.verbose > 0:
print('\nEpoch %05d: save to %s' % (epoch + 1, file_path)) print('\nEpoch %05d: save to %s' % (epoch + 1, file_path))
with open(file_path, "wb") as f: with open(file_path, "wb") as f:
pickle.dump(callback["callback"], f) pickle.dump(callback["callback"], f)
else: else:
with open(file_path, "wb") as f: with open(file_path, "wb") as f:
if self.verbose > 0:
print('\nEpoch %05d: save to %s' % (epoch + 1, file_path))
pickle.dump(callback["callback"], f) pickle.dump(callback["callback"], f)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment