Deep Learning with Keras

By LiterallyTheOne

7: Callbacks

Deep Learning with Keras Tutorial, Callbacks

Introduction

  • Previous Tutorial: Preprocessing and Data Augmentation
  • This Tutorial: Callbacks
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

Callbacks

  • A function
  • Pass to Fit function
  • Gets called in a specific moment
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

TensorBoard

from keras.callbacks import TensorBoard

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = TensorBoard(log_dir=log_dir)

history = model.fit(
    ...,
    callbacks=[tensorboard_callback],
)

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

EarlyStopping

  • Stops the training
  • No improvement
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

EarlyStopping Example

from keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(
    monitor="val_loss",
    patience=5,
    restore_best_weights=True,
    verbose=1,
)

history = model.fit(
    ...,
    epochs=200,
    callbacks=[tensorboard_callback, early_stopping],
)
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

ModelCheckpoint

  • Saves the model
  • During training
  • Helps us
    • Not to loose the model
    • Have the previous weights to continue from them
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

ModelCheckpoint Example

from keras.callbacks import ModelCheckpoint

model_checkpoint = ModelCheckpoint(
    filepath="checkpoints/best_model.weights.h5",
    monitor="val_loss",
    save_best_only=True,
    save_weights_only=True,
    verbose=1,
)

history = model.fit(
    ...,
    callbacks=[tensorboard_callback, early_stopping, model_checkpoint],
)
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

Save only Weights

  • If save_weights_only=True:
    • file name should end with: .weights.h5
  • Helps us:
    • Capacity
    • Multi-platform
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

Load the best weights

model.load_weights("checkpoints/best_model.weights.h5")
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Callbacks

By Ramin Zarebidoky (LiterallyTheOne)