tensorflowgraphmodelcheckpoint

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


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.


Solution

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