Step by step — Data augmentation in Python

Even if some great solutions like Keras already provide a way to perform data augmentation, we will build our own Python script to demonstrate how data augmentation works.

Our script will pick some random images from an existing folder and apply transformations, like adding noise, rotating to the left or to the right, flipping the image horizontally etc. Now some code !

Step 1 — Image transformations

There are a lot of good Python libraries for image transformation like OpenCV or Pillow. We will focus on scikit-image, which is the easiest library to use from my point of view.

Let’s define a bunch of transformation functions for our data augmentation script.

Now we have three possible transformations for our images : random rotation, random noise and horizontal flip.

Note : we use scipy.ndarray to represent the image to transform. This data structure is convenient for computers, as it’s a two-dimensional array of image’s pixels (RGB colors). In fact, image processing or Deep learning often requires working with scipy.ndarray.

Step 2 — List all the files in a folder and read them

We decided to generate one thousand images based on our images/cats folder. So we perform one thousand iterations (line 13), then choose a random file from the folder (line 15) and read it with skimage.io.imread, which read images as a scipy.ndarray by default (line 17). Perfect, we have everything we need to transform images.

Step 3 — Images transformation

Again, some random magic here ! We choose the number of transformations for a single image (line 9) and the kind of transformations to apply (line 15). Then we just call the function defined in our transformations dictionary (line 16).

Step 4 — Save the new images

That’s it, we save our transformed scipy.ndarray as a .jpg file to the disk with the skimage.io.imsave function (line 5).