Shortcuts

RandomErasing

class torchvision.transforms.v2.RandomErasing(p: float = 0.5, scale: Tuple[float, float] = (0.02, 0.33), ratio: Tuple[float, float] = (0.3, 3.3), value: float = 0.0, inplace: bool = False)[source]

Randomly select a rectangle region in the input image or video and erase its pixels.

This transform does not support PIL Image. ‘Random Erasing Data Augmentation’ by Zhong et al. See https://arxiv.org/abs/1708.04896

Parameters:
  • p (float, optional) – probability that the random erasing operation will be performed.

  • scale (tuple of python:float, optional) – range of proportion of erased area against input image.

  • ratio (tuple of python:float, optional) – range of aspect ratio of erased area.

  • value (number or tuple of numbers) – erasing value. Default is 0. If a single int, it is used to erase all pixels. If a tuple of length 3, it is used to erase R, G, B channels respectively. If a str of ‘random’, erasing each pixel with random values.

  • inplace (bool, optional) – boolean to make this transform inplace. Default set to False.

Returns:

Erased input.

Example

>>> from torchvision.transforms import v2 as transforms
>>>
>>> transform = transforms.Compose([
>>>   transforms.RandomHorizontalFlip(),
>>>   transforms.PILToTensor(),
>>>   transforms.ConvertImageDtype(torch.float),
>>>   transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
>>>   transforms.RandomErasing(),
>>> ])
static get_params(img: Tensor, scale: Tuple[float, float], ratio: Tuple[float, float], value: Optional[List[float]] = None) Tuple[int, int, int, int, Tensor][source]

Get parameters for erase for a random erasing.

Parameters:
  • img (Tensor) – Tensor image to be erased.

  • scale (sequence) – range of proportion of erased area against input image.

  • ratio (sequence) – range of aspect ratio of erased area.

  • value (list, optional) – erasing value. If None, it is interpreted as “random” (erasing each pixel with random values). If len(value) is 1, it is interpreted as a number, i.e. value[0].

Returns:

params (i, j, h, w, v) to be passed to erase for random erasing.

Return type:

tuple

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources