# Are the image data augmentation generators in Keras randomly applied

I am working on an image classification problem and using data augmentation in Keras.

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
rotation_range=2,
horizontal_flip=True)


I would like to know if the ImageDataGeneratorapplies the transformation randomly to image patches. That is for example, rotation may be applied on one image, whilst flip may be applied to another image. I want to know if the decision to apply rotation or a flip is randomly determined.

I suggest having a look at the relevant documentation.

There, it states:

rotation_range: Int. Degree range for random rotations.

...

horizontal_flip: Boolean. Randomly flip inputs horizontally.

Saying each of the operations is applied randomly, I would say, means your images will be generated sometime with and sometimes without the augmentation steps, independently from one another.

If that doesn't convince you, here is the relevant snippet from the source code:

if self.horizontal_flip:
if np.random.random() < 0.5:
x = flip_axis(x, img_col_axis)


The code for the rotation step is a little more involved, but contained within the same class that I liked above.