Reading in jpeg files and displaying them in Tensorboard

8,720 views
Skip to first unread message

Durham Smith

unread,
Nov 15, 2015, 6:05:53 PM11/15/15
to Discuss
Hi all I am wondering if anyone can help. 

My goal is to read in a batch of jpg files and convert them to a tensor of shape [height, width, channels] where channels =3. 
I then would like to display use tensorboard to check if these images have loaded correctly.




As far as I understand I should do the following:

import tensorflow as tf
import os

def read_image_data():
 dirname
, filename = os.path.split(os.path.abspath(__file__))
 
#Create a list of filenames (for whaledata they are jpegs)
 filenames
= ['/data_dir/w_7489.jpg']
 
#Create a queue that produces the filenames to read
 filename_queue
= tf.train.string_input_producer(filenames)
 
#Create a reader for the filequeue
 reader
= tf.WholeFileReader()
 
#Read in the files
 key
, value = reader.read(filename_queue)
 
#Convert the Tensor(of type string) to representing the Tensor of type uint8
 
# and shape [height, width, channels] representing the images
 images
= tf.image.decode_jpeg(value, channels=3)
 
#convert images to floats and attach image summary
 float_images
= tf.cast(images, tf.float32)
 tf
.image_summary('images', float_images)
 
#Create session
 sess
= tf.Session()
 summary_op
= tf.merge_all_summaries()
 tf
.initialize_all_variables()
 
#Write summary
 summary_writer
= tf.train.SummaryWriter(dirname+'/log/', graph_def=sess.graph_def)
 summary_str
= sess.run(summary_op)
 summary_writer
.add_summary(summary_str)
 
#Close session
 sess
.close()


def main(argv=None):
 read_image_data
()
 
return 0

if __name__ == '__main__':
 tf
.app.run()


When I execute this code the following happens:
1) The /log/ directory and an event file called events.out.tfevents.1447627799.Durhams-MacBook-Pro.local is created
2) The program never finishes execution (I need to kill -9 <pid> it)

Any insight into solving this would be great.

Additionally it would be great if someone can tell me how to handle cases where the jpegs I need to read in do not have the same dimensions.

Vijay Vasudevan

unread,
Nov 16, 2015, 11:49:30 AM11/16/15
to Durham Smith, Discuss
I suspect the section called "Creating threads to prefetch using QueueRunner objects" in http://tensorflow.org/how_tos/reading_data/index.md#QueueRunners may be useful.  My guess is that you need to spawn a few threads / queue runners to decouple the running of the queue from the processing of the results.

For your second question, http://tensorflow.org/api_docs/python/image.md#AUTOGENERATED-resizing will probably help.

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/126e2ffe-e6d8-4e55-b2d9-09717d6569f9%40tensorflow.org.

martin...@gmail.com

unread,
Nov 18, 2015, 6:14:18 AM11/18/15
to Discuss, durham...@gmail.com
Hey, I got here from a stackoverflow thread and while this helped, I wanted you to know that the links in your reply 404 - this should fix the first link: http://tensorflow.org/how_tos/reading_data/index.html#QueueRunner, but I have no idea about the second!

durham...@gmail.com

unread,
Nov 18, 2015, 10:01:46 AM11/18/15
to Discuss, durham...@gmail.com
Thanks for the help Vijay and Martin!

For anyone that has the same issue in future two things are needed to be changed from the code posted above. They are:
  1. Add a batchsize to float_images in my code above, if you want to test it with only one image you can use the expand_dims function.
  2. Start the queue runner [ tf.train.start_queue_runners(sess=sess)]
Martin as for the 2nd link not working you can find it going to the api page ->python->images->resizing (I've notice some issues when linking to the pages containing the api quite often now).

Michele Campus

unread,
Apr 20, 2016, 12:33:18 PM4/20/16
to Discuss, durham...@gmail.com
Noticed that this question refer to here, I'd like to know how is possibile that the answer are consider correct.
I tried and the code does not properly work if you don't modify the "eval" line like this: image = my_img.eval(session=sess).

Is there something that I miss ?

Thanks

Martin Tutek

unread,
Apr 20, 2016, 12:41:28 PM4/20/16
to Michele Campus, Discuss, durham...@gmail.com
Hi,

it is not mentioned in the post, however the problem was posted for tensorflow version 0.5 (current stable vesion is 0.8). So probably there will be some syntax differences - so image = my_img.eval(session=sess)   is the only change required?

I'll edit this ASAP,
Cheers,


--
You received this message because you are subscribed to a topic in the Google Groups "Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/a/tensorflow.org/d/topic/discuss/ik7hSLIKEBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss+u...@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

Michele Campus

unread,
Apr 20, 2016, 12:45:54 PM4/20/16
to Martin Tutek, Discuss, durham...@gmail.com
Glad to help you.
I also have problem with Image.show().
Image.show(Image.fromarray(np.asarray(image)))
AttributeError: 'module' object has no attribute 'show'
--
Michele Campus
Reply all
Reply to author
Forward
0 new messages