Python 你如何获得 Keras 模型中 tensorflow 输出节点的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40028175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:05:44  来源:igfitidea点击:

How do you get the name of the tensorflow output nodes in a Keras Model?

pythontensorflowkeras

提问by agsolid

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?

我正在尝试从我的 Keras(tensorflow 后端)模型创建一个 pb 文件,以便我可以在 iOS 上构建它。我正在使用 freeze.py,我需要传递输出节点。如何获取我的 Keras 模型的输出节点的名称?

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

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

采纳答案by BerndSchmitt

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

您可以使用 Keras model.summary() 来获取最后一层的名称。

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

如果 model.outputs 不为空,您可以通过以下方式获取节点名称:

[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

之后,您可以通过编写 protobuf 文件

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

回答by Oleg F.

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

如果在 Keras 中构建模型时没有明确指定输出节点,您可以像这样打印出来:

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

[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_namesin freeze_graphfunction.

然后你需要做的就是找到合适的,这通常类似于激活函数的名称。您可以使用您找到的这个字符串名称作为output_node_namesinfreeze_graph函数的值。

回答by Monster

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

您还可以使用 tensorflow 实用程序:summarize_graph找到可能的output_nodes. 从官方文档

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.

该工具支持的许多转换需要知道模型的输入和输出层是什么。这些的最佳来源是模型训练过程,对于分类器,输入将是从训练集接收数据的节点,输出将是预测。如果您不确定,summary_graph 工具可以检查模型并提供有关可能的输入和输出节点的猜测,以及对调试有用的其他信息。

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

它只需要保存的图形pb文件作为输入。查看文档以获取示例。

回答by Saurabh Saxena

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

output_node_names应该包含您打算使用的推理(如SOFTMAX)图中节点的名称。它用于提取推理所需的子图。查看freeze_graph_test可能很有用。