张量流图节点名称与操作名称不匹配

tensorflow graph node names dont match operation names

我是tensorflow的新手,我对tensorflow教程中的图形结构感到困惑label_image。使用代码自爆:

import tensorflow as tf
with tf.Session() as sess:
    with open('/var/tmp/feng/tensorflow/tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

        print graph_def

我可以看到图结构是这样的

...
...
node {
  name: "InceptionV3/Predictions/Reshape_1"
  op: "Reshape"
  input: "InceptionV3/Predictions/Softmax"
  input: "InceptionV3/Predictions/Shape"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "Tshape"
    value {
      type: DT_INT32
    }
  }
}

没关系。我可以看到最后一个节点名称是 "InceptionV3/Predictions/Reshape_1",但是,当我使用代码 blew 来显示所有操作时

the operation in this graph
    with tf.Session(graph=graph) as sess:
        # results = sess.run(output_tensor, {
        #     input_tensor: t
        # })
        print(sess.graph.get_operations())

它显示我是这样的

 <tf.Operation 'import/InceptionV3/Predictions/Softmax' type=Softmax>,
 <tf.Operation 'import/InceptionV3/Predictions/Shape' type=Const>, 
 <tf.Operation 'import/InceptionV3/Predictions/Reshape_1' type=Reshape>

为什么操作名称在节点前添加 import name.It 很奇怪,因为字符串 import 从未出现在这个 model.I google 的图中,但每个人都说函数

get_operation_by_name

return类型tf.Operation的实例,是计算图中的一个节点,所以混淆了操作名和节点名的区别.谁能帮帮我?

很可能,您使用 import_graph_def 或最终调用 import_graph_def 的某些 higher-level 函数导入了图表。这是其 name 参数的文档副本:

name: (Optional.) A prefix that will be prepended to the names in 
      graph_def. Note that this does not apply to imported function 
      names. Defaults to "import".

如果您不想添加任何前缀,请使用 name=''

调用它