Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow image operations for batches

Tags:

There are a number of image operations in TensorFlow used for distorting input images during training, e.g. tf.image.random_flip_left_right(image, seed=None) and tf.image.random_brightness(image, max_delta, seed=None) and several others.

These functions are made for single images (i.e. 3-D tensors with shape [height, width, color-channel]). How can I make them work on a batch of images (i.e. 4-D tensors with shape [batch, height, width, color-channel])?

A working example would be greatly appreciated!

like image 222
questiondude Avatar asked Aug 12 '16 14:08

questiondude


2 Answers

One possibility is to use the recently added tf.map_fn() to apply the single-image operator to each element of the batch.

result = tf.map_fn(lambda img: tf.image.random_flip_left_right(img), images)

This effectively builds the same graph as keveman suggests building, but it can be more efficient for larger batch sizes, by using TensorFlow's support for loops.

like image 98
mrry Avatar answered Sep 22 '22 14:09

mrry


You can call the image operation in a loop and concatenate the result. For example :

transformed_images = []
for i in range(batch_size):
  transformed_images.append(tf.image.random_flip_left_right(image[i, :, :, :]))
retsult = tf.stack(transformed_images)
like image 31
keveman Avatar answered Sep 18 '22 14:09

keveman