Difference between tf.saved_model.save(model, path_to_dir) and tf.keras.model.save

Viewed 3423

I have a keras model and want to save it as a tensorflow graph. Is there a difference between tf.saved_model.save(model, path_to_dir) and tf.keras.model.save.

In both of these, I want to save in a tensorflow saved format and will not be using h5 format. I understand tf.saved_model.save is more generic but if I am using a keras model are these two different in anyways.

2 Answers

In general cases, there should not be any difference. To give a concrete answer, we can save the entire tf. keras model - one is TensorFlow SavedModel and another one is .h5 format.

As you said, you have a keras model and wanted to save its graph. You can do this either

  • Model.save or
  • tf.keras.models.save_model
(1)
Model.save(
    filepath,
    overwrite=True,
    include_optimizer=True,
    save_format=None,
    signatures=None,
    options=None,
    save_traces=True,
)

(2)
tf.keras.models.save_model(
    model,
    filepath,
    overwrite=True,
    include_optimizer=True,
    save_format=None,
    signatures=None,
    options=None,
    save_traces=True,
)

(3)
# tf.saved_model.save(model, path_to_dir)
tf.saved_model.save(model,
                    export_dir, 
                    signatures, 
                    options)

As you can see, all of them take the same argument too. I would recommend choosing (1) or (2), easy to follow. Here is an example, I've trained a function model on the CIFAR data set and saved the model as follows with (1) and (2) approaches.

func_model.fit(x_train, y_train, batch_size=128, epochs=2, verbose = 1)
1s/step - loss: 2.7365 - categorical_accuracy: 0.1088
1s/step - loss: 1.8353 - categorical_accuracy: 0.3905

# with (1)
func_model.save('/content/1/', 
                save_format='tf', save_traces=False)
INFO:tensorflow:Assets written to: /content/1/assets
INFO:tensorflow:Assets written to: /content/1/assets

# with (2)
tf.keras.models.save_model(func_model, '/content/2/', 
                           save_format='tf', save_traces=False)
INFO:tensorflow:Assets written to: /content/2/assets
INFO:tensorflow:Assets written to: /content/2/assets

Load and re-train them

from tensorflow.keras.models import  load_model

x = load_model('.//1/') 
y = load_model('.//2/')

x.fit(x_train, y_train, batch_size=128, epochs=1, verbose = 1)
1s/step - loss: 1.0963 - categorical_accuracy: 0.6780

y.fit(x_train, y_train, batch_size=128, epochs=1, verbose = 1)
1s/step - loss: 1.0963 - categorical_accuracy: 0.6780

Also note that there is a parameter called save_traces, and by default, it sets as True. I set it False as I don't want to. Newly added, according to doc:

save_traces: (only applies to SavedModel format) When enabled, the SavedModel will store the function traces for each layer. This can be disabled so that only the configs of each layer are stored. Defaults to True. Disabling this will decrease serialization time and reduce file size, but it requires that all custom layers/models implement a get_config() method.


Also FYI, as you save the entire model, I like to remind you to use the custom_object parameter while loading the model if you've custom layer-like stuff.

new_moedl = keras.models.load_model(
    "my_model", custom_objects={"CustomModel": CustomModel}
)

Would you mind checking this: enter link description here

Scroll down to the answer by @WGierke don't forget to upvote if it helpsed, you will get the code and load your Model normally. Code Sample:

    import tensorflow as tf
import tensorflow_hub as hub

m = tf.keras.Sequential([hub.KerasLayer("saved_model", trainable=True),
    tf.keras.layers.Dense(10, activation='softmax')
])
m.build([None, 224, 224, 3])
m.summary()

It works fine with me.

Related