gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:41:39 24 4
gpt4 key购买 nike

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

我已按照此处的一些说明进行操作 convert tensorflow model to pb tensorflow以及其他一些建议,这些建议似乎可以追溯到tensorflow 1.0是最新版本时,但它们给出了类似的问题。

下面的代码中出现的错误是当我尝试将变量转换为常量时:它提示我的变量不在 session 定义的图中。 (我是 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 will be
pruned so subgraphs that are not necessary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
from tensorflow.compat.v1.graph_util import convert_variables_to_constants
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.compat.v1.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.compat.v1.global_variables()]
# Graph -> GraphDef ProtoBuf
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph


frozen_graph = freeze_session(tf.compat.v1.keras.backend.get_session(), output_names=[out.op.name for out in pre_model.outputs])

输出+错误:

[<tf.Tensor 'conv2d_1_input:0' shape=(None, 28, 28, 1) dtype=float32>]
[<tf.Tensor 'dense_2/Identity:0' shape=(None, 10) dtype=float32>]
File "saveGraph.py", line 40, in freeze_session
output_names, freeze_var_names)
File "C:\Users\jgoer\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\Users\jgoer\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\graph_util_impl.py", line 277, in convert_variables_to_constants
inference_graph = extract_sub_graph(input_graph_def, output_node_names)
File "C:\Users\jgoer\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\Users\jgoer\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\graph_util_impl.py", line 197, in extract_sub_graph
_assert_nodes_are_present(name_to_node, dest_nodes)
File "C:\Users\jgoer\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\graph_util_impl.py", line 152, in _assert_nodes_are_present
assert d in name_to_node, "%s is not in graph" % d
AssertionError: dense_2/Identity is not in graph

最佳答案

看看 TensorFlow 的 tutorial on saving and loading models 。您可以使用 model.save("path"),如果不包含扩展名,模型将以 SavedModel 格式保存。

import tensorflow as tf

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

关于python - Tensorflow 2.0 将keras模型转换为.pb文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60005661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com