In machine learning and deep learning, having more data is very important to help you get good performance from the model. You can create more data using a technique called data enhancement. Data enhancement is a technique that practitioners use to add data by creating modified data from existing data.
If your project dataset is small, or if you want to reduce overfitting in ML or deep learning (DL) models, it is a good practice to use data enhancement techniques.
In this article, you will learn how to perform data enhancement using a new open source library called Augly from Facebook.
1. What is Augly?
AugLy is a data enhancement library that can help you evaluate and improve the robustness of your model. The library supports four modes (audio, video, image and text) and contains more than 100 methods to perform data enhancement.
If you are working on a machine learning or deep learning project that uses audio, video, image, or text datasets, you can use this library to increase data and improve model performance.
The library was developed by Joanna Bitton, a software engineer at Facebook AI, Zoe Papakipos, a research engineer at FAIR, and other researchers and engineers at Facebook.
The library has been used for different projects, such as:
- Image similarity challenge: NeurIPS 2021 competition organized by Facebook AI, with a prize of $200000. It has generated the DISC21 dataset, which will be publicly released after the challenge!
- DeepFake detection challenge: Kaggle competition organized by Facebook AI in 2020, with a bonus of US $1 million; DFDC datasets are also generated.
- Simsearchnet: an approximate duplicate detection model developed by Facebook AI to identify infringing content on the platform.
2. How to install Augly?
AugLy is a Python 3.6 + library. It can be installed in the following ways:
pip install augly
Note: the above command only installs the basic requirements for using image and text modes. For audio and video modes, you can install additional dependencies as required
pip install augly[av]
In some environments, pip does not install Python magic as expected. In this case, you need to run another:
conda install -c conda-forge python-magic
3. Data enhancement technology of text data
import augly.text as textaugs
Then create a simple text entry.
# Define input text input_text = "Hello, world! Today we learn Data Augmentation techniques"
Now we can apply various enhancements as follows:
3.1 simulated typos
Use spelling errors, keyboard distance, and switching techniques to simulate spelling errors in each text.
print(textaugs.simulate_typos(input_text)) # Hello, world! Today ew leanr Dtaa Augmentation techniques
As you can see, this technique adds some spelling errors and exchanges some text words.
3.2 inserting punctuation marks
You can insert punctuation marks in each input text.
print(textaugs.insert_punctuation_chars(input_text)) # ['H,e,l,l,o,,, ,w,o,r,l,d,!,,T,o,d,a,y, ,w,e, ,l,e,a, r,n, ,D,a,t,a, ,A,u,g,m,e,n,t,a,t,i,o,n, ,t,e,c,h,n,i ,q,u,e,s']
3.3 two way replacement
This technique inverts each word (or part of a word) in each input text and renders the text in the original order using two-way markers. It reverses each word individually and maintains word order even if the line breaks.
print(textaugs.replace_bidirectional(input_text)) # ['\u202eseuqinhcet noitatnemguA ataD nrael ew yadoT !dlrow ,olleH\u202c']
3.4 replacing similar characters
This replaces the letters in each text with similar characters.
print(textaugs.replace_similar_chars(input_text)) # Hello, wor7d! T()day we learn Data Augm3^tatin techniques
As you can see, the character "l" has been replaced with the number 7, the character "o" has been replaced with "()", the character "e" has been replaced with the number 3, and then the character "o" has been replaced with "".
3.5 inverted replacement
This reverses the words in the text according to granularity.
print(textaugs.replace_upside_down(input_text)) # sǝnbᴉuɥɔǝʇ uoᴉʇɐʇuǝɯɓnⱯ ɐʇɐᗡ uɹɐǝl ǝʍ ʎɐpoꞱ ¡plɹoʍ 'ollǝH
3.6 word segmentation
This function splits a word in text into child words.
print(textaugs.split_words(input_text)) # He llo, world! To day we learn Data Augmentation techniques
4. Data enhancement technology of image data
The first step is to import the image mode and its dependencies, including the enhancement technology of image data.
import os import augly.image as imaugs import augly.utils as utils from IPython.display import display
Now we can apply various enhancements as follows:
4.1 image scaling
The zoom function can help you change the resolution of the image. You can use a parameter called factor to define the scale at which the image is reduced or enlarged.
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)
4.2 blurred image
In this function, the larger the radius, the more blurred the image.
input_img = imaugs.blur(input_img, radius=5.0) display(input_img)
4.3 changing the brightness of the image
To change the brightness, you need to adjust the factor parameter in this function. A value less than 1.0 darkens the image, and a value greater than 1.0 brightens the image. Setting the factor to 1.0 does not change the brightness of the image.
Let's set the value of the factor to 1.5.
input_img = imaugs.brightness(input_img,factor=1.5) display(input_img)
Then let's set the value of the factor to 0.5 to make it darker.
#make it darker input_img = imaugs.brightness(input_img,factor=0.5) display(input_img)
4.4 changing the aspect ratio of the image
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)
4.5 changing the contrast of the image
In this function, the factor parameter handles everything. When you set the factor to zero, it will give a gray image, a value lower than 1.0 will reduce the contrast, a coefficient of 1.0 will give the original image, and a coefficient greater than 1.0 will increase the contrast.
input_img = imaugs.contrast(input_img,factor=1.7) display(input_img)
4.6 crop image
To crop an image, you need to define the positions of the upper left, upper right, and lower edges of the cropped image.
input_img = imaugs.crop(input_img, x1=0.25, x2=0.75, y1=0.25, y2=0.75 ) display(input_img)
As I explained earlier, the library has more than 100 enhancements, most of which are not covered in this article.