Get input and output node name from .ckpt and .met

2019-08-01 17:09发布

问题:

I have .meta and .ckpt files of the tensorflow model. I wanted to know exact input and output node name but I am getting a list of node names by following this.

When I have a frozen protobuf model, I get the input node name and output node name as the starting and end of the list using this code:

import tensorflow as tf
from tensorflow.python.platform import gfile
GRAPH_PB_PATH = 'frozen_model.pb'
with tf.Session() as sess:
   print("load graph")
   with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f:
       graph_def = tf.GraphDef()
   graph_def.ParseFromString(f.read())
   sess.graph.as_default()
   tf.import_graph_def(graph_def, name='')
   graph_nodes=[n for n in graph_def.node]
   names = []
   for t in graph_nodes:
      names.append(t.name)
   print(names)

Can I do something similar for .ckpt or .meta file ?

回答1:

The .meta file contains information about the different node in the tensorflow graph. This has been better explained here.

The values of the different variables in the graph at that moment are stored separately in the checkpoint folder in checkpoint.data-xxxx-of-xxxx file.

There is no concept of an input or output node in the normal checkpoint process, as opposed to the case of a frozen model. Freezing a model outputs a subset of the whole tensorflow graph. This subset of the main graph has only those nodes present on which the output node is dependent on. Because freezing a model is done for serving purposes, it converts the tensorflow variables to constants, eliminating the need for storing additional information like gradients of the different variables at each step.

If you still want to identify the nodes you would be interested in, you can restore your graph from the .meta file and visualize it in tensorboard.

import tensorflow as tf
from tensorflow.summary import FileWriter

sess = tf.Session()
tf.train.import_meta_graph("your-meta-graph-file.meta")
FileWriter("__tb", sess.graph)

This will create a __tb folder in your current directory and you can then view the graph by issuing the following command.

tensorboard --logdir __tb

Here is a link to the screenshot of some model with a node selected. You can get the name of the node from the top right corner.