GridDropout
Apply GridDropout augmentation to images, masks, bounding boxes, and keypoints.
GridDropout drops out rectangular regions of an image and the corresponding mask in a grid fashion. This technique can help improve model robustness by forcing the network to rely on a broader context rather than specific local features.
ratioThe ratio of the mask holes to the unit size (same for horizontal and vertical directions). Must be between 0 and 1. Default: 0.5.
unit_size_rangeRange from which to sample grid size. Default: None. Must be between 2 and the image's shorter edge. If None, grid size is calculated based on image size.
holes_number_xyThe number of grid units in x and y directions. First value should be between 1 and image width//2, Second value should be between 1 and image height//2. Default: None. If provided, overrides unit_size_range.
random_offsetWhether to offset the grid randomly between 0 and (grid unit size - hole size). If True, entered shift_xy is ignored and set randomly. Default: True.
fillValue 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_maskValue for the dropped pixels in mask. If None, the mask is not modified. Default: None.
shift_xyOffsets of the grid start in x and y directions from (0,0) coordinate. Only used when random_offset is False. Default: (0, 0).
pProbability of applying the transform. Default: 0.5.
>>> 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 standard fill value
>>> aug_basic = A.GridDropout(
... ratio=0.3,
... unit_size_range=(10, 20),
... random_offset=True,
... p=1.0
... )
>>> # Example with random uniform fill
>>> aug_random = A.GridDropout(
... ratio=0.3,
... unit_size_range=(10, 20),
... fill="random_uniform",
... p=1.0
... )
>>> # Example with inpainting
>>> aug_inpaint = A.GridDropout(
... ratio=0.3,
... unit_size_range=(10, 20),
... fill="inpaint_ns",
... p=1.0
... )
>>> transformed = aug_random(image=image, mask=mask)
>>> transformed_image, transformed_mask = transformed["image"], transformed["mask"]- If both unit_size_range and holes_number_xy are None, the grid size is calculated based on the image size.
- The actual number of dropped regions may differ slightly from holes_number_xy due to rounding.
- Inpainting methods ('inpaint_telea', 'inpaint_ns') work only with grayscale or RGB images.
- For 'random_uniform' fill, each grid cell gets a single random color, unlike 'random' where each pixel gets its own random value.