Data Augmentation With AugLy. All-in-one augmentation packages for… | by Ng Wai Foong | Apr, 2022

Picture taken from the AugLy’s Github repository

On the planet of machine studying, information augmentation is likely one of the most helpful strategies to reinforce the efficiency of ML fashions.

Knowledge augmentation serves to create artificial information by way of slight modification or transformation to the present information. This helps to:

  • improve the quantity of coaching and check information.
  • cut back over-fitting of your mannequin

As of June 17, 2021, Fb formally open-sourced its information augmentation library known as AugLy. The library can be utilized to enhance the robustness of machine studying fashions.

It at the moment helps the next modalities:

This text covers solely the textual content modality however be happy to experiment with the opposite modalities by yourself. With out additional ado, let’s proceed to the subsequent part and begin putting in AugLy.

It’s extremely advisable to put in it on Linux-based working system particularly if you will apply it to audio information. On the time of this writing, the set up course of just isn’t that easy for Home windows person. The set up step beneath has been examined on Home windows 10 working system operating on Python model 3.7.7.

AugLy

Earlier than that, just be sure you have created a brand new digital setting with Python model not less than 3.6 or above. Activate it and run the next command to put in AugLy:

pip set up augly[all]

In actual fact, you’ll be able to set up simply the dependencies for a single modality. For instance, run the next command to put in solely the audio sub-library:

pip set up augly

Python-magic

Run pip record to verify if python-magic is put in in your setting. If it isn’t put in, run the next command in case you are utilizing Linux:

apt-get set up python3-magic

Alternatively, you’ll be able to set up it as follows in case you are utilizing Conda:

conda set up -c conda-forge python-magic

For Home windows customers, you have to set up a further bundle of python-magic that comes with DLL as follows:

pip set up python-magic-bin

Matplotlib Repair (For Home windows Customers)

The early model of Augly (0.1.1) requires matplotlib==3.3.4 as a part of its dependencies. In case you encountered difficulty with:

module 'sip' has no attribute 'setapi'

Kindly downgrade your matplotlib model to three.2 as follows:

pip set up matplotlib==3.2

You’ll be able to safely ignore the incompatibilities warning.

Create a brand new Python file known as test_augmenter.py in your working listing.

Import

Add the next import assertion on the high of the file:

import augly.textual content as textaugs

Subsequent, outline the enter textual content. You’ll be able to outline it as a single string or an inventory of strings:

texts = ["Who are you and what are you doing here?", "Hello, world! Welcome to Speakr!"]

There are two methods to perform augmentations:

  • Class-based
  • Perform-based

Let’s take a look at an instance to insert punctuation into the enter texts.

# instantiate the augmenter
remodel = textaugs.InsertPunctuationChars(granularity="all", cadence=5.0, vary_chars=True)
# carry out transformation on enter textual content
aug_texts = remodel(texts)

Merely instantiate the augmenter class and cross the enter texts as enter parameters.

The identical course of could be carried out utilizing the next function-based augmentation:

# carry out transformation on the enter textual content
aug_texts = textaugs.insert_punctuation_chars(texts, granularity="all", cadence=5.0, vary_chars=True)

It’s best to get the next output in your console once you run the Python file (the result’s completely different on every run):

['Who a.re yo?u and, what: are ,you d:oing ?here?', "Hello', wor!ld! W-elcom.e to ...Speak.r!"]

Examine the following code on the official repository for extra info on the out there enter arguments.

AugLy helps the next text augmentations:

  • insert_punctuation_chars: Inserts punctuation characters in every enter textual content.
  • insert_zero_width_chars: Inserts zero-width characters in every enter textual content.
  • replace_bidirectional: Reverses every phrase (or a part of the phrase) in every enter textual content and makes use of bidirectional marks to render the textual content in its authentic order. It reverses every phrase individually which retains the phrase order even when a line wraps.
  • replace_fun_fonts: Replaces phrases or characters relying on the granularity with enjoyable fonts utilized.
  • replace_similar_chars: Replaces letters in every textual content with comparable characters.
  • replace_similar_unicode_chars: Replaces letters in every textual content with comparable unicodes.
  • replace_upside_down: Flips phrases within the textual content the other way up relying on the granularity.
  • simulate_typos: Simulates typos in every textual content utilizing misspellings, keyboard distance, and swapping.
  • split_words: Splits phrases within the textual content into subwords.

Amongst all of the out there augmenters, simulate_typos is likely one of the most helpful capabilities to generate artificial information meant for chatbot or textual content classification.

Let’s run a easy check on this augmenter utilizing the next code:

for i in vary(5):
aug_texts = textaugs.simulate_typos(texts)
print(aug_texts)

The output must be as follows:

['Who are you and whaaat arf ytou doing here?', 'Hello, worls! Welcome to Zpeakr!']
['Who aer you andd waht are you doing here?', 'Hello, worls! Welcome to Speark!']
['Who aee you adn waht are you doing here?', 'Hello, worls! Eelcome to Speakr!']
['Who arf ytou anbd what are you doing here?', 'Hello, worls! Welcome ot Speakr!']
['Who are you anbd what are yuo doing htere?', 'Hello, worls! Welcoem to Speakr!']

More Posts