Moondra
Moondra

Reputation: 4521

Reading image files into Tensorflow with tf.WholeFileReader()

I'm trying to read a directory of images files into tensorflow and I'm having a little trouble. When I run the script, the shell is just hanging (even waited 10 mins for an output) and the only output I get is the from the print(len(os.listdir()) line

My attempts stem from this guide:

https://www.tensorflow.org/api_guides/python/reading_data

import tensorflow as tf
import os
os.chdir(r'C:\Users\Moondra\Desktop\Testing')
print(len(os.listdir()))  # only 2 in this case

file_names =tf.constant(os.listdir(), tf.string)
file_tensors =tf.train.string_input_producer(string_tensor = file_names)
reader =tf.WholeFileReader()
key, value = reader.read(file_tensors)
##features = tf.parse_single_example(value)
#records = reader.num_records_produced()

with tf.Session() as sess:
    values =sess.run(value)
    ##print(records_num)
    print(type(values))

The reader is supposed to read images one at a time, so I'm assuming value will hold the image values on on the current image. Despite this, the shell is just hanging and no output.

Thank you and --just incase @mrry is available.

Upvotes: 0

Views: 388

Answers (1)

greeness
greeness

Reputation: 16124

tf.train.string_input_producer adds a QueueRunner to the current Graph and you need to manually start it. Otherwise it is just hanging and no output is produced.

with tf.Session() as sess:
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  values = sess.run(value)
  # ....

Upvotes: 1

Related Questions