darrahts
darrahts

Reputation: 393

keras model not loading

I've read through similar questions on this and am still stumped. On the same computer, I am training a model and saving with

model.save(model_location)
type(model)   
keras.engine.functional.Functional

Then, I am loading the model with

model = keras.models.load_model(model_location)
 # model = tf.saved_model.load(model_location) results in the same
type(model)

<class 'tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject'>

and getting the following error when calling model.predict(X)

AttributeError: '_UserObject' object has no attribute 'predict'

tf.saved_model.contains_saved_model(model_location)

true

tensorflow-gpu == 2.7.0 keras == 2.8.0


This is on Windows, and the same environment on my Linux machine does not produce this error.

Thanks in advance!

Upvotes: 2

Views: 1696

Answers (1)

user11530462
user11530462

Reputation:

A workaround to solve this is to get the Keras API, by wrapping it inside a KerasLayer in a Sequential model as follows:

import tensorflow as tf
import tensorflow_hub as hub

model = tf.keras.Sequential([
    hub.KerasLayer("saved/model/path")
])
model.build(<input_shape>)

Now the model supports all Keras API like predict, summary, etc, and this now should work:

model.predict(X)

Let us know if the issue still persists. Thnaks!

Upvotes: 1

Related Questions