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
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)
- as_text = False worked for me. – WaterRocket8236 Mar 13 '18 at 8:29
- 2[node.op.name for node in model.outputs] was the key for me. I found NO official documentation pointing me to how to retrieve the output nodes for a model compiled and fit via Keras API with Tensorflow. – ThePartyTurtleAug 24 '18 at 20:07
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_graph
function.
- 14This method will return more than 10000 nodes name, how to figure out which one should be chosen? Thanks!– Tony Wang Feb 25 '18 at 4:49
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.
- 7Im 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
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.
'Python Library > TensorFlow' 카테고리의 다른 글
How To Convert Keras Model To .Pb format Example (0) | 2019.08.18 |
---|---|
Colab Tensorboard API (0) | 2019.08.18 |
how to off Learning_phase for dropout and batch_norm when export .pb (0) | 2019.08.18 |
How to Freeze Variables to Export keras model as .pb - 1 (0) | 2019.08.18 |
tf.graph_util.convert_variables_to_constants 예제 (0) | 2019.08.14 |