【问题标题】:How to see multiple images through tf.image_summary如何通过 tf.image_summary 查看多张图片
【发布时间】:2016-01-09 17:47:56
【问题描述】:

问题 - TensorBoard 上只显示一张图片

受此启发 How can I visualize the weights(variables) in cnn in Tensorflow?

代码如下:

# --- image reader ---
# - rsq: random shuffle queue with [fn l] pairs 
def img_reader_jpg(rsq):
    fn, label  = rsq.dequeue()
    img_b = tf.read_file(fn)
    img_u = tf.image.decode_jpeg(img_b, channels=3) 
    img_f = tf.cast(img_u, tf.float32)  
    img_4 = tf.expand_dims(img_f,0)
    return img_4, label


# filenames and labels are pre-loaded
fv = tf.constant(fnames)
lv = tf.constant(ohl)

rsq    = tf.RandomShuffleQueue(len(fnames), 0, [tf.string, tf.float32])
do_enq = rsq.enqueue_many([fv, lv])

# reading_op 
image, label   = img_reader_jpg(rsq)

# test: some op
im_t     = tf.placeholder(tf.float32, shape=[None,30,30,3], name='img_tensor')
lab_t    = tf.placeholder(tf.float32, shape=[None,2],       name='lab_tensor')
some_op  = tf.add(im_t,im_t) 
ims_op   = tf.image_summary("img", im_t)

# service ops
init_op    = tf.initialize_all_variables()

#  run it
with tf.Session() as sess:

    summary_writer = tf.train.SummaryWriter(summ_dir, graph_def=sess.graph_def)
    print 'log at:', summ_dir

    sess.run(init_op)
    sess.run(do_enq)
    print "rsq.size:", rsq.size().eval()

    for i in xrange(5):
        print "\ni:",i

        img_i, lab_i = sess.run([image, label]) # read image - right?
        print "I:", img_i.shape , " L:", lab_i

        feed_dict = {
            im_t: img_i
        }

        img2 = sess.run([some_op], feed_dict = feed_dict)

        # now summary part
        imss = sess.run(ims_op, feed_dict = feed_dict)
        #print "imss",imss
        summary_writer.add_summary(imss,i) 

    print "rsq.size:", rsq.size().eval()
    summary_writer.close()

print 'ok'

这里是输出:

log at: /mnt/code/test_00/log/2016-01-09 17:10:37
rsq.size: 1225

i: 0
I: (1, 30, 30, 3)  L: [ 1.  0.]

i: 1
I: (1, 30, 30, 3)  L: [ 1.  0.]

i: 2
I: (1, 30, 30, 3)  L: [ 0.  1.]

i: 3
I: (1, 30, 30, 3)  L: [ 0.  1.]

i: 4
I: (1, 30, 30, 3)  L: [ 0.  1.]
rsq.size: 1220
ok

看起来不错

  • 5 对 [图像标签] 已交付
  • 如果我取消注释 print "imss",imss 我可以看到 5 个不同的缓冲区,每个缓冲区都有自己的 png 图像
  • 操作图在 TB 中看起来没问题

但是,TB 中只有一张图片。我怀疑我错过了一些关于 TF 工作方式的重要信息 - 即。是什么导致了图形执行时的情况。

第二个问题:我需要做什么才能看到结果,即 img2 = img+img in TB?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    你是对的,你只会看到一张图片。您在每个 for 循环中调用一次图像摘要操作,每次调用它时,您都传递给它一个图像。

    要查看所有想要查看的图像,您可以做的是将这些图像编译成一个张量。如果我们参考 TensorFlow API(链接总是变化,所以找到最新的)

    tf.image_summary(tag, tensor, max_images=3, collections=None, 名称=无)

    从 TF 1.0.0 开始,是这样的:

    tf.summary.image(name, tensor, max_outputs=3, collections=None)

    把你的“多张图片张量”放进去,把max_images设置成你拥有的图片数量,你应该可以看到TensorBoard中的所有图片了。

    如果还有问题,请告诉我。

    【讨论】:

    • 我做了“多图像张量”image_summary - 它确实有效。但是我们不使用多维张量来存储标量摘要 - 我们只是在每次训练迭代或其他任何情况下写入当前标量。那么为什么图像有不同的行为呢?我会将您的答案标记为可以,因为它确实提供了我会说一些解决方法。
    • 你说得对,图像有不同的行为。为什么它可能是这样的一个合乎逻辑的原因是因为标量摘要的大小很小并且很容易累积。如果要累积所有图像摘要,我可以想象摘要文件会非常大。如果您想在 TensorBoard 中使用这样的功能,也许您可​​以在他们的 GitHub 上将其作为问题提出。那里的人能给出比我更好的答案。
    【解决方案2】:

    从 r0.12 开始,tf.image_summary 已替换为 tf.summary.image

    tf.summary.image(name, tensor, max_outputs=3, collections=None)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 2015-06-18
      相关资源
      最近更新 更多