agupta231
agupta231

Reputation: 1171

Importing images in tensorflow

I'm trying to import images and convert them to tensors. All the other solutions recommend making a filename_queue and using tf.reader(), but I can't get that to work... hence, I'm just going with the basics.

I have a file called test.jpg in my desktop directory, and I'm running a linux environment. Here is my code:

import tensorflow as tf

image = tf.image.decode_jpeg("~/Desktop/test.jpg", channels=1)
print(image)

As you can see, some very simple code... however it outputs

Tensor("DecodeJpeg:0", shape=(?, ?, 1), dtype=uint8)

Which is telling me that it isn't reading the file correctly. Is there anything that I am doing wrong?

Thanks!

Upvotes: 1

Views: 5435

Answers (1)

keveman
keveman

Reputation: 8487

The function tf.image.decode_jpeg just constructs a graph node and adds it to an execution graph. You have to actually evaluate the node to run it and get its value as a numpy array. Try the following :

import tensorflow as tf

image = tf.image.decode_jpeg(tf.read_file("~/Desktop/test.jpg"), channels=1)
sess = tf.InteractiveSession()
print(sess.run(image))

Note that the argument to tf.image.decode_jpeg itself should be a string Tensor. Thankfully, TensorFlow has an op for reading a file into a Tensor, viz., tf.read_file.

Upvotes: 5

Related Questions