Data Augmentation Compilation with Python and OpenCV

Prevent Your Model from Overfitting

LA Tran
Towards Data Science

--

Photo by Ulises Baga on Unsplash

Data augmentation is a technique to increase the diversity of dataset without an effort to collect any more real data but still help improve your model accuracy and prevent the model from overfitting. In this post, you will learn to implement the most popular and efficient data augmentation procedures for object detection task using Python and OpenCV.

The set of data augmentation methods that are about to be introduced includes:

  1. Random Crop
  2. Cutout
  3. ColorJitter
  4. Adding Noise
  5. Filtering

Firstly, let’s import several libraries and prepare some necessary subroutines before going ahead.

The below image is used as a sample image during this post.

Image: tr03–14–18–1-FRONT.jpg in WAYMO Dataset

Random Crop

Random Crop selects randomly a region and crops it out to make a new data sample, the cropped region should have the same width/height ratio as the original image to maintain the shapes of objects.

Image by Author

From the above figure, the left image indicates the original image with the ground-truth bounding boxes (in red), a new sample as the right image is created by cropping the region inside the orange box. In the new sample’s annotation, all the objects which do not overlap with the orange box in the left image are removed, and the coordinates of the objects which lie on the orange box boundary are refined to be proper with the new image sample. The outputs of random crop for an original image are a new cropped image and its annotation.

Cutout

Cutout, introduced in 2017 by Terrance DeVries and Graham W. Taylor in their paper, is a simple regularization technique of randomly masking out square regions of input during training, which can be used to improve the robustness and overall performance of convolutional neural networks. This method is not only extremely easy to implement but also demonstrates that it can be used in conjunction with existing forms of data augmentation and other regularizers to further improve model performance.

As in the paper, cutout was applied to improve image recognition (classification) accuracy , hence, if we deploy the same scheme to object detection dataset, it may cause the problem of losing objects, especially small objects. In the figure below, a considerable number of small objects inside the cutout area (black region) are removed, and this is not proper with the spirit of data augmentation.

Image by Author

In order to make this manner suitable to object detection, we can make a simple modification, instead of using only one mask and put it at a random position in the image, it would be better when we choose randomly half number of objects and apply cutout to each of those object areas. The augmented image is shown as the right image in the figure below.

Image by Author

The output of cutout is a newly generated image, we do not remove objects or change image size, then the annotation for the generated image is as the original one.

ColorJitter

ColorJitter is another simple type of image data augmentation where we randomly change the brightness, contrast, and saturation of the image. I believe that this “guy” can be easily understood by most readers.

Image by Author

Adding Noise

In common sense, noise is considered an unexpected factor in an image, nevertheless, several genres of noises (e.g., Gaussian, salt-and-pepper) can be utilized for data augmentation and adding noise is a very simple and beneficial method of data augmentation in deep learning. In the following example, Gaussian noise and salt-and-pepper noise are added to the original image for the sake of data augmentation.

Image by Author

For those who can not recognize the difference between Gaussian noise and salt-and-pepper noise, Gaussian noise has a range of values from 0 to 255 depending on the configuration, therefore, in RGB image, Gaussian noise pixel is able to be any colors. In contrast, salt-and-pepper noise pixel can have only two values 0 or 255 respective to black (pepper) or white (salt).

Filtering

The last data augmenting procedure that is introduced in this post is filtering. Similar to adding noise, filtering is also simple and easy to implement. Three types of filtering that are used in the implementation include blur (mean), Gaussian, and median.

Image by Author

Summary

Image by Author

In this post, I have introduced to all of you a tutorial on the implementation of data augmentation for the object detection task. You guys can find my full implementation here. If you feel it helps, please do not hesitate to give it a star. You are welcome to visit my Facebook fanpage which is for sharing things regarding Machine Learning: Diving Into Machine Learning.

It’s enough for today. Thanks for spending time!

--

--

PhD in Machine Learning and Computer Vision | Once the WHY is clear, the HOW goes easy.