【问题标题】:Outputting an image summary to Tensorboard将图像摘要输出到 Tensorboard
【发布时间】:2018-05-20 17:04:26
【问题描述】:

我正在尝试使用this code 可视化卷积层的过滤器,但无法将图像写入我的摘要文件。我输出的标量没有问题,我尝试修改代码以添加图像,如下所示

summary = tf.Summary()
summary.value.add(tag='Perf/Reward', simple_value=float(mean_reward))
summary.value.add(tag='Perf/Length', simple_value=float(mean_length))
with tf.variable_scope(self.name + "/conv1", reuse=True):
    weights = tf.get_variable("weights")
    grid = put_kernels_on_grid(weights)
    image = tf.summary.image('conv1/weights', grid, max_outputs=1)
    summary.value.add(tag='conv1/weights', image=image)
self.summary_writer.add_summary(summary, episode_count)

只有标量,这工作正常,但添加图像会产生错误

TypeError: Parameter to MergeFrom() must be instance of same class: expected Image got Tensor. for field Value.image

我也尝试通过将代码更改为直接添加图像摘要

summary = tf.Summary()
summary.value.add(tag='Perf/Reward', simple_value=float(mean_reward))
summary.value.add(tag='Perf/Length', simple_value=float(mean_length))
with tf.variable_scope(self.name + "/conv1", reuse=True):
    weights = tf.get_variable("weights")
    grid = put_kernels_on_grid(weights)
    image = tf.summary.image('conv1/weights', grid, max_outputs=1)
    self.summary_writer.add_summary(image, episode_count)
self.summary_writer.add_summary(summary, episode_count)

但是出错了

AttributeError: 'Tensor' object has no attribute 'value'

将图像输出到摘要文件的正确方法是什么?

【问题讨论】:

    标签: python tensorflow tensorboard


    【解决方案1】:

    put_kernels_on_grid 正在返回一个张量;通过“图像”,我认为作者只是意味着您可以将其打印出来以查看内核的外观。尝试使用tf.summary.tensor_summary

    哎呀,对不起。我已经能够使用以下方法将张量保存为图像:

    import tensorflow as tf
    import numpy as np
    
    batch_xs = np.ones((100, 100, 1)) * 200
    init = tf.constant(batch_xs, dtype=tf.uint8)
    grid = tf.get_variable('var_name', dtype=tf.uint8, initializer=init)
    
    encoded_image = tf.image.encode_jpeg(grid)
    
    fwrite = tf.write_file("junk.jpeg", encoded_image)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        result = sess.run(fwrite)
    

    但是tf.image.encode_jpeg 仍然返回一个带有 DataType 字符串的张量,所以tf.summary.image 不会接受它。您正在使用的代码早于 TensorFlow 1.0,所以它肯定不会像编写的那样工作。

    希望这会有所帮助。

    【讨论】:

    • 给出的示例代码将摘要输出到 Tensorboard。 tf.summary.image() 接受一个张量并输出一个摘要协议缓冲区,因此它应该与摘要写入器一起使用。
    • 我试过这个方法,它大部分都在工作,但我遇到了另一个问题,即使我知道我的权重在变化,我的输出图像每次都是相同的(我还有另一个问题 here详细说明。我是否需要做一些额外的事情才能让它在每次打印出来时重新评估新的权重,因为我试图观察它如何随着时间的推移而变化?
    猜你喜欢
    • 2018-12-12
    • 2016-07-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2018-02-26
    • 2017-05-23
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多