本文介绍了冻结模型时出错 (freeze_graph)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 tensorflow 非常陌生,想在 C++ 环境中使用预训练模型 (Python) 进行推理.据我了解,为此我需要使用freeze_graph"工具冻结训练好的模型.

I'm very new in tensorflow and would like to use pre-trained models (Python) in C++ environment for inference. As I understood, for this I need to freeze the trained model using "freeze_graph" tool.

这是一个代码片段,它是如何查找非常简单的 MNIST 模型的:

Here is a code snippet how it looks for the very simple MNIST model:

with tf.Session(config=config) as s:
    s.run(tf.global_variables_initializer())

    for i in range(n):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
    saver.save(s, 'models/saved_checkpoint')

with tf.Session(config=config) as s:
    # save the graph definition
    tf.train.write_graph(s.graph_def, 'models', "graph_def.pbtxt")

freeze_graph.freeze_graph(input_graph = "models/graph_def.pbtxt", input_saver = "", input_binary = False, input_checkpoint = "models/saved_checkpoint", output_node_names = "output_node", restore_op_name = "save/restore_all", filename_tensor_name = "save/Const:0", output_graph = "frozen_graph.pb", clear_devices = True, initializer_nodes = "")

这样做会出现以下错误:

Doing it this way I'm getting the following error:

文件mnist.py",第 180 行,在主目录中output_graph = "frozen_graph.pb", clear_devices = True, initializer_nodes = "")
文件"/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py",第 184 行,在 freeze_graphvariable_names_blacklist)
文件/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py",第 87 行,在 freeze_graph_with_def_protos 中_ = importer.import_graph_def(input_graph_def, name="")
文件/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py",第 313 行,在 import_graph_def 中op_def=op_def) 文件 "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py",第 2633 行,在 create_op 中self._add_op(ret)
文件/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py",第 2312 行,在 _add_op 中已使用"% op.name) ValueError: 无法添加名称为 conv1/Variable/Adam 的操作,因为该名称已被使用

有人知道这里可能有什么问题吗?我正在使用 tensorflow 1.3 和 python 2.7.不幸的是,我找不到有关图形冻结的太多信息,并且可用示例对我不起作用...

Does anybody have any idea what might be wrong here? I'm using tensorflow 1.3 and python 2.7. Unfortunately, I cannot find much information about the graph freeze and available examples don't work for me...

提前感谢您的建议!

最好的,阿列克谢

推荐答案

我能够使用 Tensorflow-GPU 1.3 冻结图形.我在虚拟环境中安装了 tensorflow,所以freeze_graph.py"在虚拟环境路径中.

I was able to freeze the graph using Tensorflow-GPU 1.3. I installed tensorflow in a virtual environment, so the 'freeze_graph.py' was in virtual env path.

用于冻结图形的命令:

python/home/ck/venvs/enet/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py --input_graph ./log/graph.pbtxt --input_checkpoint ./log/model.ckpt-0 --output_graph ./log/frozen_model.pb --output_node_names=ENet/logits_to_softmax

这里的log"是保存检查点和 graph.pbtxt 的文件夹.

here 'log' is the folder in which checkpoint as well graph.pbtxt was saved.

注意:一旦检查点和 pbtxt 文件被保存,我是从命令行完成的.我还没有尝试过您描述的方法,但是如果您的目的只是冻结图形,那么我想这应该可行.

Note: I did it from the command line, once the checkpoint and pbtxt file got saved. I haven't tried the method you described, however if your aim is just to freeze the graph then I guess this should work.

这篇关于冻结模型时出错 (freeze_graph)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:04