Data augmentation refers to randomly applying various kinds of transforms to the images in our dataset. These transforms help introduce more variety in our dataset. Let’s see what that means.
Consider an example of building a face lock. When you take samples for the lock you take some photos in the present and you are done. However, the user is not always going to be in the same lighting when he wants to unlock his phone (Brightness). Or, the user won’t always be facing the camera at the same angle (Warp). Maybe his picture appears blur because of some reason. We need to account for these situations while training our data. And that’s exactly what data augmentation gives us.
Trending AI Articles:
1. Machines Demonstrate Self-Awareness
2. Bursting the Jargon bubbles — Deep Learning
3. How Can We Improve the Quality of Our Data?
4. Machine Learning using Logistic Regression in Python with Code
We start with an image like this
And we end up with a bunch of pictures that look like these
What we do is, instead of feeding the model with the same pictures every time, we do small random transformations (a bit of rotation, zoom, translation, etc…) that doesn’t change what’s inside the image (for the human eye) but changes its pixel values. Models trained with data augmentation will then generalize better. If you want to test this, you can take any dataset and train it with and without transforms.
Data augmentation is also useful in situations when we have a relatively small number of training samples.
Full jupyter notebook.
In this short article, we won’t be focusing much on the code that achieves these transformations. Instead, we will visualize the various transformations available to understand what happens behind the scenes when we perform data augmentation.
We start by looking at the defaults in the fastai library. These defaults are set in such a way that they would work well for a majority of tasks.
We can however change a few of them (for example
do_flip ) based on the nature of our application.
get transforms returns a tuple of two list of transforms: one for the training set and one for the validation set. However, we don’t want to modify the pictures in our validation set, so the second lists is limited to resizing the pictures.
We use a little helper function to plot our grids. The function is as shown below.
**kwargs means we can pass any number of arguments (and we don’t know the number in advance). Some useful arguments to pass can be
size so that all images are of the same size, and
padding to decide what to do with missing pixels in our images.
Now that we’re all set, let us take a look at some of the cool transformations we can do.
Random zoom / crop
Randomly crop or zoom the image.
The randomness that we introduce in the transformations can be done in 2 ways.
In the first way, we can pass the probability that an image will be transformed. Based on that probability, the image will be transformed or not transformed. In the next transformation, we rotate the image by 30 degrees with a 50% probability.
In the second way, instead of passing the probability, we pass a range of values (here degrees). The image will then be rotated at a random degree chosen between the 2 floats.
We can also combine the 2 methods mentioned above!
This transform takes an argument called
change to 0 will convert our image to black and setting it to 1 will turn it white
Similar to brightness, contrast can be set using a
This transform applies combines a flip (horizontal or vertical) and a rotation of a multiple of 90 degrees.
Horizontally flip the image.
This transform changes the pixels of the image by randomly replacing them with pixels from the neighborhood thus causing jitter.
That would be it for this article. If you want to find out more about these transformations, you can do so by reading the fastai documentation.
If you liked this article give it atleast 50 claps :p