Stay updated

News & Insights
utils

albumentations.augmentations.dropout.grid_mask


Apply GridMask augmentation by dropping grid-line regions.

Members

GridMaskclass

GridMask(
    num_grid_range: tuple[int, int] = (3, 7),
    line_width_range: tuple[float, float] = (0.2, 0.5),
    rotation_range: tuple[float, float] = (0, 0),
    fill: tuple[float, ...] | float | random | random_uniform | inpaint_telea | inpaint_ns = 0,
    fill_mask: tuple[float, ...] | float | None,
    p: float = 0.5
)

Apply GridMask augmentation by dropping grid-line regions. Unlike GridDropout which drops rectangular cells, GridMask drops the grid lines themselves — continuous horizontal and vertical stripes forming a grid pattern. The grid can optionally be rotated.

Parameters

NameTypeDefaultDescription
num_grid_rangetuple[int, int](3, 7)Range for number of grid divisions along the shorter image side. Default: (3, 7).
line_width_rangetuple[float, float](0.2, 0.5)Range for line width as a fraction of grid cell size. Default: (0.2, 0.5).
rotation_rangetuple[float, float](0, 0)Range for grid rotation in radians. Default: (0, 0) (no rotation).
fill
One of:
  • tuple[float, ...]
  • float
  • random
  • random_uniform
  • inpaint_telea
  • inpaint_ns
0Fill value for dropped pixels. Default: 0.
fill_mask
One of:
  • tuple[float, ...]
  • float
  • None
-Fill value for mask. Default: None.
pfloat0.5Probability of applying the transform. Default: 0.5.

Examples

>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.GridMask(num_grid_range=(3, 5), line_width_range=(0.2, 0.4), p=1.0)
>>> result = transform(image=image)["image"]

Notes

GridMask was shown to outperform AutoAugment while being less computationally expensive. It achieves +1.4% on ImageNet (ResNet50), +1.8% on COCO detection (FasterRCNN-50-FPN), and +0.8% on Cityscapes segmentation (PSPNet50).

References

  • [{'description': 'GridMask paper', 'source': 'https://arxiv.org/abs/2001.04086'}]