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!
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With