如何在 TensorFlow v1 中使用 freeze_graph.py 工具 [英] How to use freeze_graph.py tool in TensorFlow v1

查看:37
本文介绍了如何在 TensorFlow v1 中使用 freeze_graph.py 工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 freeze_graph.py 工具,在 TensorFlow v1 中通过 saver.save 保存模型?如果是这样,如何?

Is it possible to use the freeze_graph.py tool with models saved via saver.save in TensorFlow v1? If so, how?

我的代码大致如下:

supervisor = tf.train.Supervisor(logdir=output_directory_path)

with supervisor.managed_session() as session:
    # train the model here
    supervisor.saver.save(session, output_directory_path)

这会生成一个包含以下内容的目录:

This produces a directory containing:

checkpoint
output
output-16640.data-00000-of-00001
output-16640.index
output-16640.meta

其中 output 是包含中间训练步骤文件的目录.其余的是文件.

Where output is a directory containing the files for intermediate training steps. The rest are files.

我的理解是,这是 saver v2 格式的元图(.meta 文件)及其变量(.data* 文件).这些文件包含 freeze_graph.py 工具所需的数据,但不清楚如何告诉 freeze_graph.py 工具从这些文件加载​​数据.

My understanding is that this is a meta graph (the .meta file) and its variables (the .data* file) in saver v2 format. These files contain the data needed for the freeze_graph.py tool but it is unclear how to tell the freeze_graph.py tool to load the data from these files.

所有这些尝试都会产生错误消息 Input checkpoint '...' does not exist!

All of these attempts produce the error message Input checkpoint '...' doesn't exist!

python freeze_graph.py --input_checkpoint checkpoint --output_graph /tmp/out
python freeze_graph.py --input_checkpoint . --output_graph /tmp/out
python freeze_graph.py --input_checkpoint output-16640 --output_graph /tmp/out

freeze_graph.py 代码包含注释 'input_checkpoint' 可能是前缀,如果我们使用 Saver V2 格式 旁边的 --input_checkpoint 参数被使用,所以我认为上述第三次尝试会奏效,但是,唉,不行.

The freeze_graph.py code includes the comment 'input_checkpoint' may be a prefix if we're using Saver V2 format next to where the --input_checkpoint argument is used so I had thought the third of the above attempts would work but, alas, no.

推荐答案

正如@mrry 在评论中指出的,这个特定问题的答案是在输出前缀前加上 ./.完成后,我发现还需要为 --input_graph--output_name_names 参数提供值.

As @mrry pointed out in a comment, the answer to this particular question is to prefix the output prefix with ./. When this was done I discovered it is also necessary to provide values for the --input_graph and --output_name_names arguments.

命令现在看起来像

python freeze_graph.py \
    --input_graph output/graph.pbtxt \
    --input_checkpoint ./output-16640 \
    --output_graph /tmp/out \
    --output_node_names <name>

不幸的是,我的图表包含预加载数据的变量,这会导致 freeze_graph.py 失败并显示类似 Attempting to use uninitialized value ... 的消息;解决这个后续问题超出了本问题的范围.

Unfortunately my graph contains variables for pre-loaded data which causes freeze_graph.py to fail with a message like Attempting to use uninitialized value ...; solving this subsequent problem is beyond the scope of this question.

这篇关于如何在 TensorFlow v1 中使用 freeze_graph.py 工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆