Custom Preprocessing Function with ImageDataGenerator

In this code snippet, I will show you, how to write a custom preprocessing function to use with ImageDataGenerator to extract image patches, random selecting from predefined sizes.

Here we go!

Note that, for every image, we get either the complete image or a patch of it. The relative proportion of cases can be controlled by the parameter p. Also note that all patches are of square shape but that can be changed easily. So I leave that to you.

Thats it!