54

I am building a standard image classification model with Tensorflow. For this I have input images, each assigned with a label (number in {0,1}). The Data can hence be stored in a list using the following format:

/path/to/image_0 label_0
/path/to/image_1 label_1
/path/to/image_2 label_2
...

I want to use TensorFlow's queuing system to read my data and feed it to my model. Ignoring the labels, one can easily achieve this by using string_input_producer and wholeFileReader. Here the code:

def read_my_file_format(filename_queue):
  reader = tf.WholeFileReader()
  key, value = reader.read(filename_queue)
  example = tf.image.decode_png(value)
  return example

#removing label, obtaining list containing /path/to/image_x
image_list = [line[:-2] for line in image_label_list]

input_queue = tf.train.string_input_producer(image_list)                                                     
input_images = read_my_file_format(input_queue)

However, the labels are lost in that process as the image data is purposely shuffled as part of the input pipeline. What is the easiest way of pushing the labels together with the image data through the input queues?

JohnAllen
  • 7,317
  • 9
  • 41
  • 65
MarvMind
  • 3,366
  • 2
  • 21
  • 19
  • I have a question: how you assign a label to an image ? I have 3 folder of images and I want to assign to every image the proper label. How can I do this ? – Kyrol May 08 '16 at 11:15
  • Well, this is task specific, depending what you want to classify. Lets say you have images of cats and dogs. You can define `cats := 0` and `dogs := 1`. Then you would assign 0 to all images displaying cats and 1 to all displaying dogs. You can try to assign labels however you want, as long as there are clear semantic criteria, so that the network is able to generalize well. – MarvMind May 08 '16 at 20:45

3 Answers3

51

Using slice_input_producer provides a solution which is much cleaner. Slice Input Producer allows us to create an Input Queue containing arbitrarily many separable values. This snippet of the question would look like this:

def read_labeled_image_list(image_list_file):
    """Reads a .txt file containing pathes and labeles
    Args:
       image_list_file: a .txt file with one /path/to/image per line
       label: optionally, if set label will be pasted after each line
    Returns:
       List with all filenames in file image_list_file
    """
    f = open(image_list_file, 'r')
    filenames = []
    labels = []
    for line in f:
        filename, label = line[:-1].split(' ')
        filenames.append(filename)
        labels.append(int(label))
    return filenames, labels

def read_images_from_disk(input_queue):
    """Consumes a single filename and label as a ' '-delimited string.
    Args:
      filename_and_label_tensor: A scalar string tensor.
    Returns:
      Two tensors: the decoded image, and the string label.
    """
    label = input_queue[1]
    file_contents = tf.read_file(input_queue[0])
    example = tf.image.decode_png(file_contents, channels=3)
    return example, label

# Reads pfathes of images together with their labels
image_list, label_list = read_labeled_image_list(filename)

images = ops.convert_to_tensor(image_list, dtype=dtypes.string)
labels = ops.convert_to_tensor(label_list, dtype=dtypes.int32)

# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
                                            num_epochs=num_epochs,
                                            shuffle=True)

image, label = read_images_from_disk(input_queue)

# Optional Preprocessing or Data Augmentation
# tf.image implements most of the standard image augmentation
image = preprocess_image(image)
label = preprocess_label(label)

# Optional Image and Label Batching
image_batch, label_batch = tf.train.batch([image, label],
                                          batch_size=batch_size)

See also the generic_input_producer from the TensorVision examples for full input-pipeline.

MarvMind
  • 3,366
  • 2
  • 21
  • 19
  • You seem to be passing `num_labels` to `read_images_from_disk`, which is not a parameter of this function. Where should I be actually passing this info? – ldavid May 22 '16 at 04:34
  • 1
    Sry, this is a mistake I did when producing the minimal example out of bigger code. I now removed `num_labels`. You do not need `num_labels` when reading from a file. If you know `num_labels` advance you can use it for checks (assert) and to produce `one hot labels`. The latter is not required anymore in many cases, since `tf.nn.sparse_softmax_cross_entropy_with_logits` allows to use integer labels directly. – MarvMind May 22 '16 at 17:23
  • 1
    My question is what is the difference between these approaches (`WholeFileReader ` vs `tf.read_file`) as far their performance and the queues that are created for buffered loading? – Alex Rothberg Aug 30 '16 at 13:29
  • No. I posted to ML as well. – Alex Rothberg Sep 06 '16 at 16:10
  • Will the producer ever get empty? What happens after one iteration? – Ray.R.Chua Feb 09 '17 at 16:45
  • I'm using TensorFlow 1.0 and had to change `ops.convert_to_tensor` to `tf.convert_to_tensor` and drop the dtypes argument. However I'm still getting an error at tf.train.batch `ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(None)]), TensorShape([])]`. Any ideas on what's going wrong? – user2962197 Feb 24 '17 at 11:03
  • Thanks for this - please fix the link https://github.com/TensorVision/TensorVision/blob/master/examples/inputs/generic_input.py – Mr_and_Mrs_D Apr 07 '17 at 21:31
  • I got the same error of All shapes must be fully defined, but the link suggested in the comments did not work (404 error) @Mr_and_Mrs_D – Mohanad Kaleia Apr 20 '17 at 18:12
  • 2
    I had it work by setting the shape of the images in the read_images_from_disk function: example.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS]) – Mohanad Kaleia Apr 20 '17 at 20:20
  • I tried to use your code, but it didn't match the labels with the images properly for some reason. Any ideas why? http://stackoverflow.com/questions/43567552/tf-slice-input-producer-not-keeping-tensors-in-sync – rasen58 Apr 23 '17 at 20:14
  • had exactly the same issue as @MohanadKaleia using TF v1.0, thanks for the fix! – galactica May 02 '17 at 20:19
22

There are three main steps to solving this problem:

  1. Populate the tf.train.string_input_producer() with a list of strings containing the original, space-delimited string containing the filename and the label.

  2. Use tf.read_file(filename) rather than tf.WholeFileReader() to read your image files. tf.read_file() is a stateless op that consumes a single filename and produces a single string containing the contents of the file. It has the advantage that it's a pure function, so it's easy to associate data with the input and the output. For example, your read_my_file_format function would become:

    def read_my_file_format(filename_and_label_tensor):
      """Consumes a single filename and label as a ' '-delimited string.
    
      Args:
        filename_and_label_tensor: A scalar string tensor.
    
      Returns:
        Two tensors: the decoded image, and the string label.
      """
      filename, label = tf.decode_csv(filename_and_label_tensor, [[""], [""]], " ")
      file_contents = tf.read_file(filename)
      example = tf.image.decode_png(file_contents)
      return example, label
    
  3. Invoke the new version of read_my_file_format by passing a single dequeued element from the input_queue:

    image, label = read_my_file_format(input_queue.dequeue())         
    

You can then use the image and label tensors in the remainder of your model.

mrry
  • 125,488
  • 26
  • 399
  • 400
  • Hi, if the files are `.mat` format that need to first load using `h5py`, then using `convert_to_tensor`, how to parse the `filename_and_label_tensor` into a numpy string to using `h5py.Open()`? Currently tensorflow seems no `decode_mat` support. Not sure why tensorflow needs to take the string as a `Tensor`. – mining Jul 21 '17 at 18:18
2

In addition to the answers provided there are few other things you can do:

Encode your label into the filename. If you have N different categories you can rename your files to something like: 0_file001, 5_file002, N_file003. Afterwards when you read the data from a reader key, value = reader.read(filename_queue) your key/value are:

The output of Read will be a filename (key) and the contents of that file (value)

Then parse your filename, extract the label and convert it to int. This will require a little bit of preprocessing of the data.

Use TFRecords which will allow you to store the data and labels at the same file.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753