【问题标题】:Loading the saved models from tf.keras in different versions从不同版本的 tf.keras 加载保存的模型
【发布时间】:2020-07-06 03:15:39
【问题描述】:

我在 google 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']))

【问题讨论】:

    标签: python tensorflow keras conv-neural-network version


    【解决方案1】:

    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')
    

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

    【讨论】:

    • 谢谢。我可以在谷歌 colab 上用 1.15 加载它,但我想在我的笔记本电脑离线时做同样的事情。我可以在带 CPU 的笔记本电脑上安装 TensorFlow 1.15 吗?我尝试使用 Anaconda,但无法安装该版本。是不是这个版本只支持GPU?
    猜你喜欢
    • 2021-12-26
    • 2011-07-08
    • 1970-01-01
    • 2018-08-05
    • 2021-01-29
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多