【问题标题】:Save Tensorflow graph for viewing in Tensorboard without summary operations保存 Tensorflow 图以在 Tensorboard 中查看,无需汇总操作
【发布时间】:2019-04-14 23:26:57
【问题描述】:

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

我试过这个:

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

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

这似乎是相关的: Graph visualisaton is not showing in tensorboard for seq2seq model

【问题讨论】:

    标签: tensorflow tensorboard


    【解决方案1】:

    为了提高效率,tf.train.SummaryWriter 异步记录到磁盘。为确保图表出现在日志中,您必须在程序退出前调用编写器上的close()flush()

    【讨论】:

    • tf.summary.FileWriter 最新版本的 TF
    【解决方案2】:

    您还可以将图形转储为 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按钮
    • 我是这里的新手。那么,怎么上传呢?谢谢!
    • 如果我运行 tensorboard --logdir ./ 没有摘要输出,“图表”选项卡的左侧没有面板。
    • 我想你的意思是:>>> 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) 此评论适用于 tensorflow 新用户。
    【解决方案3】:

    这对我有用:

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

    使用“--logdir=logdir/”启动张量板时会自动加载图表。不需要“上传”按钮。

    【讨论】:

    • 感谢 y g(用户 5656195)修复错字“FileWrite”而不是“FileWriter”。 3 个审稿人拒绝了这个编辑,说“打算向作者致辞”(大概是因为 y g 在编辑解释的末尾放了一个问号)。疯了。
    • tf.train.SummaryWriter 已弃用,改为使用 tf.summary.FileWriter。
    【解决方案4】:

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

    使用以下命令初始化编写器:

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

    并使用编写器:

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

    【讨论】:

      【解决方案5】:

      除此之外没有什么对我有用

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

      【讨论】:

      • 我很好奇为什么我们需要session?我们可以通过调用tf.compat.v1.get_default_graph() 来代替吗?
      猜你喜欢
      • 1970-01-01
      • 2019-07-21
      • 2016-03-31
      • 2017-07-13
      • 2021-09-08
      • 2019-08-04
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多