Augmentation
This module applies a range of image augmentations to the labeled dataset to improve model generalization during training. It uses the Albumentations library for robust image transformations while keeping bounding boxes aligned.
Key Features
Applies a configurable set of augmentations, including horizontal flip, brightness/contrast adjustment, color shift, noise, rotation, grayscale, and blur.
Maintains bounding box alignment using
pascal_voc
formatSaves augmented images and prediction labels in parallel
Detects and handles images without predictions to avoid generating invalid annotations.
Core Components
build_augmentation_transform(config: dict) -> A.Compose
Creates an augmentation pipeline from a config dictionary.
Transforms include:
HorizontalFlip
(defaultp=0.5
)RandomBrightnessContrast
(defaultp=0.5
)HueSaturationValue
(defaultp=0.5
)Blur
(defaultp=0.3
andblur_limit=3
)GaussNoise
(defaultp=0.3
, min=10, max=50 )ToGray
(defaultp=0.2
)Rotate
(defaultp=0.4
androtate_limit=15
)
All parameters and probabilities are configurable.
Note: YOLO uses upright bounding boxes for training. Modifying
rotate_limit
to larger angle may change the size of the bounding boxes and alter its accuracy.
augment_images(matched_pairs: list, transform: A.Compose, output_img_dir: Path, output_json_dir: Path, num_augmentations: int,config: dict)
Applies the transform pipeline on each image-label pair.
Inputs:
matched_pairs
: list of(json_path, image_path)
tuplestransform
: AlbumentationsCompose
objectoutput_img_dir
: Directory to save augmented.jpg
imagesoutput_json_dir
: Directory to save corresponding.json
label filesnum_augmentations
: Number of augmented versions to generate per imageconfig
: Dictionary that may include a base"seed"
key for reproducibility.
Behavior:
Saves augmented images as
.jpg
files and.json
labels with matching filenamesHandles images with no predictions by saving them unmodified to
no_prediction_images/
If a base seed is provided in
config
, offsets it by iteration index (base_seed + i * 2
) to ensure consistent varied results across multiple augmentations per image. Essentially, this avoids applying the exact same augmentation whennum_augmentations
> 1.
augment_dataset(image_dir: Path, output_dir: Path, config: dict) -> None
Coordinates the augmentation process end-to-end.
Inputs:
image_dir
: Directory containing the original input imagesoutput_dir
: Root directory where augmentedimages/
andlabels/
will be savedconfig
: Dictionary of augmentation settings, including:num_augmentations
: Number of times each image should be augmentedTransform parameters (e.g., probabilities and limits for each augmentation)
labeled_dir
: Path to the directory containing.json
label files
Note: If the
image_dir
is modified, thelabeled_dir
inaugmentation_config.json
should also point to the matching directory that holds the corresponding.json
label files.
By default,labeled_dir
is set toautoml_workspace/data_pipeline/labeled
.
Workflow:
Loads all
.json
label files from the directory specified byconfig["labeled_dir"]
Loads image files from
image_dir
and matches them with labels by filename stemBuilds the augmentation transform using
build_augmentation_transform(config)
Calls
augment_images()
to apply the transform and save augmented outputs:Augmented images are saved to
<output_dir>/images/
as.jpg
filesCorresponding augmented labels are saved to
<output_dir>/labels/
as.json
filesOriginal images without any predictions are saved (unmodified) to a separate folder:
<output_dir>/../no_prediction_images/
Prints a summary of:
Total label files loaded
Total image files loaded
Number of matched image-label pairs processed
Output directories used for augmented files
Configuration Parameters (for Augmentation from pipeline_config.json
)
The following fields from the augmentation_config.json
file directly control the image augmentation pipeline:
Key |
Description |
---|---|
|
Number of augmented versions to generate per image (default: |
|
Probability of flipping the image horizontally (default: |
|
Probability of applying brightness/contrast change (default: |
|
Probability of adjusting hue and saturation (default: |
|
Probability of applying Gaussian blur (default: |
|
Maximum kernel size for blur (default: |
|
Probability of adding Gaussian noise (default: |
|
Minimum variance for Gaussian noise (default: |
|
Maximum variance for Gaussian noise (default: |
|
Probability of converting the image to grayscale (default: |
|
Probability of rotating the image (default: |
|
Maximum rotation angle in degrees (default: |
These values define how aggressively and in what ways the dataset will be augmented to improve model robustness.
Example Call
augment_dataset(
image_dir=Path("automl_workspace/data_pipeline/input"),
output_dir=Path("automl_workspace/data_pipeline/labeled"),
config=config
)
Output Structure
automl_workspace/
├── data_pipeline/
│ ├── labeled/ # Original labels
│ ├── input/ # Original images
│ ├── augmented/
│ │ ├── images/ # Augmented image files
│ │ ├── labels/ # Augmented JSON files
│ │ ├── no_prediction_images/ # Skipped originals with no predictions