DataAugmentationHelper Module¶
Provides comprehensive data augmentation pipelines for images and other data types.
- HMB.DataAugmentationHelper.PerformDataAugmentation(imagePath, config, numResultantImages, auxImagesList=None, extensions=('.png', '.jpg', '.jpeg', '.bmp', '.gif'), seed=None)[source]¶
Performs data augmentation on an image by randomly applying one augmentation technique per generated image.
This function loads an image from the specified path and generates multiple augmented versions by randomly selecting and applying one augmentation technique to each generated image. The augmentation techniques are configured through a dictionary where each technique can be enabled or disabled.
- Parameters:
imagePath (str) – Path to the input image file.
config (Dict[str, Any]) – Dictionary containing augmentation configurations with “enabled” flags and parameter ranges. Each augmentation type should have an “enabled” key (bool) and relevant parameters for that technique.
numResultantImages (int) – Number of augmented images to generate.
auxImagesList (List[str]) – Optional list of auxiliary image paths for advanced augmentations like mixup, cutmix, mosaic. Default is None.
extensions (Tuple[str, ...]) – Tuple of valid image file extensions for auxiliary images. Default includes common formats.
seed (int | None)
- Returns:
List of augmented PIL Image objects.
- Return type:
List[Image.Image]
Examples
configs = { "rotation" : {"enabled": True, "range": (-30, 30)}, "flip" : {"enabled": True, "horizontal": True, "vertical": False}, "brightness" : {"enabled": True, "range": (0.7, 1.3)}, "contrast" : {"enabled": True, "range": (0.8, 1.2)}, "saturation" : {"enabled": True, "range": (0.8, 1.2)}, "blur" : {"enabled": True, "range": (0, 2)}, "sharpness" : {"enabled": True, "range": (0.5, 2.0)}, "zoom" : {"enabled": True, "range": (0.8, 1.2)}, "translation" : {"enabled": True, "range": (-20, 20)}, "noise" : {"enabled": True, "range": (0, 25)}, "cutout" : {"enabled": True, "numHoles": (1, 3), "holeSize": (0.1, 0.2)}, "hideAndSeek" : {"enabled": True, "gridSize": (4, 8), "hideProb": 0.5}, "gridmask" : {"enabled": True, "dRange": (0.3, 0.5), "rRange": (0.4, 0.7)}, "randomErasing" : {"enabled": True, "probability": 0.5, "area": (0.02, 0.2), "aspectRatio": (0.3, 3.3)}, "colorJitter" : { "enabled": True, "hueShift": (-0.05, 0.05), "saturationShift": (-0.1, 0.1), "valueShift": (-0.1, 0.1) }, "elasticDeformation" : {"enabled": True, "alpha": (30, 40), "sigma": (4, 6)}, "perspectiveTransform": {"enabled": True, "scale": (0.0, 0.15)}, "affineTransform" : {"enabled": True, "scale": (0.9, 1.1), "shear": (-10, 10), "rotate": (-20, 20)}, "clahe" : {"enabled": True, "clipLimit": (2.0, 4.0), "tileGridSize": (8, 8)}, "speckleNoise" : {"enabled": True, "intensity": (0.0, 0.15)}, "saltPepperNoise" : {"enabled": True, "amount": (0.01, 0.03), "saltVsPepper": 0.5}, "poissonNoise" : {"enabled": True, "scale": (1.0, 5.0)}, "motionBlur" : {"enabled": True, "kernelSize": (5, 11), "angle": (0, 360)}, "medianBlur" : {"enabled": True, "kernelSize": (3, 5)}, "bilateralFilter" : {"enabled": True, "d": (5, 9), "sigmaColor": (75, 125), "sigmaSpace": (75, 125)}, "channelShuffle" : {"enabled": True}, "invert" : {"enabled": True}, "solarize" : {"enabled": True, "threshold": (128, 200)}, "posterize" : {"enabled": True, "bits": (3, 5)}, "equalize" : {"enabled": True}, "emboss" : {"enabled": True}, "edgeEnhance" : {"enabled": True, "factor": (0.3, 0.6)}, "coarseDropout" : {"enabled": True, "numHoles": (3, 6), "holeSize": (0.05, 0.12), "fillValue": (0, 0, 0)}, "mixup" : {"enabled": True, "alpha": 0.4, "auxImageDir": classPath}, "cutmix" : {"enabled": True, "alpha": 1.0, "auxImageDir": classPath}, "mosaic" : {"enabled": True, "auxImageDir": classPath, "numImages": 4} } # Performing data augmentation to generate 5 augmented images. augmentedImages = PerformDataAugmentation( imagePath="path/to/image.jpg", config=configs, numResultantImages=5, auxImagesList=None ) # Saving the augmented images to a specified folder. outputFolder = "path/to/augmented_images" os.makedirs(outputFolder, exist_ok=True) SaveAugmentedImages( augmentedImages, outputFolder, baseFilename="augmented_image", )
- HMB.DataAugmentationHelper.ApplyAugmentation(image, augmentationType, augmentationParams, auxImagesList=None, extensions=('.png', '.jpg', '.jpeg', '.bmp', '.gif'))[source]¶
Applies a specific augmentation technique to an image.
This function takes an input image and applies one of the supported augmentation techniques based on the specified augmentation type and parameters. It supports a wide range of augmentation techniques including geometric transformations, color adjustments, noise addition, and advanced techniques like mixup and cutmix.
- Parameters:
image (Image.Image) – Input PIL Image object to be augmented.
augmentationType (str) – Type of augmentation to apply (e.g., “rotation”, “flip”, “brightness”, etc.).
augmentationParams (Dict[str, Any]) – Parameters for the selected augmentation technique, including ranges and specific settings for that augmentation type.
auxImagesList (List[str]) – Optional list of auxiliary image paths for advanced augmentations like mixup, cutmix, and mosaic. Default is None.
extensions (Tuple[str, ...]) – Tuple of valid image file extensions for auxiliary images. Default includes common formats.
- Returns:
Augmented PIL Image object.
- Return type:
Image.Image
- HMB.DataAugmentationHelper.LoadAuxiliaryImages(directory, maxImages=100, extensions=('.png', '.jpg', '.jpeg', '.bmp', '.gif'), verbose=True)[source]¶
Loads auxiliary image paths from a directory for advanced augmentations.
This function scans a directory for image files and returns a list of their paths. These paths can be used for advanced data augmentation techniques that require additional images, such as mixup, cutmix, and mosaic. The function supports common image formats including PNG, JPG, JPEG, BMP, and GIF.
- Parameters:
directory (str) – Path to the directory containing auxiliary images.
maxImages (int) – Maximum number of image paths to load. Default is 100.
extensions (Tuple[str, ...]) – Tuple of acceptable image file extensions. Default includes common formats.
verbose (bool) – If True, apply verbose logging. Default is True.
- Returns:
List of image file paths found in the directory.
- Return type:
List[str]
- HMB.DataAugmentationHelper.SaveAugmentedImages(augmentedImages, outputDir, baseFilename, outputExtension='.png', verbose=True)[source]¶
Saves augmented images to the specified output directory.
This function takes a list of augmented PIL Image objects and saves them to disk with sequential numbering. The output directory is created automatically if it does not exist. Each saved image is named using the base filename followed by “_Aug_” and a sequential number.
- Parameters:
augmentedImages (List[Image.Image]) – List of augmented PIL Image objects to save.
outputDir (str) – Directory where augmented images will be saved.
baseFilename (str) – Base name for the output files (without extension).
outputExtension (str) – File extension for the saved images (default is “.png”).
verbose (bool) – If True, prints the path of each saved image (default is True).
- Return type: