Ashwani K
Ashwani K

Reputation: 7990

How do I generate more augmented images than training size in Keras?

I am aware of data augmentation techniques for images. However, I have a doubt related to how to generate augmented data.

I will share my scenario and code using Keras and Tensorflow however the concept is the same for any library.

Image augmentation technique for the below scenario:

In Keras, we have ImageDataGenerator which augments the image while the data is being pulled for training. For example, if we have 100 images to start with. Then ImageDataGenerator will apply the above mention transformations on these images and will output 100 images per epoch (Default ImageDataGenerator).

However, if we implement our own ImageDataGenerator, we can also implement it to return 100 + 2*100 = 300 images for the training.

Which of the above-mentioned scenario will be good in general?

Upvotes: 0

Views: 1389

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

You will get 300 unique pictures if you use the same 100 pictures, with augmentation, for three epochs. Every time a picture is loaded, Keras is applying a random transformation.

Look below. This is 16 repeats of a dataset of one image. The same image is never transformed the same way.

import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.datasets import load_sample_image
import numpy as np

imgs = np.stack([load_sample_image('flower.jpg') for i in range(4*4)], axis=0)

data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range = 90,
    width_shift_range = 0.1,
    height_shift_range = 0.1,
    horizontal_flip = True,
    preprocessing_function=lambda x: x[..., np.random.permutation([0, 1, 2])]
)

fig = plt.figure(figsize=(4, 4))
for index, image in enumerate(next(data_gen.flow(imgs)).astype(int)):
    ax = plt.subplot(4, 4, index + 1)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.imshow(image)
plt.show()

enter image description here

Upvotes: 3

Related Questions