visit
In machine learning and deep learning, having more data is very important to help you get good performance from your models. You can create more data by using a technique called data augmentation. Data augmentation is a technique used by practitioners to increase the data by creating modified data from the existing data.
“We don’t have better algorithms. We just have more data.”-It is a good practice to use data augmentation techniques if you have a small dataset for your project or you want to reduce overfitting in your ML or deep learning (DL) models.
In this article, you will learn how to perform data augmentation by using a new open-source library from Facebook called Augly.
pip install augly
Note: The above command installs only base requirements to use the image and text modalities. For audio and video modalities, you can install the extra dependencies required with
pip install augly[av]
conda install -c conda-forge python-magic
The first step is to import text modality which contains augmentation techniques for text data.
import augly.text as textaugs
# Define input text
input_text = "Hello, world! Today we learn Data Augmentation techniques"
print(textaugs.simulate_typos(input_text))
print(textaugs.insert_punctuation_chars(input_text))
print(textaugs.replace_bidirectional(input_text))
print(textaugs.replace_similar_chars(input_text))
As you can see the character “l” has been replaced with number 7, character “o” has been replaced with “()”, character “e” has been replaced with number 3 and then the character “o” has been replaced with “[]”.
print(textaugs.replace_upside_down(input_text))
print(textaugs.split_words(input_text))
The first step is to import image modality with its dependencies which contain augmentation techniques for image data.
import os
import augly.image as imaugs
import augly.utils as utils
from IPython.display import display
The scale function can help you to alter the resolution of an image. You can use an argument called factor to define the ratio by which the image should be downscaled or upscaled.
input_img_path = "images/simple-image.jpg"
# We can use the AugLy scale augmentation
input_img = imaugs.scale(input_img_path, factor=0.2)
display(input_img)
input_img = imaugs.blur(input_img, radius=5.0)
display(input_img)
To change the brightness you need to adjust the factor argument in this function. Values less than 1.0 darken the image and values greater than 1.0 brighten the image. Setting the factor to 1.0 will not alter the image's brightness.
Let's set factor's value be 1.5.input_img = imaugs.brightness(input_img,factor=1.5)
display(input_img)
Then let’s set the factor's value to 0.5 to make it darker.
#make it darker
input_img = imaugs.brightness(input_img,factor=0.5)
display(input_img)
In this function, the aspect ratio is the width/height of the new image you want to create.
input_img = imaugs.change_aspect_ratio(input_img, ratio=0.8)
display(input_img)
In this function the factor argument handle everything, When you set the factor to zero, it gives a grayscale image, values below 1.0 decreases contrast,
A factor of 1.0 gives the original image, and a factor greater than 1.0 increases the contrast.input_img = imaugs.contrast(input_img,factor=1.7)
display(input_img)
input_img = imaugs.crop(input_img,
x1=0.25,
x2=0.75,
y1=0.25,
y2=0.75
)
display(input_img
As I have explained before, the library has over 100 augmentation techniques and most of them were not covered in this article.
If you want to learn how to perform data augmentation for audio and video data, please read in the README for each modality!
And you can read more articles like this here.
Want to keep up to date with all the latest in python? Subscribe to our newsletter in the footer below