26

I'm trying to create a pb file from my Keras (tensorflow backend) model so I can build it on iOS. I'm using freeze.py and I need to pass the output nodes. How do i get the names of the output nodes of my Keras model?

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

18

You can use Keras model.summary() to get the name of the last layer.

If model.outputs is not empty you can get the node names via:

[node.op.name for node in model.outputs]

you get the session via

session = keras.backend.get_session()

and you convert all training variables to consts via

min_graph = convert_variables_to_constants(session, session.graph_def, [node.op.name for node in model.outputs])

after that you can write a protobuf-file via

tensorflow.train.write_graph(min_graph, "/logdir/", "file.pb", as_text=True)
12

If output nodes are not explicitly specified when constructing a model in Keras, you can print them out like this:

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

Then all you need to do is find the right one, which often is similar to the name of activation function. You can just use this string name you've found as a value for output_node_names in freeze_graphfunction.

4

You can also use the tensorflow utility: summarize_graph to find possible output_nodes. From the official documentation:

Many of the transforms that the tool supports need to know what the input and output layers of the model are. The best source for these is the model training process, where for a classifier the inputs will be the nodes that receive the data from the training set, and the output will be the predictions. If you're unsure, the summarize_graph tool can inspect the model and provide guesses about likely input and output nodes, as well as other information that's useful for debugging.

It just needs the saved graph pb file as the input. Check the documentation for an example.

  • 7
    Im in a circular dependency, I want to use this tool but I dont have a *.pd file. In order to get the *.pd file from a checkpoint you need the output names. – wi1 Apr 19 '18 at 20:33
  • @wi1 you can also generate a graph_def .pd file programmatically via the Python API like this: tf.train.write_graph(keras.backend.get_session().graph, <path to where you want to gen .pb>, "model_serial.pb", as_text=False). – ThePartyTurtle Aug 24 '18 at 19:18
  • Thanks for the suggestion, I'll give that a try. Its been awhile but I believe at the time I modified code to be able to inspect the graph with tensorboard and then stepped through the graph with tfdbg. tfdbg is awesome by the way. – wi1 Aug 27 '18 at 16:50
2

The output_node_names should contain the names of the graph nodes you intend to use for inference(e.g. softmax). It is used to extract the sub-graph that will be needed for inference. It may be useful to look at freeze_graph_test.