小编gli*_*ipR的帖子

Tensorflow 2.0 将 keras 模型转换为 .pb 文件

我正在生成 keras 模型并将其保存到 .h5 文件,然后尝试将其转换为 .pb 文件以供稍后统一使用。

我已经按照这里的一些说明将 tensorflow 模型转换为 pb tensorflow以及一些其他建议,这些建议似乎可以追溯到 tensorflow 1.0 是最新版本时,但它们给出了类似的问题。

我在下面的代码中遇到的错误是当我尝试将变量转换为常量时:它抱怨我的变量不在会话定义的图中。(我是 tensorflow 的菜鸟,所以我不完全知道这意味着什么,但我认为这与我的模型无关。)

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from keras import backend as K

tf.keras.backend.set_learning_phase(0)

pre_model = tf.keras.models.load_model("final_model.h5")

print(pre_model.inputs)
print(pre_model.outputs)

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph …
Run Code Online (Sandbox Code Playgroud)

python python-3.x keras tensorflow

2
推荐指数
1
解决办法
6130
查看次数

标签 统计

keras ×1

python ×1

python-3.x ×1

tensorflow ×1