0

With save_weights_only=False, how to configure the tf.keras.callbacks.ModelCheckpoint to save the SavedModel format instead of the HDF5 format?

There are two related issues regarding this. I am using TF2.4

https://github.com/tensorflow/tensorflow/issues/39679

https://github.com/keras-team/keras/issues/16657

thinkdeep
  • 945
  • 1
  • 14
  • 32
  • Check this https://stackoverflow.com/a/66914609/9215780, it contains many consideration (including yours). Let me know. – Innat Feb 28 '23 at 07:30

1 Answers1

0

The model will be saved into SavedModel format unless you specifically mention the HDF5 format.

In ModelCheckpoint save_weights_only=True means the model's weights will be saved (model.save_weights(filepath)). If you want to save the ModelCheckpoint without weights, you can specify save_weights_only=False which means the full model is saved (model.save(filepath)).

Please check the below code to save the ModelCheckpoint without saving the weights:

checkpoint_path = "training_2/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# Create a callback without saving the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=False,
                                                 verbose=1)
model.fit(..., callbacks=[cp_callback])

The model will save as the entire model in ['cp.ckpt'] file without saving the index file(that indicates which weights are stored in which shard and shards file (that contain your model's weights).

os.listdir(checkpoint_dir) #output - ['cp.ckpt']

Later you can load this full model saved checkpoint to retrain the model.

#Loads the weights

model.load_weights(checkpoint_path)
model.fit(...)
TF_Renu Patel
  • 356
  • 1
  • 4