简体   繁体   English

从不同版本的 tf.keras 加载保存的模型(从 tf 2.3.0 到 tf 1.12)

[英]Loading the saved models from tf.keras in different versions (From tf 2.3.0 to tf 1.12)

Question: I have created and trained a keras model in tf 2.3.0 and I need to load this model in tf 1.12.0 in order to be used with a library that requires an older version of tf.问题:我已经在 tf 2.3.0 中创建并训练了一个 keras 模型,我需要在 tf 1.12.0 中加载这个模型,以便与需要旧版本 tf. Is there any way to either convert the models from the format of the new version of tf to an older version so I can load the model with tf 1.12.0?有什么方法可以将模型从新版本的 tf 格式转换为旧版本,以便我可以使用 tf 1.12.0 加载模型?

What I have tried so far: A similar discussion showed how to convert models from tf 1.15 - 2.1 to tf.10, but when I tried this solution I got an error "Unknown layer: functional".到目前为止我尝试过的:一个类似的讨论展示了如何将模型从 tf 1.15 - 2.1 转换为 tf.10,但是当我尝试这个解决方案时,我得到了一个错误“未知层:功能”。 Link: Loading the saved models from tf.keras in different versions链接: 从不同版本的 tf.keras 加载保存的模型

I tried to fix this by using the following line suggested by another question:我尝试使用另一个问题建议的以下行来解决此问题:

new_model = tf.keras.models.model_from_json(json_config, custom_objects {'Functional':tf.keras.models.Model})

Link: ValueError: Unknown layer: Functional链接: ValueError:未知层:功能

However, if I use this the I get an error: ('Unrecognized keyword arguments:', dict_keys(['ragged'])) , which is the same error discussed in the first discussion I linked above.但是,如果我使用它,我会收到一个错误: ('Unrecognized keyword arguments:', dict_keys(['ragged'])) ,这与我在上面链接的第一个讨论中讨论的错误相同。

Another method I tried was using the Onnx library to convert the keras model to an Onnx model and then back to a keras model of a different version.我尝试的另一种方法是使用 Onnx 库将 keras 模型转换为 Onnx 模型,然后再转换回不同版本的 keras 模型。 However, I soon realized that the keras2onnx library required tf 2.x.但是,我很快意识到 keras2onnx 库需要 tf 2.x。

Links: https://github.com/onnx/tensorflow-onnx and https://github.com/gmalivenko/onnx2keras链接: https : //github.com/onnx/tensorflow-onnxhttps://github.com/gmalivenko/onnx2keras

Any suggestions about how to get around this without having to retrain my models in a older version of tensorflow would be greatly appreciated!关于如何解决这个问题而不必在旧版本的 tensorflow 中重新训练我的模型的任何建议将不胜感激! Thanks谢谢

Here is the simple code that I tried to implement to load my model:这是我尝试实现以加载模型的简单代码:

Save in tf 2.3.0保存在 tf 2.3.0

import tensorflow as tf

CNN_model=tf.keras.models.load_model('Real_Image_XAI_Models/Test_10_DC_R_Image.h5')

CNN_model.save_weights("Real_Image_XAI_Models/weights_only.h5")

json_config = CNN_model.to_json()

with open('Real_Image_XAI_Models/model_config.json', 'w') as json_file:
    json_file.write(json_config)

Load in tf 1.12.0在 tf 1.12.0 中加载

with open('Real_Image_XAI_Models/model_config.json') as json_file:
    json_config = json_file.read()

new_model = tf.keras.models.model_from_json(json_config)

#or implement the line to acount for the functional class

#new_model = tf.keras.models.model_from_json(json_config, custom_objects={'Functional':tf.keras.models.Model})

new_model.load_weights('Real_Image_XAI_Models/weights_only.h5')

There are mainly two breaking changes in the model config from tf-1.12.0 to tf-2.3.0:从 tf-1.12.0 到 tf-2.3.0,模型配置主要有两个重大变化:

  1. The root class Model is now Functional根类Model现在是Functional
  2. The support for Ragged tensors was added in tf-1.15在 tf-1.15 中添加了对不规则张量的支持

You can try to edit the model config json file once saved from tf-2.3.0 to reverse the effects of these changes as follows:您可以尝试编辑从 tf-2.3.0 保存的模型配置 json 文件,以反转这些更改的效果,如下所示:

  1. Replace the root class definition "class_name": "Functional" by "class_name": "Model" .将根类定义"class_name": "Functional"替换为"class_name": "Model" This will reverse the effect of change #1 above.这将逆转上述更改#1 的效果。
  2. Delete all occurrences of "ragged": false, (and of "ragged": true, if present).删除所有出现的"ragged": false, (以及"ragged": true,如果存在的话)。 This will reverse the effect of change #2 above.这将逆转上述更改#2 的效果。

You may try to find a way to make these changes programmatically in the json dictionary or at the model load time, but I find it easier to make these one-time changes to the json file itself.您可能会尝试找到一种在 json 字典中或在模型加载时以编程方式进行这些更改的方法,但我发现对 json 文件本身进行这些一次性更改更容易。

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

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