Tensorflow reads images using tags

I am creating a standard image classification model with Tensorflow. To do this, I have input images, each of which is assigned a label (a number in {0,1}). Therefore, the data can be saved in a list in 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 the TensorFlow queuing system to read my data and transfer it to my model. By ignoring tags, this can easily be achieved using string_input_producer and wholeFileReader . Here is 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, labels are lost in this process because image data is intentionally shuffled as part of the input pipeline. What is the easiest way to click tags along with image data through input queues?

+48
python tensorflow
Dec 17 '15 at 17:14
source share
3 answers

Using slice_input_producer provides a solution that is much cleaner. Slice Input Producer allows you to create an input queue containing arbitrarily many shared values. This snippet of the question will 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 TensorVision examples for a complete input pipeline.

+46
Apr 29 '16 at 21:20
source share

There are three main ways to solve this problem:

  • Fill in tf.train.string_input_producer() with a list of lines containing the source line with space-delimited lines containing the file name and label.

  • Use tf.read_file(filename) instead of tf.WholeFileReader() to read your image files. tf.read_file() is a stand-alone mode that uses a single file name and creates a single line containing the contents of the file. This has the advantage of being a pure function, so it is easy to associate data with input and output. For example, your read_my_file_format function will 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 
  • Call the new version of read_my_file_format , passing one of the selected items from input_queue :

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

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

+20
Dec 17 '15 at 22:54
source share

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

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

The result of reading will be the file name (key) and the contents of this file (value)

Then parse your file name, extract the shortcut and convert it to int. This will require a bit of pre-processing of the data.

Use TFRecords , which allows you to store data and tags in a single file.

0
Jul 03 '17 at 21:33
source share



All Articles