GridMask

Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32

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.

Arguments
num_grid_range
tuple[int, int]
[3,7]

Range for number of grid divisions along the shorter image side. Default: (3, 7).

line_width_range
tuple[float, float]
[0.2,0.5]

Range for line width as a fraction of grid cell size. Default: (0.2, 0.5).

rotation_range
tuple[float, float]
[0,0]

Range for grid rotation in radians. Default: (0, 0) (no rotation).

fill
tuple[float, ...] | float | random | random_uniform | inpaint_telea | inpaint_ns
0

Fill value for dropped pixels. Default: 0.

fill_mask
tuple[float, ...] | float | None

Fill value for mask. Default: None.

p
float
0.5

Probability 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