Cant properly load saved model and call predict method

hi,

I am having trouble loading large model after saving.

have tried all below saveing methods:


tf.saved_model.save(model, model_save_path)
model.save(model_save_path+"_new_save")
tf.keras.models.save_model(model, model_save_path+"_v3")

error when loading :

method 1

m2=tf.keras.models.load_model(model_save_path+"_v3")
error:
 __init__() got an unexpected keyword argument 'reduction'

method 2

m3=tf.keras.models.load_model(model_save_path

error:
ARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
ValueError: Unable to create a Keras model from SavedModel at xxxx . This SavedModel was exported with `tf.saved_model.save`, and lacks the Keras metadata file. Please save your Keras model by calling `model.save`or `tf.keras.models.save_model`. Note that you can still load this SavedModel with `tf.saved_model.load`.

method 3

m4=tf.saved_model.load(model_save_path)

this works but m4 object has no predict method 
and not able to use 

model.signatures["serving_default"](**input_data)

or

model.__call__(input_data,training=False)

to predict on data

any help would be appreciated

I remember there was a discussion on this some month ago in this same forum but I couldn’t find it myself

Maybe @markdaoust can remember

just so you don’t stay empty handed from my almost empty message, I’ll add these links:

See me comment here:

# This gives you a vanilla SavedModel
tf.saved_model.save(model, model_save_path)

# This gives you a keras-SavedModel
model.save(model_save_path)

You can’t load a vanilla savedmodel as a keras-model.

For the other errors, It would help if you were a little more clear.

error:
 __init__() got an unexpected keyword argument 'reduction'

This could happen if you’re using custom layers and your “get_config” and “from_config” don’t work well together.

tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.

What versions of tensorflow are you using to save/load the model?

but m4 object has no predict method 
and not able to use 

model.signatures["serving_default"](**input_data)

or

model.__call__(input_data,training=False)

to predict on data

Can you be more specific? what was the error?

1 Like

Someone else asked me to clarify what I meant about get_config and from_config:

Okay, so .get_config is supposed to return a json-serializable object. That should only save the layer’s configuration, not its variable values.

So layers.Dense(units=5).get_config() returns {'units': 5}. Keras, basically, saves the class name and the config so that it can later rebuild the layer, and then restore a checkpoint into it to restore the weights.

from_config is a classmethod that recieves the json dict that was saved earlier, and needs to create the layer. The default implementation is:

@classmethod
def from_config(cls, config):
  return cls(**config)

That is: Create an instance of the class, passing the dict’s key-value pairs as key-word arguments to the class’s regular constructor: __init__.

What I think is happening here is that get_config is returning a dict that contains a key that from_config or __init__ don’t recognize.

But I could be wrong about that. It’s hard to know without the full traceback.

1 Like

I debugged this same error with someone else.

It turns out you can get this error if you pass a keras.losses instance in the list of metrics in Model.compile.