Given the graph of the tensor flow model, how to find node input and display node names

I am using a custom model for classification in the Tensor Flow Camera Demo. I generated a .pb file (a serialized protobuf file) and was able to display the huge graph that it contains. To convert this graph to optimized, as described in [ https://www.oreilly.com/learning/tensorflow-on-android] , you can use the following procedure:

$ 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

Here's how to find the input and output names from the graph. When I do not use my own names, the device crashes:

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)]]
+17
source share
3 answers

Try the following:

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']

, node . , , - ?

:

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

UPDATE: ,   () , :

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

, (re) / op "Mul" "Softmax", / "Placeholder" "Softmax".

BTW, : https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/. memmapped graph - , , memmapped graph android, ...:( ( / android)

+18

:

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

( 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)
0

Source: https://habr.com/ru/post/1675248/


All Articles