buzjwa
buzjwa

Reputation: 2652

tf.image.random_flip_left_right() for multiple images together

I want to randomly flip several input images of different sizes in my input pipeline. The flip needs to be consistent for the different images - either all are flipped or none. Since using tf.image.random_flip_left_right() is inadequate for this, I did this instead:

images = [img1, img2]
def fliplr(*args):
    return [tf.image.flip_left_right(t) for t in args]
def id(*args):
    return args
img1, img2 = tf.cond(tf.random_uniform([1]) > 0.5, fliplr(images), id(images), name='fliplr')

This is part of a function called by tf.Dataset.map() with my input dataset object.

However I am getting this error from the flip_left_right line:

ValueError: Dimension 1 in both shapes must be equal, but are 3 and 1. Shapes are [240,320,3] and [240,320,1].

From merging shape 0 with other shapes. for 'flip_left_right/image' (op: 'Pack') with input shapes: [240,320,3], [240,320,1].

I think this is because my images are of different sizes (240x320, either 1 or 3 channels) but I don't understand why this would matter.

How can I resolve this error? Alternatively, is there a better way to get what I want that avoids this error?

I am using TensorFlow-1.8 (but can upgrade if required).

Upvotes: 4

Views: 2761

Answers (1)

Albert
Albert

Reputation: 68240

Just like this?

do_flip = tf.random_uniform([]) > 0.5
img1 = tf.cond(do_flip, lambda: tf.image.flip_left_right(img1), lambda: img1)
img2 = tf.cond(do_flip, lambda: tf.image.flip_left_right(img2), lambda: img2)

Upvotes: 6

Related Questions