How to view multiple images via tf.image_summary

Problem - TensorBoard only shows one image

Inspired by this How to visualize weight (variables) in cnn in Tensorflow?

Here is the code:

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

It displays here:

 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 

It looks fine

  • 5 pairs of images were sent [image label].
  • in case I uncomment print "imss", imss I see 5 different buffers, each with its own png image
  • op graph looks fine in tb

However, only one image in TB. I suspect that I missed something important about how TF --.ie works, which caused the runtime on the chart.

The second question: what do I need to do to see the result, i.e. img2 = img + img in TB?

+8
tensorflow
source share
2 answers

You are right to see only one image. You invoke a summary of the op image once in each cycle, and each time you invoke it, you pass it one image.

What you could do to see all the images you want to see is to compile these images into one tensor. If we link to the TensorFlow API (the link always changes, so find the last one)

tf.image_summary (tag, tensor, max_images = 3, collections = None, name = None)

Starting with TF 1.0.0, this is:

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

Put your "multiple image tensor", set max_images to the number of images you have, and you can see all the images in TensorBoard.

Let me know if there are any problems.

+6
source share

Starting from r0.12, tf.image_summary been replaced by tf.summary.image

 tf.summary.image(name, tensor, max_outputs=3, collections=None) 
+1
source share

All Articles