class mindspore.dataset.vision.py_transforms.RandomErasing(prob=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False, max_attempts=10)[source]

Randomly erase the pixels within a random selected rectangle region with a given probability.

See Zhun Zhong et al. ‘Random Erasing Data Augmentation’ 2017 on https://arxiv.org/pdf/1708.04896.pdf

  • prob (float, optional) – Probability of the image being randomly erased (default=0.5).

  • scale (sequence of floats, optional) – Range of the relative erase area to the original image (default=(0.02, 0.33)).

  • ratio (sequence, optional) – Range of aspect ratio of the erased area (default=(0.3, 3.3)).

  • value (Union[int, sequence, str]) – Erasing value (default=0). If value is a single integer, it is used to erase all pixels. If value is a sequence of length 3, it is used to erase R, G, B channels respectively. If value is a string of ‘random’, each pixel will be erased with a random value obtained from a standard normal distribution.

  • inplace (bool, optional) – Whether to apply this transformation inplace (default=False).

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid area to be erased (default=10). If exceeded, return the original image.

  • TypeError – If prob is not of type float.

  • TypeError – If scale is not of type sequence.

  • TypeError – If ratio is not of type sequence.

  • TypeError – If value is not of type integer, sequence or string.

  • TypeError – If inplace is not of type boolean.

  • TypeError – If max_attempts is not of type integer.

  • ValueError – If prob is not in range [0, 1].

  • ValueError – If scale is negative.

  • ValueError – If ratio is negative.

  • ValueError – If value is not in range [0, 255].

  • ValueError – If max_attempts is not positive.

Supported Platforms:



>>> from mindspore.dataset.transforms.py_transforms import Compose
>>> transforms_list = Compose([py_vision.Decode(),
...                            py_vision.ToTensor(),
...                            py_vision.RandomErasing(value='random')])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")