The Keras ImageDataGenerator

Not so long ago, the only way to do image augmentation was to code up the transforms and apply them randomly to the training set, saving the transformed images to disk as we went (uphill, both ways, in the snow). Luckily for us, Keras now provides an ImageDataGenerator class that can apply transformations on the fly as we train, without having to hand code the transformations.

We can create a data generator object from ImageDataGenerator by instantiating it like this:

def create_datagen(train_X):
data_generator = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.02,
height_shift_range=0.02,
horizontal_flip=True)
data_generator.fit(train_X)
return data_generator

In this example I'm using both shifts, rotation, and horizontal flips. I'm using only very small shifts. Through experimentation, I found that larger shifts were too much and my network wasn't actually able to learn anything. Your experience will vary as your problem does, but I would expect larger images to be more tolerant of shifting. In this case, we're using 32 pixel images, which are quite small.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.254.44