简体   繁体   中英

How to stack channels in Tensorflow input pipeline?

I started working with tf a few weeks ago and am struggling with the input queue right now. What I want to do is the following: I have a folder with 477 temporal, greyscale images. Now I want eg take the first 3 images and stack them together (=> 600,600,3), so that I get a single example with 3 channels. Next I want to take the fourth image and use it as a label (just 1 channel => 600,600,1). Then I want to pass both to tf.train.batch and create batches.

I think I found a solution, see code below. But I was wondering if there is a more fashionable solution.

My actual question is: What happens at the end of the queue. Since I'm always picking 4 images from the queue (3 for input, 1 for label) and I have 477 images in my queue, things are not working out. Does tf then just fill up my queue again and continues (so if there is 1 image left in the queue, it takes this image, fills up the queue again and take 2 more images to get the desired 3 images?). Or do I need a number of images divisible by 4 in my folder if I want a proper solution?

def read_image(filename_queue):
  reader = tf.WholeFileReader()
  _, value = reader.read(filename_queue)
  image = tf.image.decode_png(value, dtype=tf.uint8)
  image = tf.cast(image, tf.float32)
  image = tf.image.resize_images(image, [600, 600])
  return image

def input_pipeline(file_names, batch_size, num_epochs=None):

  filename_queue = tf.train.string_input_producer(file_names, num_epochs=num_epochs, shuffle=False)
  image1 = read_image(filename_queue)
  image2 = read_image(filename_queue)
  image3 = read_image(filename_queue)
  image = tf.concat([image1, image2, image3,], axis=2)
  label = read.image(filename_queue)

  # Reshape is necessary, otherwise I get an error..
  image = tf.reshape(image, [600, 600, 3])
  label = tf.reshape(label, [600, 600, 1])

  min_after_dequeue = 200
  capacity = min_after_dequeue + 12 * batch_size
  image_batch, label_batch = tf.train.batch([image, label],
                             batch_size=batch_size,
                             num_threads=12,
                             capacity=capacity)
  return image_batch, label_batch

Thanks for any help!

But I was wondering if there is a more fashionable solution

Yes! there's a better and faster solution. First you redesign your database, since you want to combine 3 gray images into 1 rgb images for training; prepare a dataset of RGB images from gray images (it will save whole lot of time during training).

redesign the way you retrieve data

  # retrieve image and corresponding label at the same time 
  # here if you set the num_epochs=None, the queue will run continuously; and it will take-care of the data need for training till end
  filename_queue = tf.train.string_input_producer([file_names_images_list, corresponding_file_names_label_list], num_epochs=None, shuffle=False)

  image = read_image(filename_queue[0])
  label = read_image(filename_queue[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM