What's the recommended way to load Tensorflow 2 saved models

I have a general question regarding the Tensorflow 2 API when it comes to use saved models.

The guideline on this post explains how to save and load TF models

I am wondering why in the post the saved model gets loaded like this:

model = tf.keras.models.load_model('saved_model_folder')

Is this way of loading the model equal to

model = tf.saved_model.load('saved_model_folder')

I am asking because I read that the Keras H5 format is the old format and it is recommended to use the saved model format.

See here: Save and load Keras models  |  TensorFlow Core

When I save my model like this

model.save(saved_model_folder)

I get the follwoing files

image

It looks like I get both the Keras H5 and the saved model file

I would perfer to use “new_model = tf.keras.models.load_model(‘saved_model_folder’)” because it gives me a better handling of the model. I can directly call “model.predict()” which is not possible when I load the model via “model = tf.saved_model.load(‘saved_model_folder’)”.

You can see:

https://www.tensorflow.org/api_docs/python/tf/saved_model/load

Loading Keras models
Keras models are trackable, so they can be saved to SavedModel. The object returned by tf.saved_model.load is not a Keras object (i.e. doesn’t have .fit, .predict, etc. methods). A few attributes and functions are still available: .variables, .trainable_variables and .__call__.
Use tf.keras.models.load_model to restore the Keras model.

1 Like

Those are the main differences.

Using the keras save/load can restore the keras objects, using that additional keras metadata.

But those keras classes can only be restored in python.

If you need some custom function, like predict, in a non-keras saved_model you implement it as a tf.function-method attached to your model.