Data Augmentation — Imgaug Library

Buse Köseoğlu
4 min readMay 14, 2021

--

You can access the repo I prepared on the subject from my github account.

Data enhancement is to generate new data by changing the original data.

In order for a model to learn the data, enough data should be given to the model. Sometimes the data we have may not be sufficient for the model to learn and we may need to increase our data. In this case, the first thing that comes to mind may be to go wherever we have obtained the data and collect some more data. But this solution takes a lot of time and effort. Instead, we can enlarge the data set we have and bring it to a form that the model can learn with data augmentation techniques. The data we create with data augmentation techniques is called synthetic data.

In this article, we will examine the imgaug library used with data augmentation.

Before running the scripts below, you must install the necessary libraries.

Upload the Photo

Imgaug library does not have a function to read photo. So we will use the imageio library to import the photo. If you want, you can also use OpenCv. OpenCV reads photos in BGR format but supports imgaug RGB format. So don’t forget to convert BGR to RGB if you import with OpenCV.

Data Augmentation

Rotation

Rotation allows the photo to be turned left or right at a certain angle.

First we start by importing the necessary library. Then we give the value 4 to the ia.seed () function to get the same result each time. The value of the rotate parameter in iaa.Affine (-25.25) indicates that the photo will rotate left or right with a random value selected in this range. Then we apply the rotation process with rotate (image = img) to the photo we imported, and when we view the photo, we get the following result.

If we want, we can prevent the rotation from being random by giving a value such as rotate = 90. In addition, if we make a value as rotate = [25,60], one of the values ​​of 25 or 60 is selected for the rotation operation.

Applying Multiple Techniques Simultaneously

When using the imgaug library, we can apply more than one process at the same time.

Here, we can use many augmentation techniques simultaneously with the help of Sequential. In addition, we can apply the augmentation techniques to the photos simultaneously by putting the photos we uploaded into the list. Since we uploaded a photo here, we will go through this photo.

If we examine the code:

1. First of all, we create a variable named images. We put the same photo in a list 4 times and give it to this variable.

2. We list the techniques we want to the Sequential function in imgaug.augmenters.

*iaa.Affine (rotate = (- 20,30)): Select a random value in the range (-20,30) and rotate the photo

*iaa.AdditiveGaussianNoise (scale = (10.60)): Select a value in the range (10.60) and add gaussian noise to the photo according to this value

*iaa.Crop (percent = (0,0.2)): It means to choose a value between (0,0.2) and crop the photo according to this value percentage.

3. We apply the augmentation techniques we prepared with seq (images = images) to our photos.

4. Finally, we view the photos and get the following result.

While we only had one photo at the beginning of the process, now there are 4 different versions of the same photo. Adding noise to the photo, rotating it or cropping it are important processes here. This is because computers, like humans, cannot distinguish a crooked photo from a flat one. Therefore, the more different data we have, the more successful our model will be.

Working with Photographs of Different Sizes

imgaug library supports different size photos. So we don’t need to reshape the images. Let’s upload a photo of Tesla to see this.

The size of the photo we uploaded (872, 700, 3). He states that the 3 photos here are colored, in other words RGB.

First of all, we write the augmentation techniques we want to apply into Sequential. Then we put img (edison) and img2 (tesla) photos into the same list and apply the augmentation techniques we created with seq (images = images) to the photos. Then we view the first and final versions of the photos.

--

--