Tensorflow read images with labels

tensorflow read images from directory
tensorflow image preprocessing
tensorflow load images from directory
tensorflow image classification
tf.read_file image
tensorflow dataset tutorial
datasetv1adapter
preparing image dataset for tensorflow

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?

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.

Tensorflow read images with labels, 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  tensorflow / tensorflow / examples / label_image / label_image.py / Jump to Code definitions load_graph Function read_tensor_from_image_file Function load_labels Function

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.

Tensorflow read images with labels - Article, It has not been explained in the Tensorflow documentation how to load images and labels directly from a TXT file. The code below illustrates how I achieved it.   Starting with images stored in HyperStore object storage, we use a TensorFlow machine learning model to identify what’s depicted in the image, then attach those labels to each image as S3 metadata, and finally automatically index and search the object metadata using ElasticSearch and Kibana.

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.

tensorflow, Because the predicted output is a number, it should be converted as string so human can read. Code 2. label names. Explore the Data. You can  We need to manually create labels for all the images in our dataset when working with object detection problems. To solve this problem, we propose an automatic way for annotating the image by a

CIFAR-10 Image Classification in TensorFlow, 2 — Convert file path to (image, label) pair. To do that, we will define 3 functions. i​) Get labels from a file_path: get_label() function. Write a short pure-tensorflow function that converts a file path to an (img, label) pair: def get_label(file_path): # convert the path to a list of path components parts = tf.strings.split(file_path, os.path.sep) # The second to last is the class-directory return parts[-2] == CLASS_NAMES

[TensorFlow 2.0] Load Images to tensorflow - A Ydobon, TensorFlow Tutorial and Examples for Beginners (support TF v1 & v2) Read dataset file image, label = tf.train.slice_input_producer([imagepaths, labels],. filenames_list and labels_list are properly loaded, the shapes of tensors (x, y, y_, W and b) are correct; TensorFlow graph is properly constructed and visible in TensorBoard. No idea what else should I check. It seems that I don't understand something about Queues in TensorFlow, but I do not know what exactly. Thanks in advance for any help!

TensorFlow-Examples/build_an_image_dataset.py at master , Reading the dataset # 2 modes: 'file' or 'folder' def read_images(dataset_path, mode, batch_size): imagepaths, labels = list(), list() if mode == 'file': # Read  This tutorial provides a simple example of how to load an image dataset using tf.data.. The dataset used in this example is distributed as directories of images, with one class of image per directory.

Comments
  • 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 ?
  • 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.
  • 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?
  • 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.
  • 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?
  • @AlexRothberg did you find an answer to your question?
  • 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])
  • 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.