Erasing

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

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.

Arguments
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
tuple[float, ...] | float | random | random_uniform | inpaint_telea | inpaint_ns
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
tuple[float, ...] | float | 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

Examples
>>> 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.