简体   繁体   中英

Cannot Reload saved Keras model using tensorflow

I am working in a single jupyter notebook. I create and train a very simple CNN with keras. It compiles, fits, and predicts fine. I save it with:

model.save("mymodel.hd5")

Model is a keras.models.Sequential.

I then read that back in with:

reload_keras_model = keras.models.load_model("mymodel.hd5")

That also works fine. However if I try to read the model in using tensorflow via:

from tensorflow.keras.models import load_model
reload_tf_mmodel = load_model("mymodel.hd5")

That fails with:

ValueError: Unknown layer:layers

Most of the threads I've read on github say "update your model" or comments about custom objects (I'm not using any). My target platform is the rpi zero and I've been able to install tf but unable to install keras, and that's why I want to load via tf. Why would keras and tf.keras handle this model differently and what do I need to update/change to read it in with tf.keras?

While keras (can) use TF as Backend, it does not guarantee that the saved model is readable in TF as well.

Note that you can use keras with both theano and tf, thus reload_keras_model = keras.models.load_model("mymodel.hd5") will work good with both backends, as the saving/loading is done in the "keras" part, and not using the backend.

You can use this tool: keras_to_tensorflow

Or something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM