'Saving best model with ModelCheckPointer and tf.saved_model

I am training a Unet model, and want to save the model using tf.saved_model to deploy via tf serving. How can the model be saved according to the desired format with a callback? Following is the snippet of callbacks.

checkpointer = tf.keras.callbacks.ModelCheckpoint('model_for_road_segmentation.h5', verbose=1, save_best_only=True)
log_dir = "logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir)
callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=2, monitor="val_loss"),
    tf.keras.callbacks.TensorBoard(log_dir="logs"),
    checkpointer,
    tensorboard_callback
]

Fitting the model

results = model.fit(train_batches, validation_data=validation_batches, \
                    batch_size=2, epochs=25, callbacks=callbacks
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source