从 .ckpt 和 .meta 文件中获取输入和输出节点名称 tensorflow

Ash*_*hra 4 python deep-learning tensorflow

我有张量流模型的 .meta 和 .ckpt 文件。我想知道确切的输入和输出节点名称,但我通过遵循获取节点名称列表。

当我有一个冻结的 protobuf 模型时,我使用以下代码将输入节点名称和输出节点名称作为列表的开头和结尾:

import tensorflow as tf
from tensorflow.python.platform import gfile
GRAPH_PB_PATH = 'frozen_model.pb'
with tf.Session() as sess:
   print("load graph")
   with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f:
       graph_def = tf.GraphDef()
   graph_def.ParseFromString(f.read())
   sess.graph.as_default()
   tf.import_graph_def(graph_def, name='')
   graph_nodes=[n for n in graph_def.node]
   names = []
   for t in graph_nodes:
      names.append(t.name)
   print(names)
Run Code Online (Sandbox Code Playgroud)

我可以为 .ckpt 或 .meta 文件做类似的事情吗?

小智 8

.meta文件包含有关张量流图中不同节点的信息。这在此处得到了更好的解释。

此时图中不同变量的值分别存储在checkpoint.data-xxxx-of-xxxx文件中的检查点文件夹中。

与冻结模型的情况相反,正常检查点过程中没有输入或输出节点的概念。冻结模型输出整个张量流图的一个子集。主图的这个子集只有输出节点所依赖的那些节点。因为冻结模型是为了服务目的,它将张量流变量转换为常量,无需在每个步骤存储额外的信息,如不同变量的梯度。

如果您仍然想确定您感兴趣的节点,您可以从.meta文件中恢复您的图形并在 tensorboard 中对其进行可视化。

import tensorflow as tf
from tensorflow.summary import FileWriter

sess = tf.Session()
tf.train.import_meta_graph("your-meta-graph-file.meta")
FileWriter("__tb", sess.graph)
Run Code Online (Sandbox Code Playgroud)

这将__tb在您的当前目录中创建一个文件夹,然后您可以通过发出以下命令来查看图表。

tensorboard --logdir __tb
Run Code Online (Sandbox Code Playgroud)

是一个链接到某个模型的屏幕截图,其中选择了一个节点。您可以从右上角获取节点的名称。