Alexey Abramov
Alexey Abramov

Reputation: 493

error while freezing the model (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.

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:

File "mnist.py", line 180, in main output_graph = "frozen_graph.pb", clear_devices = True, initializer_nodes = "")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 184, in freeze_graph variable_names_blacklist)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 87, in freeze_graph_with_def_protos _ = importer.import_graph_def(input_graph_def, name="")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2633, in create_op self._add_op(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2312, in _add_op "is already used" % op.name) ValueError: cannot add op with name conv1/Variable/Adam as that name is already used

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...

Thanks in advance for any advice!

Best, Alexey

Upvotes: 0

Views: 1325

Answers (1)

chandrakant_k
chandrakant_k

Reputation: 101

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.

Command used to freeze the graph:

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

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

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.

Upvotes: 2

Related Questions