user17651088
user17651088

Reputation: 126

Unable to perform Data Augmentation on images in Tensorflow

I have the below code but it does not do the data augmentation. I just simply print the same image as it is without making transformation on the image.

data_augmentation =Sequential()
data_augmentation.add(layers.RandomFlip("horizontal_and_vertical",input_shape=(img_height,img_width,3)))
data_augmentation.add(layers.RandomRotation(0.2,fill_mode='wrap'))
data_augmentation.add(layers.RandomZoom(height_factor=(0.2, 0.3), width_factor=(0.2, 0.3), fill_mode='reflect')) 


plt.figure(figsize=[15,11])
for image, label in train_ds.take(1):
    for i in range(9):
        augmented_images = data_augmentation(image)
        plt.subplot(3, 3, i + 1, xticks=[],yticks=[])
        plt.imshow(augmented_images[0].numpy().astype("uint8"))        
plt.show()

Can someone please suggest what am I not doing right.

Upvotes: 1

Views: 1203

Answers (2)

Jim Ögren
Jim Ögren

Reputation: 31

I had the same issue. The preprocessing layers in keras are only applied during training and not during inference. But you can apply them also during inference by passing training=True:

augmented_image = data_augmentation(image, training=True)

Modifying Loris's answer above, here is a working example:

import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt

ds = tfds.load('mnist',
               split='test')

image = next(iter(ds))['image']
plt.imshow(image)
plt.show()


data_augmentation = tf.keras.Sequential()
data_augmentation.add(tf.keras.layers.RandomFlip("horizontal_and_vertical"))
data_augmentation.add(tf.keras.layers.RandomRotation(0.2))
data_augmentation.add(tf.keras.layers.RandomZoom(height_factor=(.05),
                                                 width_factor=(.05)))

fig, axs = plt.subplots(1, 4, figsize=(20, 5))
for ax in axs:
    augmented_image = data_augmentation(image, training=True)
    ax.imshow(augmented_image)
    ax.axis("off")
plt.show()

Upvotes: 1

Loris Pilotto
Loris Pilotto

Reputation: 260

I made a reproducible example that works:

Load image:

import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt

ds = tfds.load('mnist',
               split='test')

image = next(iter(ds))['image']
plt.imshow(image)
plt.show()

Then for data augmentation:

data_augmentation = tf.keras.Sequential()
data_augmentation.add(tf.keras.layers.RandomFlip("horizontal_and_vertical"))
data_augmentation.add(tf.keras.layers.RandomRotation(0.2))
data_augmentation.add(tf.keras.layers.RandomZoom(height_factor=(.05),
                                                 width_factor=(.05)))

fig, axs = plt.subplots(1, 4, figsize=(20, 5))
for ax in axs:
    augmented_image = data_augmentation(image)
    ax.imshow(augmented_image)
    plt.axis("off")
plt.show()

Upvotes: 0

Related Questions