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



我在谷歌colab中使用TensorFlow和Keras创建了一个图像分类模型。它保存在那里,GPU版本分别为1.15和2.2.4。现在我想把它们加载到我的CPU和1.10和2.2.2版本的远程机器中,我无法做到这一点,并且出现了错误。这是我第一次接触CNN以及tf和keras,所以我不知道确切的原因是什么以及如何解决这个问题。我在下面提到了代码和错误:

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)

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

Tensorflow 1.15包含一些突破性的更改,如粗糙张量支持,因此它不支持向后兼容性(Tf 1.10(。这就是问题所在。请尝试使用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')

您可以参考链接以更好地了解此链接

最新更新