Reputation: 1051
I am trying to read and decode an image file using Tensorflow. I have the following code:
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path + '/images/cat/cat1.jpg'
image_file = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_file, channels=3)
print(image_file)
print(image_decoded)
This results in the following output:
Tensor("ReadFile:0", shape=(), dtype=string)
Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8)
It appears as if the file is not read at all by Tensorflow. However I could not find any error messages indicating that something went wrong. I don't know how I can resolve this, any help would be greatly appreciated!
Upvotes: 4
Views: 6833
Reputation: 134
That was one big obstacle when we first tried to use Tensorflow.
Now, Tensorflow team has made a solution.
import tensorflow as tf
tf.enable_eager_execution()
Run the above two lines at the very beginning of your program.
Then your print functions will produce something like the followings.
<tf.Tensor: id=15, shape=(), dtype=string, numpy=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\
<tf.Tensor: id=17, shape=(747, 1024, 3), dtype=uint8, numpy=
array([[[ 0, 0, 0],
[ 0, 0,
Tensorflow team calls it eager execution. Now we don't need to run the tf.Session to see what are inside tensors.
Upvotes: 4
Reputation: 1012
Tensorflow creates a computation graph which should then be evaluated. What you see over there in the result is the op that is created. You need to define a Session object to get the results of your operations.
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path + '/images/cat/cat1.jpg'
image_file = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_file, channels=3)
with tf.Session() as sess:
f, img = sess.run([image_file, image_decoded])
print(f)
print(img)
Check out this tensorflow resource to help you further understand!
Upvotes: 5