保存 TensorFlow 图以在 Tensorboard 中查看,无需汇总操作

Save Tensorflow graph for viewing in Tensorboard without summary operations

我有一个相当复杂的 Tensorflow 图,我想将其可视化以用于优化目的。有没有我可以调用的函数,它可以简单地保存图表以便在 Tensorboard 中查看而无需注释变量?

我试过这个:

merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/Users/Name/Desktop/tf_logs", session.graph_def)

但是没有输出。这是使用0.6的轮子。

这似乎是相关的:

为了效率,在程序退出前tf.train.SummaryWriter logs asynchronously to disk. To ensure that the graph appears in the log, you must call close() or flush()在writer上

您还可以将图形转储为 GraphDef protobuf,然后将其直接加载到 TensorBoard 中。您可以在不启动会话或 运行 模型的情况下执行此操作。

## ... create graph ...
>>> graph_def = tf.get_default_graph().as_graph_def()
>>> graphpb_txt = str(graph_def)
>>> with open('graphpb.txt', 'w') as f: f.write(graphpb_txt)

这将输出一个看起来像这样的文件,具体取决于您的模型的具体情况。

node {
  name: "W"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
...
version 1

在 TensorBoard 中,您可以使用 "Upload" 按钮从磁盘加载它。

为清楚起见,这就是我使用 .flush() 方法并解决问题的方式:

初始化编写器:

writer = tf.train.SummaryWriter("/home/rob/Dropbox/ConvNets/tf/log_tb", sess.graph_def)

并将编写器用于:

writer.add_summary(summary_str, i)
    writer.flush()

这对我有用:

graph = tf.Graph()
with graph.as_default():
    ... build graph (without annotations) ...
writer = tf.summary.FileWriter(logdir='logdir', graph=graph)
writer.flush()

使用“--logdir=logdir/”启动 tensorboard 时会自动加载图表。不需要 "upload" 按钮。

除了这个对我没用

# Helper for Converting Frozen graph from Disk to TF serving compatible Model
def get_graph_def_from_file(graph_filepath):
  tf.reset_default_graph()
  with ops.Graph().as_default():
    with tf.gfile.GFile(graph_filepath, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString(f.read())
      return graph_def

#let us get the output nodes from the graph
graph_def =get_graph_def_from_file('/coding/ssd_inception_v2_coco_2018_01_28/frozen_inference_graph.pb')

with tf.Session(graph=tf.Graph()) as session:
    tf.import_graph_def(graph_def, name='')
    writer = tf.summary.FileWriter(logdir='/coding/log_tb/1', graph=session.graph)
    writer.flush()

然后使用 TB 工作

#ssh -L 6006:127.0.0.1:6006 root@<remoteip> # for tensor board - in your local machine type 127.0.0.1
!tensorboard --logdir '/coding/log_tb/1'