Reputation: 41
I use tensorflow and fully convolutional network to do regression on image. I want to augment data by random flip/rotation, but how can I apply them simultaneously to input and output image tensor?
Upvotes: 1
Views: 978
Reputation: 24641
You're right, functions like tf.random_flip_left_right
can be used only if there are no covariant variables. If there are, for example if your output is an image of labels, then you need to apply the same random operation to all of them.
I see two ways to do it:
You basically rewrite the random operations to work with several tensors. For example, to replace tf.random_flip_left_right
, you could use
coin = tf.less(tf.random_uniform((), 0., 1.), 0.5)
im1 = tf.cond(coin, lambda: tf.flip_left_right(im1), lambda: im1)
im2 = tf.cond(coin, lambda: tf.flip_left_right(im2), lambda: im2)
...
You stack all your tensors together before the random op:
all_im = tf.stack([im1, im2,...], axis=2)
all_im = tf.random_flip_left_right(all_im)
[im1, im2, ...] = tf.split(all_im, [im1.shape[2], im2.shape[2], ...], axis=2)
This second solution works only if all of the tensors have the same spatial range.
Upvotes: 2