bluesummers
bluesummers

Reputation: 12607

TensorFlow read and decode BATCH of images

Using tf.train.string_input_producer and tf.image.decode_jpeg I manage to read from disk and decode a single image.

This is the code:

# -------- Graph
filename_queue = tf.train.string_input_producer(
    [img_path, img_path])

image_reader = tf.WholeFileReader()

key, image_file = image_reader.read(filename_queue)

image = tf.image.decode_jpeg(image_file, channels=3)

# Run my network
logits = network.get_logits(image)

# -------- Session
sess = tf.Session()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

logits_output = sess.run(logits)

The thing is, that when I look at the shape of the logit_outputs I get only 1 value even though the queue is 2 images long.

How can I read and decode the entire queue?

Upvotes: 2

Views: 2488

Answers (1)

benjaminplanche
benjaminplanche

Reputation: 15119

tf.WholeFileReader(), along tf.train.string_input_producer() work as an iterator, and thus does not have an easy way to evaluate the size of the complete dataset it is handling.

To obtain batches of N samples out of it, you could instead use image_reader.read_up_to(filename_queue, N).

Note: you can achieve the same using the newer tf.data pipeline:

def _parse_function(filename):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_image(image_string)
  return image_decoded 

# A vector of filenames.
filenames = tf.constant([img_path, img_path])

dataset = tf.data.Dataset.from_tensor_slices((filenames))
dataset = dataset.map(_parse_function).batch(N)
iterator = dataset.make_one_shot_iterator()
next_image_batch = iterator.get_next()

logits = network.get_logits(next_image_batch)
# ...

Upvotes: 3

Related Questions