简体   繁体   English

无法使用张量流重新加载保存的Keras模型

[英]Cannot Reload saved Keras model using tensorflow

I am working in a single jupyter notebook. 我正在使用单个Jupyter笔记本工作。 I create and train a very simple CNN with keras. 我使用keras创建并训练了一个非常简单的CNN。 It compiles, fits, and predicts fine. 它可以编译,拟合并预测良好。 I save it with: 我将其保存为:

model.save("mymodel.hd5")

Model is a keras.models.Sequential. 模型是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: 但是,如果我尝试通过以下方式使用tensorflow来读取模型:

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). 我在github上阅读的大多数线程都说“更新您的模型”或有关自定义对象的注释(我不使用任何对象)。 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. 我的目标平台是rpi zero,我已经能够安装tf但无法安装keras,这就是为什么我要通过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? 为什么keras和tf.keras会以不同的方式处理此模型,我需要更新/更改以使用tf.keras读取它吗?

While keras (can) use TF as Backend, it does not guarantee that the saved model is readable in TF as well. 尽管keras(可以)将TF用作后端,但它不能保证所保存的模型也可以在TF中读取。

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. 请注意,您可以将keras与theano和tf一起使用,因此reload_keras_model = keras.models.load_model("mymodel.hd5")在两个后端都可以正常工作,因为保存/加载是在“ keras”部分完成的,而不是使用后端。

You can use this tool: keras_to_tensorflow 您可以使用此工具: keras_to_tensorflow

Or something similar. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练后的 model 无法保存 tensorflow keras - Trained model cannot be saved tensorflow keras 如何将保存的 model 转换或加载到 TensorFlow 或 Keras? - How to convert or load saved model into TensorFlow or Keras? 无法加载保存在 keras / tensorflow 中的 model? - Can not load saved model in keras / tensorflow? 无法加载 Tensorflow (Keras) 已保存 Model,因为文件不存在(而它确实存在) - Cannot load Tensorflow (Keras) Saved Model because file does not exist (whereas it does exist) TensorFlow中的Keras无法使用config(KeyError:'name')重新初始化顺序模型 - Keras in TensorFlow cannot reinitialize a sequential model using config (KeyError: 'name') TensorFlow 使用已保存的 model 进行推理 - TensorFlow inference using saved model 使用Keras和张量流的预测模型 - Predictive model using Keras and tensorflow 无法加载 tensorflow 已保存 model 返回 - Cannot load tensorflow saved model back 在 python 中保存 Keras/Sklearn 并在 tensorflow.js 中加载保存的模型 - Saving a Keras/Sklearn in python and loading the saved model in tensorflow.js 如何从 Colaboratory 中保存的检查点加载 TensorFlow Keras model? - How to load TensorFlow Keras model from a saved checkpoint in Colaboratory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM