在Tensorflow 2中导出冻结的图.pb文件

Bur*_*000 6 tensorflow tensorflow2.0

我一直在尝试Tensorflow 2 Alpha,并且一直在尝试冻结模型并将其导出到.pb graphdef文件。

在Tensorflow 1中,我可以做这样的事情:

# Freeze the graph.
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
    sess,
    sess.graph_def,
    output_node_names)

# Save the frozen graph to .pb file.
with open('model.pb', 'wb') as f:
    f.write(frozen_graph_def.SerializeToString())
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不再可行,因为convert_variables_to_constants被删除并且不鼓励使用会话。

我看了看,发现有一个冻结图 实用程序https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py与SavedModel导出一起使用。

是否仍可以在Python中执行某些操作,或者我现在打算切换并使用此工具?

小智 3

我在从tensorflow1.x迁移到tensoflow2.0 beta时也遇到了同样的问题。这个问题可以通过2种方法解决:

  1. 第一个是去tensflow2.0文档搜索您使用过的方法并更改每行的语法&
  2. 使用谷歌的 tf_ugrade_v2 脚本

tf_upgrade_v2 --infile your_tf1_script_file --outfile returned_tf2_file

您尝试上述命令将您的tensorflow1.x脚本更改为tensorflow2.0,它将解决您的所有问题。

另外,您可以重命名该方法(参考文档手动步骤)将'tf.graph_util.convert_variables_to_constants' 重命名为 'tf.compat.v1.graph_util.convert_variables_to_constants'

测量问题是在tensorflow2.0中,许多语法和功能已经改变,尝试参考tensoflow2.0文档或使用google的tf_upgrade_v2脚本