Kong
Kong

Reputation: 2412

tensorflow get size of string_input_producer

How do I check the number of filenames string_input_producer has read? Different operations will be performed depending on size in input data so I need to know how many images will be read or have been read.

Code below not telling me how much images I have read or am about to read.

import tensorflow as tf
import matplotlib.pyplot as plt

# Make a queue of file names including all the JPEG images files in the relative image directory.
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("./MNIST_data/*.png"))

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

image = tf.image.decode_png(value) # use png or jpg decoder based on your files.

num_preprocess_threads = 1
min_queue_examples = 256
batch_size=2;
images = tf.train.shuffle_batch([image], batch_size, min_queue_examples + 3 * batch_size, num_threads=num_preprocess_threads, min_after_dequeue=min_queue_examples)

with tf.Session() as sess:
  sess.run(tf.initialize_all_variables())

  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  t_image = image.eval() #here is your image Tensor :) 
  fig = plt.figure()
  plt.imshow(t_image)
  plt.show()

  coord.request_stop()
  coord.join(threads)

Upvotes: 2

Views: 905

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222751

string_input_producer returns you back a standard FIFOQueue (it returns you an input_producer and it returns you a queue.

A FIFOQueue does not have information about the number of elements it has read, only the number of elements are currently in a queue (q.size()). If you want to know how many element has been read you need to manually add a counter which you will increment each time you read an element.

Upvotes: 0

Jie.Zhou
Jie.Zhou

Reputation: 1318

Functions like string_input_producer will add a queue to current graph which can only dequeue only one example each time. Usually the output tensors will be feed to functions like tf.train.shuffle_batch which is what you want. The argument batch_size of this function can control how many examples each time dequeued as the input of of your model

UPDATE:

if you want to check whether your input data is correct, you can run it out with sess.run(my_img) which will give you a numpy.array tensor. You can directly look at the element of this tensor or just plot it with matplotlib.

make sure you have already started queue runners before sess.run or your program will hang forever

Upvotes: 1

Related Questions