To integrate Albumentations augmentations into the existing custom dataset configuration for the D-FINE repository, you need to modify the dataset transform pipeline. The current pipeline uses PyTorch and Torchvision transforms, but Albumentations can be integrated by defining a custom transform that applies Albumentations augmentations and then incorporates it into the existing configuration.
Here's how you can modify the code and configuration to support Albumentations:
1. Create a Wrapper for Albumentations
In the src/data/transforms/ directory, create a new file (e.g., albumentations_wrapper.py) or add the following class to an existing module like _transforms.py. This wrapper will convert inputs and outputs between Albumentations and PyTorch formats.
python
import albumentations as A
from albumentations.pytorch import ToTensorV2
import numpy as np
import torch
from PIL import Image
from ...core import register
@register()
class AlbumentationsTransform:
def __init__(self, augmentations=None):
"""
Wrapper for Albumentations transformations.
Args:
augmentations: List of Albumentations transformations.
"""
if augmentations is None:
augmentations = []
self.transform = A.Compose(augmentations)
def __call__(self, image, target):
"""
Apply Albumentations transformations.
Args:
image (PIL.Image or numpy array): Input image.
target (dict): Target annotations, including bounding boxes.
Returns:
Transformed image and adjusted target.
"""
# Convert PIL Image to numpy array
if isinstance(image, Image.Image):
image = np.array(image)
# Albumentations expects bounding boxes in the format [x_min, y_min, x_max, y_max]
boxes = target.get("boxes", [])
if len(boxes) > 0:
boxes = boxes.tolist()
bboxes = [{"bbox": box, "category_id": label} for box, label in zip(boxes, target["labels"].tolist())]
else:
bboxes = []
# Perform the transformation
transformed = self.transform(
image=image,
bboxes=[b["bbox"] for b in bboxes],
category_id=[b["category_id"] for b in bboxes]
)
# Update image
image = transformed["image"]
# Update target with transformed bounding boxes
target["boxes"] = torch.tensor(transformed["bboxes"], dtype=torch.float32)
target["labels"] = torch.tensor(transformed["category_id"], dtype=torch.int64)
return image, target
2. Define Albumentations Augmentations
You can define your Albumentations augmentation pipeline. Add this in your dataset configuration file or directly in the relevant Python file. For example:
python
import albumentations as A
from albumentations.pytorch import ToTensorV2
albumentations_augmentations = [
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.5),
[Redacted Person](blur_limit=(3, 7), p=0.3),
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
ToTensorV2()
]
3. Integrate Albumentations in the Dataset Config
Modify the custom_detection.yml configuration file to include the Albumentations wrapper in the transforms pipeline. Add the following changes:
For validation, you should use minimal transformations (e.g., resizing and normalization) since augmentations are typically not applied during evaluation. Adjust the val_dataloader in custom_detection.yml as follows:
Run the training script and verify that the Albumentations augmentations are being applied during training.
6. Optional Debugging
If you want to debug the augmentations being applied, you can temporarily add a visualization step to inspect transformed images and bounding boxes. For example: