Tensorflow: How to convert .meta, .data and .index model files into one graph.pb file
Asked Answered
D

4

38

In tensorflow the training from the scratch produced following 6 files:

  1. events.out.tfevents.1503494436.06L7-BRM738
  2. model.ckpt-22480.meta
  3. checkpoint
  4. model.ckpt-22480.data-00000-of-00001
  5. model.ckpt-22480.index
  6. graph.pbtxt

I would like to convert them (or only the needed ones) into one file graph.pb to be able to transfer it to my Android application.

I tried the script freeze_graph.py but it requires as an input already the input.pb file which I do not have. (I have only these 6 files mentioned before). How to proceed to get this one freezed_graph.pb file? I saw several threads but none was working for me.

Driblet answered 24/8, 2017 at 14:32 Comment(3)
See here: #45433731Featherveined
How did you get graph.pbtxt? If it is the graph of your model you can freeze it with freeze.py. .pbtxt.Thunderclap
The graph.pbtxt I found in the training logs after finishing the training. It was however saved before the training was finished. Check for it in the previously saved status of the graph. For the training from scratch I used the script: train_image_classifier.py. For training I I used my own pictures (.jpg) which I had to convert to .tfrecord files before using the script build_image_data.pyDriblet
T
46

You can use this simple script to do that. But you must specify the names of the output nodes.

import tensorflow as tf

meta_path = 'model.ckpt-22480.meta' # Your .meta file
output_node_names = ['output:0']    # Output nodes

with tf.Session() as sess:
    # Restore the graph
    saver = tf.train.import_meta_graph(meta_path)

    # Load weights
    saver.restore(sess,tf.train.latest_checkpoint('path/of/your/.meta/file'))

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

    # Save the frozen graph
    with open('output_graph.pb', 'wb') as f:
      f.write(frozen_graph_def.SerializeToString())

If you don't know the name of the output node or nodes, there are two ways

  1. You can explore the graph and find the name with Netron or with console summarize_graph utility.

  2. You can use all the nodes as output ones as shown below.

output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]

(Note that you have to put this line just before convert_variables_to_constants call.)

But I think it's unusual situation, because if you don't know the output node, you cannot use the graph actually.

Thunderclap answered 24/8, 2017 at 17:55 Comment(15)
Is there a simple way to get the output node names?Factorage
I am trying to do the same. Is there a way to find the output node names?Waterworks
You can use summarize_graph utility.Thunderclap
I get this error, it may be because I'm not sure my output_node_names are correct. File "/path/to/saver.py", line 1796, in restore raise ValueError("Can't load save_path when it is None.")Sordino
sure. It's a bit long for a comment, so I put it here: pastebin.com/2YLjtMvQSordino
@Sordino Looks like it cannot find any checkpoint in the current directory (with path name .) Try to set the path to your checkpoint explicitely: saver.restore(sess, 'path/to/model.ckpt').Thunderclap
Correcting the folder definitely helped, and I think it might have been a badly formatted ckpt file as well. It turns out I had some other problems with my training. Once I fixed those, this problem goes away. (I'm left with the problem that what I thought were my output_node_names are actually not in graph. But I'm sure I'll figure that out soon)Sordino
Try: [n.name for n in tf.get_default_graph().as_graph_def().node]Acosta
If anyone comes here and has the same problem I did, aka, when trying to freeze the graph, it will fail with "Attempting to use uninitialized value", just add init=tf.global_variables_initializer() sess.run(init) after you Load the Weights.Fcc
@niza-siwale Thank you for your editing, but your code puts all the nodes into output_node_names instead only the output node as suggested by the documentation.Thunderclap
@Thunderclap sorry for that, I was going to rectify my mistake but I forgot. Thanks for helpTriptolemus
What is the difference compared to freeze_graph.py ?Creese
What is tf.train.latest_checkpoint('.'), it should be replaced with checkpoint dir?Creese
This is a better way to get the 'trainable' variables and could automate saving the model vars_train = tf.trainable_variables() output_node_names = [var.name.split(":")[0] for var in vars_train]Shue
@Shue Usually not all the trainable variables are the output nodes you need. Furthermore, the output node may not be a variable at all. For example in the graph for the expression a * x + 1 the output node is add.Thunderclap
P
7

As it may be helpful for others, I also answer here after the answer on github ;-). I think you can try something like this (with the freeze_graph script in tensorflow/python/tools) :

python freeze_graph.py --input_graph=/path/to/graph.pbtxt --input_checkpoint=/path/to/model.ckpt-22480 --input_binary=false --output_graph=/path/to/frozen_graph.pb --output_node_names="the nodes that you want to output e.g. InceptionV3/Predictions/Reshape_1 for Inception V3 "

The important flag here is --input_binary=false as the file graph.pbtxt is in text format. I think it corresponds to the required graph.pb which is the equivalent in binary format.

Concerning the output_node_names, that's really confusing for me as I still have some problems on this part but you can use the summarize_graph script in tensorflow which can take the pb or the pbtxt as an input.

Regards,

Steph

Perkoff answered 25/8, 2017 at 9:7 Comment(3)
what should I use for ssd_mobilnet_v1_coco inplace of -out_node_nameMewl
@PratikKhadloya can you please answer my above comment?Mewl
Example usage: python freeze_graph.py --input_graph=some_graph_def.pb --input_checkpoint=model.ckpt-8361242 --output_graph=/tmp/frozen_graph.pb --output_node_names=softmaxScratchboard
T
3

I tried the freezed_graph.py script, but the output_node_name parameter is totally confusing. Job failed.

So I tried the other one: export_inference_graph.py. And it worked as expected!

python -u /tfPath/models/object_detection/export_inference_graph.py \
  --input_type=image_tensor \
  --pipeline_config_path=/your/config/path/ssd_mobilenet_v1_pets.config \
  --trained_checkpoint_prefix=/your/checkpoint/path/model.ckpt-50000 \
  --output_directory=/output/path

The tensorflow installation package I used is from here: https://github.com/tensorflow/models

Totemism answered 13/6, 2018 at 1:33 Comment(2)
hi @kennynut what is this --pipeline_config_path? what is written in this kind of file, could you give me an example? I've been using tensorflow for a while but have never need to use such a configuration file.Melodramatize
The pipeline_config_path provides the basic configuration for the frozen graph to function properly. Say, it normally comes with the default name-pipeline.config--under the default root path of compressed packages among model zoos from google git hub repositories.Exit
W
1

First, use the following code to generate the graph.pb file. with tf.Session() as sess:

    # Restore the graph
    _ = tf.train.import_meta_graph(args.input)

    # save graph file
    g = sess.graph
    gdef = g.as_graph_def()
    tf.train.write_graph(gdef, ".", args.output, True)

then, use summarize graph get the output node name. Finally, use

python freeze_graph.py --input_graph=/path/to/graph.pbtxt --input_checkpoint=/path/to/model.ckpt-22480 --input_binary=false --output_graph=/path/to/frozen_graph.pb --output_node_names="the nodes that you want to output e.g. InceptionV3/Predictions/Reshape_1 for Inception V3 "

to generate the freeze graph.

Waltz answered 19/12, 2018 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.