Python Tensorflow中图形中的张量名称列表

Python Tensorflow中图形中的张量名称列表,python,tensorflow,Python,Tensorflow,Tensorflow中的graph对象有一个名为“get_tensor_by_name(name)”的方法。是否仍然可以获得有效张量名称的列表 如果没有,是否有人知道预训练模型inception-v3的有效名称?从他们的例子来看,pool_3是一个有效的张量,但是列出所有这些张量就好了。我看了一下,其中一些图层似乎与表1中的尺寸相对应,但不是全部。这篇论文没有准确地反映模型。如果您从arxiv下载源代码,它有一个准确的模型描述model.txt,其中的名称与发布模型中的名称密切相关 要回答第一个

Tensorflow中的graph对象有一个名为“get_tensor_by_name(name)”的方法。是否仍然可以获得有效张量名称的列表


如果没有,是否有人知道预训练模型inception-v3的有效名称?从他们的例子来看,pool_3是一个有效的张量,但是列出所有这些张量就好了。我看了一下,其中一些图层似乎与表1中的尺寸相对应,但不是全部。

这篇论文没有准确地反映模型。如果您从arxiv下载源代码,它有一个准确的模型描述model.txt,其中的名称与发布模型中的名称密切相关


要回答第一个问题,
sess.graph.get_operations()
提供了一个操作列表。对于一个op,
op.name
为您提供名称,
op.values()
为您提供它生成的张量列表(在inception-v3模型中,所有张量名称都是op名称,后面附加了“:0”,因此
pool\u 3:0
是最终池op生成的张量)。

查看图表中的操作(你会看到很多,所以简而言之,我这里只给出了第一个字符串)

sess=tf.Session()
op=sess.graph.get_操作()
[m.values()表示op中的m][1]
输出:
(,)

您甚至不必创建会话来查看图形中所有操作名称的名称。为此,您只需获取一个默认图形并提取所有操作:。每个操作都有一个名称,您需要的名称是

代码如下:

import tensorflow as tf
a = tf.Variable(5)
b = tf.Variable(6)
c = tf.Variable(7)
d = (a + b) * c

for i in tf.get_default_graph().get_operations():
    print i.name

作为嵌套列表理解:

tensor_names = [t.name for op in tf.get_default_graph().get_operations() for t in op.values()]
函数获取图形中张量的名称(默认为默认图形):

函数获取图形中的张量(默认为默认图形):


以上答案是正确的。我为上述任务找到了一个易于理解/简单的代码。请在此处分享:-

import tensorflow as tf

def printTensors(pb_file):

    # read pb into graph_def
    with tf.gfile.GFile(pb_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # import graph_def
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def)

    # print operations
    for op in graph.get_operations():
        print(op.name)


printTensors("path-to-my-pbfile.pb")

saved\u model\u cli
是TF附带的另一种命令行工具,如果您处理“SavedModel”格式,它可能会很有用

此输出可能很有用,例如:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['dense_input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1280)
        name: serving_default_dense_input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['dense_1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall:0
  Method name is: tensorflow/serving/predict

感谢您的快速回答!对于这个预训练模型,model.txt和outputshape中似乎仍然存在一些差异。例如,如果我查看“pool:0”我猜这是第一个池层,我得到了73x73x64的形状,但在model.txt中它后面的层的输入是73x73x80。还是我误解了什么?@john我没有深入挖掘model.txt中的注释,我认为注释中有一些不一致的地方-我没有在非注释中发现不一致的地方。Fo在池层中,前一个卷积有64个滤波器组(Conv_2赋值中Conv的第二个参数),所以池层也有64个通道。80是下一个转换层的输出数…@etarion我在哪里可以下载这个model.txt?你能给我一个直接链接吗?谢谢advance@OleksandrKhryplyvenko这是@etarion源代码发行版的一部分,谢谢!顺便说一句,我已经构建了inception v3图。内存消耗过多正如您告诉我的,在构建图的过程中,n是由重复的条目引起的。这是可行的,但出于某种原因,inception模型(我唯一尝试过的模型)在它的大多数名称中都有一个
:0
,并且它没有被上面的
i.name
代码所反映。这是为什么?AttributeError:模块“tensorflow”没有属性“get\u default\u graph”-这也是tf2.x的代码吗?要在>=Tf 2.0中获取图形,请不要使用Tf.get\u default\u graph()。相反,您需要先实例化tf.function并按如下方式访问graph属性:graph=func.get_concrete_function()。graph只是对,
op.values()的一个小更新
已被弃用。请改用
op.outputs
。我从ParseFromString中获得了一条
google.protobuf.message.DecodeError:Error解析消息
。但我使用的是一个
保存的\u模型
。为什么TF中的模型有这么多格式?有点糟糕。
def get_tensors(graph=tf.get_default_graph()):
    return [t for op in graph.get_operations() for t in op.values()]
import tensorflow as tf

def printTensors(pb_file):

    # read pb into graph_def
    with tf.gfile.GFile(pb_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # import graph_def
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def)

    # print operations
    for op in graph.get_operations():
        print(op.name)


printTensors("path-to-my-pbfile.pb")
!saved_model_cli show --dir /tmp/mobilenet/1 --tag_set serve --all
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['dense_input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1280)
        name: serving_default_dense_input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['dense_1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall:0
  Method name is: tensorflow/serving/predict