How Flip Augmentation Improves Model Performance

Flipping an image (and its annotations) is a deceivingly simple technique that can improve model performance in substantial ways.

Our models are learning what collection of pixels and the relationship between those collections of pixels denote an object is in-frame. But machine learning models (like convolutional neural networks) have a tendency to be quite brittle: they might memorize a specific ordering of pixels describes an object, but if that same object is mirrored across the image, our models may struggle to recognize it.

Consider the orientation of your face when you are taking a selfie versus using the backwards lens on your camera: one interpretation may be mirrored while the other is not, yet they are still both your face. This mirroring of orientation is what we call flipping an image.

By creating several versions of our images in various orientations, we give our deep learning model more information to learn from without having to go through the time consuming process of collecting and labeling more training data.

Implementing Flip

Flipping an image is quite straightforward in NumPy:

    def update_image(self):
        if self.kwargs["how"] == "vertical":
            img = np.flipud(self.img)
        elif self.kwargs["how"] == "horizontal":
            img = np.fliplr(self.img)
        return img

Remember, we need to also handle the image’s annotations as well:

if self.kwargs["how"] == "vertical":
    for box in self.annotations["boxes"]:
    name = box['label']
    # unchanged
    width = box["width"]
    # unchanged
    height = box["height"]
    # check if annotation is on bottom of image
    if box["y"] < img_height/2:
        # new Y position is addition of center and diff of
        # (center and old y)
        nY = img_height/2 + (img_height/2 - box["y"])
    # else annotation is on right side of image
    elif box["y"] >= img_height/2:
        # new Y position is center  diff between old x and center
        nY = img_height/2 - (box["y"] - img_height/2)
    # unchanged
    nX = box["x"]
    # create new boxes for each label
    new_box.append({
    "label": name,
    "x": nX,
    "y": nY,
    "width": width,
    "height": height
})

Flips in Roboflow

Roboflow includes a flip augmentation by default on all accounts. Simply create a dataset, check "Flip" augmentation, and we handle the image and annotations!

Simply toggle it on!