Data Augmentation For Bounding Boxes: Rethinking image transforms for object detection
Data Augmentation is one way to battle this shortage of data, by artificially augmenting our dataset. In fact, the technique has proven to be so successful that it's become a staple of deep learning systems.
Random Horizontal Flip
First, we import all the necessary stuff and make sure the path is added even if we call the functions from outside the folder containing the files. The following code goes in the file
The data augmentation will be implementing is
RandomHorizontalFlip which flips an image horizontally with a probability p.
We first start by defining the class, and it's
__init__ method. The init method contains the parameters of the augmentation. For this augmentation it is the probability with each image is flipped. For another augmentation like rotation, it may contain the angle by which the object is to be rotated.
The docstring of the function has been written in Numpy docstring format. This will be useful to generate documentation using Sphinx.
__init__ method of each function is used to define all the parameters of the augmentation. However, the actually logic of the augmentation is defined in the
The call function, when invoked from a class instance takes two arguments,
img is the OpenCV numpy array containing the pixel values and
bboxes is the numpy array containing the bounding box annotations.
__call__ function also returns the same arguments, and this helps us chain together a bunch of augmentations to be applied in a Sequence.
Let us break by bit by bit what's going on in here.
In a horizontal flip, we rotate the image about a verticle line passing through its center.
The new coordinates of each corner can be then described as the mirror image of the corner in the vertical line passing through the center of the image. For the mathematically inclined, the vertical line passing through the center would be the perpendicular bisector of the line joining the original corner and the new, transformed corner.
To have a better understanding of what is going on, consider the following image. The pixels in the right half of the transformed image and the left half of the original image are mirror images of each other about the central line.
The above is accomplished by the following piece of code.
Note that the line
img = img[:,::-1,:] basically takes the array containing the image and reverses it's elements in the 1st dimension, or the dimensional which stores the x-coordinates of the pixel values.
However, one must notice that the mirror image of the top left corner is the top right corner of the resultant box. Infact, the resultant coordinates are the top-right as well as bottom-left coordinates of the bounding box. However, we need them in the top-left and bottom right format.
The side-effect of our code
The following piece of code takes care of the conversion.
We end up by returning the image and the array containing the bounding boxes.
Deterministic Version of HorizontalFlip
The above code applies the transformation stochastically with the probability p. However, if we want to build a deterministic version we can simply pass the argument p as 1. Or we could write another class, where we do not have the parameter p at all, and implement the
__call__function like this.
Seeing it in action
Now, let's suppose you have to use the HorizontalFlip augmentation with your images. We will use it on one image, but you can use it on any number you like. First, we create a file
test.py. We begin by importing all the good stuff.
Then, we import the image and load the annotation.
In order to see whether our augmentation really worked or not, we define a helper function
draw_rect which takes in
bboxes and returns a numpy image array, with the bounding boxes drawn on that image.
Let us create a file
bbox_utils.py and import the neccasary stuff.
Now, we define the function
Once, this is done, let us go back to our
test.py file, and plot the original bounding boxes.
This produces something like this.
Let us see the effect of our transformation.
You should get something like this:
- The bounding box annotation should be stored in a numpy array of size N x 5, where N is the number of objects, and each box is represented by a row having 5 attributes; the coordinates of the top-left corner, the coordinates of the bottom right corner and the class of the object.
- Each data augmentation is defined as a class, where the
__init__method is used to define the parameters of the augmentation whereas the
__call__method describes the actual logic of the augmentation. It takes two arguments, the image
imgand the bounding box annotations
bboxesand returns the transformed values.
This is it for this article. In the next article we will be dealing with
Translateaugmentations. Not only they are more complex transformations, given there are more parameters (the scaling and translation factors), but also bring some challenges that we didn't have to deal with in the
HorizontalFlip transformation. An example is to decide whether to retain a box if a portion of it is outside the image after the augmentation.
Bio: Ayoosh Kathuria currently an intern at the Defense Research and Development Organization, India, where he is working on improving object detection in grainy videos. When he's not working, he's either sleeping or playing pink floyd on his guitar. You can connect with him on LinkedIn or look at more of what he does at GitHub.
Original. Reposted with permission.
- How to Implement a YOLO (v3) Object Detector from Scratch in PyTorch: Part 1
- Building a Toy Detector with Tensorflow Object Detection API
- Getting Started with PyTorch Part 1: Understanding How Automatic Differentiation Works
Top Stories Past 30 Days