To apply Albumentations augmentations to the image and bounding boxes in the D-FINE repository, you need to modify the transforms and custom dataset config. Here's a step-by-step guide on how to do it:
Step 1: Install Albumentations
First, you need to install the Albumentations library. You can do this by running the following command in your terminal:
bash
pip install albumentations
Step 2: Modify the Transforms
Next, you need to modify the transforms to use Albumentations. Create a new file called albumentations_transforms.py in the src/data/transforms directory:
In this example, we define two transforms: get_albumentations_transforms for training and get_albumentations_transforms_val for validation. The training transform applies random horizontal flipping, random sized bounding box safe cropping, random brightness and contrast adjustment, hue saturation value adjustment, conversion to grayscale, and rotation. The validation transform only converts the image to a tensor.
Step 3: Modify the Custom Dataset Config
Next, you need to modify the custom dataset config to use the Albumentations transforms. Open the custom_detection.yml file in the configs/dataset directory and add the following lines:
In this example, we specify the AlbumentationsTransforms type and the get_albumentations_transforms and get_albumentations_transforms_val functions for the training and validation datasets, respectively.
Step 4: Update the Dataset Class
Finally, you need to update the dataset class to apply the Albumentations transforms to the image and bounding boxes. Open the _dataset.py file in the src/data/dataset directory and modify the __getitem__ method as follows:
python
def __getitem__(self, index):
img, target = self.load_item(index)
if self.transforms is not None:
if isinstance(self.transforms, A.Compose):
transformed = self.transforms(image=img, bboxes=target['boxes'], class_labels=target['labels'])
img = transformed['image']
target['boxes'] = torch.tensor(transformed['bboxes'])
else:
img, target, _ = self.transforms(img, target, self)
return img, target
In this example, we check if the transforms are an instance of A.Compose and apply the Albumentations transforms to the image and bounding boxes if they are. Otherwise, we apply the original transforms.
That's it! You should now be able to apply Albumentations augmentations to the image and bounding boxes in the D-FINE repository.