给定张量流模型图,如何找到输入节点和输出节点名称

Given a tensor flow model graph, how to find the input node and output node names

我在 Tensor Flow Camera Demo 中使用自定义模型进行分类。 我生成了一个 .pb 文件(序列化的 protobuf 文件),我可以显示它包含的巨大图表。 要将此图转换为优化图,如 [https://www.oreilly.com/learning/tensorflow-on-android] 中给出的那样,可以使用以下过程:

$ bazel-bin/tensorflow/python/tools/optimize_for_inference  \
--input=tf_files/retrained_graph.pb \
--output=tensorflow/examples/android/assets/retrained_graph.pb
--input_names=Mul \
--output_names=final_result

这里是如何从图形显示中找到 input_names 和 output_names。 当我不使用专有名称时,设备会崩溃:

E/TensorFlowInferenceInterface(16821): Failed to run TensorFlow inference 
with inputs:[AvgPool], outputs:[predictions]

E/AndroidRuntime(16821): FATAL EXCEPTION: inference

E/AndroidRuntime(16821): java.lang.IllegalArgumentException: Incompatible 
shapes: [1,224,224,3] vs. [32,1,1,2048]

E/AndroidRuntime(16821):     [[Node: dropout/dropout/mul = Mul[T=DT_FLOAT, 
_device="/job:localhost/replica:0/task:0/cpu:0"](dropout/dropout/div, 
dropout/dropout/Floor)]]

试试这个:

运行 python

>>> import tensorflow as tf
>>> gf = tf.GraphDef()
>>> gf.ParseFromString(open('/your/path/to/graphname.pb','rb').read())

然后

>>> [n.name + '=>' +  n.op for n in gf.node if n.op in ( 'Softmax','Placeholder')]

然后,你可以得到类似这样的结果:

['Mul=>Placeholder', 'final_result=>Softmax']

但我不确定这是关于错误消息的节点名称问题。 我猜你在加载图形文件时提供了错误的参数或者你生成的图形文件有问题?

检查这部分:

E/AndroidRuntime(16821): java.lang.IllegalArgumentException: Incompatible 
shapes: [1,224,224,3] vs. [32,1,1,2048]

更新: 对不起, 如果您使用的是(重新)训练过的图表,那么试试这个:

[n.name + '=>' +  n.op for n in gf.node if n.op in ( 'Softmax','Mul')]

似乎(重新)训练的图将 input/output 操作名称保存为 "Mul" 和 "Softmax",而优化的 and/or 量化图将它们保存为 "Placeholder" 和 "Softmax"。

顺便说一句, 根据 Peter Warden 的 post:https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/,不推荐在移动环境中使用再训练图。由于性能和文件大小问题,最好使用量化图或内存映射图,尽管我找不到如何在 android 中加载内存映射图...:( (在 android 中加载优化/量化图没有问题)

最近我直接从 tensorflow 遇到了这个选项:

bazel build tensorflow/tools/graph_transforms:summarize_graph    
bazel-bin/tensorflow/tools/graph_transforms/summarize_graph
--in_graph=custom_graph_name.pb

我写了一个简单的脚本来分析计算图(通常是DAG,直接无环图)中的依赖关系。很明显,输入是缺少输入的节点。然而,输出可以定义为图中的任何节点,因为在最奇怪但仍然有效的情况下,输出可以是输入,而其他节点都是虚拟的。我仍然将输出操作定义为代码中没有输出的节点。你可以随意忽略它。

import tensorflow as tf

def load_graph(frozen_graph_filename):
    with tf.io.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def)
    return graph

def analyze_inputs_outputs(graph):
    ops = graph.get_operations()
    outputs_set = set(ops)
    inputs = []
    for op in ops:
        if len(op.inputs) == 0 and op.type != 'Const':
            inputs.append(op)
        else:
            for input_tensor in op.inputs:
                if input_tensor.op in outputs_set:
                    outputs_set.remove(input_tensor.op)
    outputs = list(outputs_set)
    return (inputs, outputs)