albumentations.augmentations.dropout.coarse_dropout
Implementation of coarse dropout and random erasing augmentations. This module provides several variations of coarse dropout augmentations, which drop out rectangular regions from images. It includes CoarseDropout for randomly placed dropouts, ConstrainedCoarseDropout for dropping out regions based on masks or bounding boxes, and Erasing for random erasing augmentation. These techniques help models become more robust to occlusions and varying object completeness.
Members
- classCoarseDropout
- classConstrainedCoarseDropout
- classErasing
CoarseDropoutclass
CoarseDropout(
num_holes_range: tuple[int, int] = (1, 2),
hole_height_range: tuple[float, float] | tuple[int, int] = (0.1, 0.2),
hole_width_range: tuple[float, float] | tuple[int, int] = (0.1, 0.2),
fill: tuple[float, ...] | float | Literal['random', 'random_uniform', 'inpaint_telea', 'inpaint_ns'] = 0,
fill_mask: tuple[float, ...] | float | None = None,
p: float = 0.5
)
CoarseDropout randomly drops out rectangular regions from the image and optionally, the corresponding regions in an associated mask, to simulate occlusion and varied object sizes found in real-world settings. This transformation is an evolution of CutOut and RandomErasing, offering more flexibility in the size, number of dropout regions, and fill values.
Parameters
Name | Type | Default | Description |
---|---|---|---|
num_holes_range | tuple[int, int] | (1, 2) | Range (min, max) for the number of rectangular regions to drop out. Default: (1, 1) |
hole_height_range | One of:
| (0.1, 0.2) | Range (min, max) for the height of dropout regions. If int, specifies absolute pixel values. If float, interpreted as a fraction of the image height. Default: (0.1, 0.2) |
hole_width_range | One of:
| (0.1, 0.2) | Range (min, max) for the width of dropout regions. If int, specifies absolute pixel values. If float, interpreted as a fraction of the image width. Default: (0.1, 0.2) |
fill | One of:
| 0 | Value for the dropped pixels. Can be: - int or float: all channels are filled with this value - tuple: tuple of values for each channel - 'random': each pixel is filled with random values - 'random_uniform': each hole is filled with a single random color - 'inpaint_telea': uses OpenCV Telea inpainting method - 'inpaint_ns': uses OpenCV Navier-Stokes inpainting method Default: 0 |
fill_mask | One of:
| None | Fill value for dropout regions in the mask. If None, mask regions corresponding to image dropouts are unchanged. Default: None |
p | float | 0.5 | Probability of applying the transform. Default: 0.5 |
Example
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> # Example with random uniform fill
>>> aug_random = A.CoarseDropout(
... num_holes_range=(3, 6),
... hole_height_range=(10, 20),
... hole_width_range=(10, 20),
... fill="random_uniform",
... p=1.0
... )
>>> # Example with inpainting
>>> aug_inpaint = A.CoarseDropout(
... num_holes_range=(3, 6),
... hole_height_range=(10, 20),
... hole_width_range=(10, 20),
... fill="inpaint_ns",
... p=1.0
... )
>>> transformed = aug_random(image=image, mask=mask)
>>> transformed_image, transformed_mask = transformed["image"], transformed["mask"]
Notes
- The actual number and size of dropout regions are randomly chosen within the specified ranges for each application. - When using float values for hole_height_range and hole_width_range, ensure they are between 0 and 1. - This implementation includes deprecation warnings for older parameter names (min_holes, max_holes, etc.). - Inpainting methods ('inpaint_telea', 'inpaint_ns') work only with grayscale or RGB images. - For 'random_uniform' fill, each hole gets a single random color, unlike 'random' where each pixel gets its own random value.
References
- CutOut: https://arxiv.org/abs/1708.04552
- Random Erasing: https://arxiv.org/abs/1708.04896
- OpenCV Inpainting methods: https://docs.opencv.org/master/df/d3d/tutorial_py_inpainting.html
ConstrainedCoarseDropoutclass
ConstrainedCoarseDropout(
num_holes_range: tuple[int, int] = (1, 1),
hole_height_range: tuple[float, float] = (0.1, 0.1),
hole_width_range: tuple[float, float] = (0.1, 0.1),
fill: tuple[float, ...] | float | Literal['random', 'random_uniform', 'inpaint_telea', 'inpaint_ns'] = 0,
fill_mask: tuple[float, ...] | float | None = None,
p: float = 0.5,
mask_indices: list[int] | None = None,
bbox_labels: list[str | int | float] | None = None
)
Applies coarse dropout to regions containing specific objects in the image. This augmentation creates holes (dropout regions) for each target object in the image. Objects can be specified either by their class indices in a segmentation mask or by their labels in bounding box annotations. The hole generation differs between mask and box modes: Mask mode: 1. For each connected component in the mask matching target indices: - Samples N points randomly from within the object region (with replacement) - Creates holes centered at these points - Hole sizes are proportional to sqrt(component area), not total object area - Each component's holes are sized based on its own area Box mode: 1. For each bounding box matching target labels: - Creates N holes with random positions inside the box - Hole sizes are proportional to the box dimensions In both modes: - N is sampled once from num_holes_range and used for all objects - For example, if num_holes_range=(2,4) and 3 is sampled: * With 3 target objects, you'll get exactly 3 holes per object (9 total) * Holes may overlap within or between objects * All holes are clipped to image boundaries
Parameters
Name | Type | Default | Description |
---|---|---|---|
num_holes_range | tuple[int, int] | (1, 1) | Range for number of holes per object (min, max) |
hole_height_range | tuple[float, float] | (0.1, 0.1) | Range for hole height as proportion of object height/size (min, max). E.g., (0.2, 0.4) means: - For boxes: 20-40% of box height - For masks: 20-40% of sqrt(component area) |
hole_width_range | tuple[float, float] | (0.1, 0.1) | Range for hole width, similar to height |
fill | One of:
| 0 | Value used to fill the erased regions. Can be: - int or float: fills all channels with this value - tuple: fills each channel with corresponding value - "random": fills each pixel with random values - "random_uniform": fills entire erased region with a single random color - "inpaint_telea": uses OpenCV Telea inpainting method - "inpaint_ns": uses OpenCV Navier-Stokes inpainting method Default: 0 |
fill_mask | One of:
| None | Value used to fill erased regions in the mask. If None, mask regions are not modified. Default: None |
p | float | 0.5 | Probability of applying the transform |
mask_indices | One of:
| None | List of class indices in segmentation mask to target. Only objects of these classes will be considered for hole placement. |
bbox_labels | One of:
| None | List of object labels in bbox annotations to target. String labels will be automatically encoded. When multiple label fields are specified in BboxParams, only the first label field is used for filtering. |
Notes
At least one of mask_indices or bbox_labels must be provided. If both are provided, mask_indices takes precedence.
Erasingclass
Erasing(
scale: tuple[float, float] = (0.02, 0.33),
ratio: tuple[float, float] = (0.3, 3.3),
fill: tuple[float, ...] | float | Literal['random', 'random_uniform', 'inpaint_telea', 'inpaint_ns'] = 0,
fill_mask: tuple[float, ...] | float | None = None,
p: float = 0.5
)
Randomly erases rectangular regions in an image, following the Random Erasing Data Augmentation technique. This augmentation helps improve model robustness by randomly masking out rectangular regions in the image, simulating occlusions and encouraging the model to learn from partial information. It's particularly effective for image classification and person re-identification tasks.
Parameters
Name | Type | Default | Description |
---|---|---|---|
scale | tuple[float, float] | (0.02, 0.33) | Range for the proportion of image area to erase. The actual area will be randomly sampled from (scale[0] * image_area, scale[1] * image_area). Default: (0.02, 0.33) |
ratio | tuple[float, float] | (0.3, 3.3) | Range for the aspect ratio (width/height) of the erased region. The actual ratio will be randomly sampled from (ratio[0], ratio[1]). Default: (0.3, 3.3) |
fill | One of:
| 0 | Value used to fill the erased regions. Can be: - int or float: fills all channels with this value - tuple: fills each channel with corresponding value - "random": fills each pixel with random values - "random_uniform": fills entire erased region with a single random color - "inpaint_telea": uses OpenCV Telea inpainting method - "inpaint_ns": uses OpenCV Navier-Stokes inpainting method Default: 0 |
fill_mask | One of:
| None | Value used to fill erased regions in the mask. If None, mask regions are not modified. Default: None |
p | float | 0.5 | Probability of applying the transform. Default: 0.5 |
Example
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> # Basic usage with default parameters
>>> transform = A.Erasing()
>>> transformed = transform(image=image)
>>> # Custom configuration
>>> transform = A.Erasing(
... scale=(0.1, 0.4),
... ratio=(0.5, 2.0),
... fill_value="random_uniform",
... p=1.0
... )
>>> transformed = transform(image=image)
Notes
- The transform attempts to find valid erasing parameters up to 10 times. If unsuccessful, no erasing is performed. - The actual erased area and aspect ratio are randomly sampled within the specified ranges for each application. - When using inpainting methods, only grayscale or RGB images are supported.
References
- Paper: https://arxiv.org/abs/1708.04896
- Implementation inspired by torchvision: https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.RandomErasing