Object detection models identify something in an image, and object detection datasets are used for applications such as autonomous driving and detecting natural hazards like wildfire. This guide will show you how to apply transformations to an object detection dataset following the tutorial from Albumentations.
To run these examples, make sure you have up-to-date versions of albumentations and cv2 installed:
Copied
pip install -U albumentations opencv-python
In this example, you’ll use the cppe-5 dataset for identifying medical personal protective equipment (PPE) in the context of the COVID-19 pandemic.
image: PIL.Image.Image object containing the image.
image_id: The image ID.
height: The image height.
width: The image width.
objects: A dictionary containing bounding box metadata for the objects in the image:
id: The annotation id.
area: The area of the bounding box.
bbox: The object’s bounding box (in the coco format).
category: The object’s category, with possible values including Coverall (0), Face_Shield (1), Gloves (2), Goggles (3) and Mask (4).
You can visualize the bboxes on the image using some internal torch utilities. To do that, you will need to reference the ClassLabel feature associated with the category IDs so you can look up the string labels:
Copied
With albumentations, you can apply transforms that will affect the image while also updating the bboxes accordingly. In this case, the image is resized to (480, 480), flipped horizontally, and brightened.
albumentations expects the image to be in BGR format, not RGB, so you’ll have to convert the image before applying the transform.
Copied
Now when you visualize the result, the image should be flipped, but the bboxes should still be in the right places.
Copied
Create a function to apply the transform to a batch of examples:
Copied
Use the set_transform() function to apply the transform on-the-fly which consumes less disk space. The randomness of data augmentation may return a different image if you access the same example twice. It is especially useful when training a model for several epochs.
Copied
You can verify the transform works by visualizing the 10th example: