7

我一直在尝试 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())

然而,这似乎不再可能,因为 convert_variables_to_constants 被删除并且不鼓励使用会话。

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

有没有办法在 Python 中做到这一点,或者我现在打算切换并使用这个工具?

4

2 回答 2

3

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

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

tf_upgrade_v2 --infile your_tf1_script_file --outfile convert_tf2_file

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

此外,您可以重命名方法(通过参考文档手动步骤)将“tf.graph_util.convert_variables_to_constants”重命名为“tf.compat.v1.graph_util.convert_variables_to_constants”

测量问题是,在 tensorflow2.0 中,许多语法和功能发生了变化,尝试参考 tensorflow2.0 文档或使用谷歌的 tf_upgrade_v2 脚本

于 2019-06-14T11:16:45.517 回答
1

不确定您是否看到过这个 Tensorflow 2.0 问题,但这个响应似乎是一种解决方法:

https://github.com/tensorflow/tensorflow/issues/29253#issuecomment-530782763

注意:这对我的 nlp 模型不起作用,但也许对你有用。建议的解决方法是model.save_weights('weights.h5')在 TF 2.0 环境中使用。然后使用 TF 1.14 创建新环境并在 TF 1.14 env 中执行以下所有步骤。构建您的模型model = create_model()并用于model.load_weights('weights.h5')将权重加载回您的模型中。然后用 保存整个模型model.save('final_model.h5')。如果您成功完成上述步骤,请按照链接中的其余步骤使用 freeze_graph。

于 2019-09-21T13:11:40.030 回答