Search code examples
tensorflowkerasdeep-learningconv-neural-networkdata-augmentation

Tensorflow Keras RandomFlip is applying the same flip to all images


version: tensorflow-gpu 2.10.0

I have created a data augmentation layer that is applied during pre-processing. In addition to flips, it involves random changes in contrast, rotation and brightness. I use the following code to test the augmentation:

#Defining data augmentation Keras layer
data_augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip("horizontal_and_vertical"),
    tf.keras.layers.RandomBrightness(0.25, seed=10),
    tf.keras.layers.RandomContrast(0.5, seed=20),
    tf.keras.layers.RandomRotation(0.028, fill_mode="constant", seed=35),
])

#Augmenting 1 image 9 times for testing
image = train_ds.take(1)
images = image.repeat(9)
images = images.map(lambda x,y: (data_augmentation(x, training=True),y),num_parallel_calls=AUTOTUNE)
n=0
for i,l in images:
    i=tf.cast(i, tf.uint8)
    ax = plt.subplot(3, 3, n + 1)
    _ = plt.imshow(i, cmap="gray",vmin=0,vmax=255)
    plt.axis("off")
    n+=1

The random changes in contrast, brightness and rotation act how I want: each of the 9 images has a different contrast, brightness and rotation. The seed allows me to reproduce these results (although funnily enough, the subplots will be ordered differently). I have used seeds so that I can recreate the same dataset. However, RandomFlip applies the exact same flip to all images. What's more, even with a seed, a different flip is applied each time I re-run the code. It seems to me that RandomFlip is not behaving like it should because it isn't applying a random flip to each image?

I have tried varying the seed and the position of RandomFlip in the data augmentation layer. I was hoping it was just the particular seed, but it appears to happen for other seeds. I have also tried with and without the following at the start of my code:

tf.random.set_seed(12)
np.random.seed(0)

With no effect.


Solution

  • After finding a similar problem on Github I have reverted my Tensorflow version to tensorflow-gpu 2.9.2. It seems this is a bug with Tensorflow 2.10 and the Github user verified RandomFlip behaves correctly on version 2.9.2. This is important to note for any other users like myself who are using the RandomFlip module and want to utilise your own GPU, as versions above 2.10 do not provide GPU support on native Windows.