Data augmentation : boost your image dataset with few lines of Python
To perform well, an image classifier needs a lot of images to train on. Deep learning algorithms can fail to classify let’s say cats, only because some cats are oriented differently on your test images.
It can be hard to find an exhaustive dataset of cats of all kinds, in all possible positions (for example looking to the right, to the left etc). The images you are about to classify can also present some distortions like noise, blur or a slight rotations.
Data augmentation is an automatic way to boost the number of different images you will use to train your Deep learning algorithms. After this quick guide you will get a thousand-images dataset from only a few images.
This article will present the approach I use for this open source project I am working on :
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).
If you decide to generate a few thousand of images and want to use it directly to train a deep network, you may want to keep it in memory to save disk space (if you have enough memory). It’s easy as a lot of deep learning frameworks use scipy.ndarray objects to feed their networks.
Conclusion
With this data augmentation script you can now generate 1000 new images. Of course you can add other transformations or adjust the probability that some transformations happen. For example, we may want that rotations occur more often than adding noise. Everything is possible !
Here is the full version of the code we worked on.