Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow read_file() does nothing

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!

like image 612
Dirk Hoekstra Avatar asked Jan 02 '23 18:01

Dirk Hoekstra


1 Answers

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!

like image 77
kvish Avatar answered Jan 12 '23 00:01

kvish