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, ...

Get Deep Learning Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.