Data Augmentation with Fastai Library

Fred Malack
unpack
Published in
2 min readMar 15, 2021

Data Augmentation

In AI and Machine Learning the prediction accuracy of the Deep Learning (Supervised) models is largely reliant on the amount and the diversity of data available during training. In simple terms, the amount of data required is proportional to the number of learnable parameters in the model. Data augmentation is a technique that can be used to artificially expand the size of a training dataset by creating modified versions of images in the dataset. Examples of common data augmentation techniques for images are rotation, flipping, perspective warping, brightness changes and contrast changes.

Data Augmentation using Fastai

Fastai has a method get_transforms() which applies default and random transformations with a probability of 75%: crop, horizontal flip, zoom up to 1.1, brightness and contrast, wrap (perspective).

Different Transform supported by Fastai

Brightness, contrast, crop, crop_pad, dihedral, dihedral_affine, flip_lr,skew, squish, zoom etc.

item_tfms and batch_tfms in Fastai

Item_tfms — Item transforms are pieces of code that run on each individual item, whether it be an image, category, or so forth. fastai includes many predefined transforms. one or several transforms applied to the items before batching them.

E.g item_tfms=Resize(128), item_tfms=RandomResizedCrop(128, min_scale=0.3)

Item Transform data augmentation (Image)

Batch_tfms — one or several transforms applied to the batches once they are formed. Please find the example below which illustrates what happens when images are passed through batch_tfms.

Original Teddy bear image
Image Illustrating data batch data Augmentation
Illustration of Batch data Data Augmentation

Now that we know how to choose and use the Data Augmentation techniques in the fastai library, we can apply them to our training and validation images of our Deep Learning Neural network (eg, a UNET model) in order to train it.

Citations

https://medium.com/@pierre_guillou/data-augmentation-by-fastai-v1-84ca04bea302

https://towardsdatascience.com/introduction-to-image-augmentations-using-the-fastai-library-692dfaa2da42

https://docs.fast.ai/vision.augment.html#Item-transforms

--

--