简体   繁体   English

从不同版本的 tf.keras 加载保存的模型

[英]Loading the saved models from tf.keras in different versions

I have created an image classification model using TensorFlow and Keras in google colab.我在 google colab 中使用 TensorFlow 和 Keras 创建了一个图像分类模型。 It is saved there with GPU versions 1.15 and 2.2.4 for both respectively.它分别保存在 GPU 版本 1.15 和 2.2.4 中。 Now I want to load them in my remote machine with CPU and versions 1.10 and 2.2.2 I am unable to do that and getting error.This is my first experience with CNN as well as tf and keras so I am not able to figure out what is the exact reason and how to solve this.现在我想用 CPU 和版本 1.10 和 2.2.2 将它们加载到我的远程机器中我无法做到这一点并出现错误。这是我第一次使用 CNN 以及 tf 和 keras,所以我无法弄清楚确切原因是什么以及如何解决这个问题。 I have mentioned the code and error below:我在下面提到了代码和错误:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.models import model_from_json

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

Error : ValueError: ('Unrecognized keyword arguments:', dict_keys(['ragged']))错误:ValueError:('无法识别的关键字参数:',dict_keys(['ragged']))

Tensorflow 1.15 contains breaking changes like ragged tensor support, so it does not support backwards compatibility(Tf 1.10). Tensorflow 1.15 包含破坏性更改,例如不规则的张量支持,因此它不支持向后兼容性(Tf 1.10)。 This is the issue.这是问题。 Please try to load it using Tensorflow 1.15 and it should work.请尝试使用 Tensorflow 1.15 加载它,它应该可以工作。

You can load tf1.15+ model using tf1.15-2.1. Then save only weights to open in tf1.10
___________________________________________________________________
# In tensorflow 1.15-2.1
# Load model
model = load_model("my_model.h5")

# Save weights and architecture
model.save_weights("weights_only.h5")

# Save model config
json_config = model.to_json()
with open('model_config.json', 'w') as json_file:
json_file.write(json_config)
___________________________________________________________________
# In tensorflow 1.10
# Reload the model from the 2 files we saved
with open('model_config.json') as json_file:
json_config = json_file.read()
new_model = tf.keras.models.model_from_json(json_config)

# Load weights
new_model.load_weights('weights_only.h5')

You can refer the link for better understanding on this LINK您可以参考链接以更好地了解此链接

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

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