In this era of AI, data plays a crucial role. Companies spend millions of dollars to collect proprietary datasets as open source datasets are very limited. One way to deal with the shortage of data is to use data augmentation.
Data augmentation is the process of artificially transforming the data to increase it. In data augmentation, the transformations are applied to the input data and then tested on the actual test data. It is the practitioner's responsibility to ensure that no such transformations are performed that may transform the data into something that we are not expecting to see in the test dataset.
Data augmentation is most commonly used in the following types of datasets.
It is very common to apply shifting and rotations when working with image-based datasets. Sometimes, images are very hard to collect. We can use some transformations to help create images that we expect to see in the testing stage. Some transformations are:
Note: Augmenting images may not be helpful in some situations. In fact, these transformations can prove to be disastrous. For example, if we are building a handwritten text classifier, we would not apply flipping.
It is a common practice to add noise to signals so we can make the model robust. For example, noise is added to audio signals so the classification model would be more robust. This way, the model trained on this dataset will be more robust to classification. Other common transformations include shifting the signal and changing the speed of the signal.
Generative Adversarial Networks (GANs) have also been used to augment signal-based datasets. It synthesizes new data which is close to the original input datasets.
One of the most important transformation techniques is back translation. In this technique, we take a sentence in a language and then use translation tools like google translate to get the translation in a different language. Then we again translate this text back into the first language which gives a different text than the original one.
Other transformations include word replacement, in which we replace some random words with their synonyms.
In cases when training multiple times is not an issue, we can try out different types of data augmentation techniques and find out which one works the best. We can also use multiple data augmentation techniques in combination. This technique of optimizing using different augmentation techniques is called AutoAugmentation.
Here, we use ImageDataGenerator
to augment an image.
import numpy as npimport matplotlib.pyplot as pltfrom tensorflow.keras.utils import load_img, img_to_arrayfrom tensorflow.keras.preprocessing.image import ImageDataGenerator# loading image to use.img = load_img('Detective.png')data = img_to_array(img)samples = np.expand_dims(data, 0)# defining the transformations that we needdata_gen = ImageDataGenerator(rotation_range=20,horizontal_flip=True,vertical_flip=True)it = data_gen.flow(samples, batch_size=1)fig, ax = plt.subplots(2, 2)for i in range(2):for j in range(2):batch = it.next()image = batch[0].astype('uint8')ax[i, j].imshow(image)ax[i, j].axis('off')plt.show()
This code will generate an output like the following:
ImageDataGenerator
takes multiple images at once, we need to input an array with multiple images. Hence, we use the np.expand_dims()
function to do the task.ImageDataGenerator
is supposed to do. We want it to randomly rotate images up to 20 degrees and flip them vertically and horizontally. .next()
on it.