Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see multiple images through tf.image_summary

Tags:

tensorflow

Problem - only one image is shown at TensorBoard

Inspired by this How can I visualize the weights(variables) in cnn in Tensorflow?

Here is 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'

Here is output:

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

Looks ok

  • 5 [image label] pairs were delivered
  • in case I uncomment print "imss",imss I can see 5 different buffers each with own png image
  • op graph looks ok in TB

However only one image in TB. I suspect I have missed something important about how TF is working -.i.e. what caused what at graph execution time.

Second question: what I need to do to see result i.e. img2 = img+img in TB?

like image 964
rgr Avatar asked Jan 09 '16 17:01

rgr


People also ask

What is summary in TensorFlow?

summary module provides APIs for writing summary data. This data can be visualized in TensorBoard, the visualization toolkit that comes with TensorFlow. See the TensorBoard website for more detailed tutorials about how to use these APIs, or some quick examples below.


2 Answers

As of r0.12, tf.image_summary has been replaced with tf.summary.image

tf.summary.image(name, tensor, max_outputs=3, collections=None)
like image 34
ahmedhosny Avatar answered Dec 06 '22 05:12

ahmedhosny


You are right that you will only see one image. You are calling the image summary op once in each for loop, and each time you call it, you are passing it a single image.

What you could do to see all images that you want to see, would be to compile these images into a single tensor. If we refer to TensorFlow API (link always changes so find the latest one)

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

As of TF 1.0.0, it's this:

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

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

Let me know if there are still problems.

like image 167
jkschin Avatar answered Dec 06 '22 05:12

jkschin