class
RandomErasing
extends
PhotometricTransform[_ErasingParams]RandomErasing(p: float = 0.5, scale: tuple[float, float] = (0.02, 0.33), ratio: tuple[float, float] = (0.3, 3.3), value: float | tuple[float, ...] | str = 0.0)Erase a random rectangular region of the image (Zhong et al., 2017).
At each call, with probability p one rectangle whose area is a
fraction of the image (sampled uniformly from scale) and whose
aspect ratio is sampled log-uniformly from ratio is replaced by
value. If no rectangle fits after 10 attempts the call is a
no-op (matches the torchvision contract).
Parameters
pfloat= 0.5Probability of applying the erase.
scale(float, float)= (0.02, 0.33)Range of the erased-region area as a fraction of the image
area. Sampled uniformly per call.
ratio(float, float)= (0.3, 3.3)Range of the erased-region aspect ratio (W / H). Sampled
log-uniformly per call (matches the reference implementation).
valuefloat or tuple of float or {"random"}= 0.0Fill value:
float→ fill the rectangle with this scalar (broadcast across channels).- tuple of length
C→ per-channel constants (e.g. the ImageNet pixel mean). "random"→ fill with i.i.d. samples from , drawn fresh on each call.
Notes
The erase is performed via a multiplicative keep-mask + additive
fill (same pattern as CoarseDropout) so the operation
composes cleanly with autograd and avoids any in-place writes.
The log-uniform aspect ratio means ratio=(0.3, 3.3) samples
landscape and portrait rectangles symmetrically, exactly matching
the spec in Zhong et al., 2017 §3.2.
Examples
Standard ImageNet recipe (p=0.25):
>>> import lucid
>>> from lucid.utils.transforms import RandomErasing
>>> tf = RandomErasing(p=0.25, scale=(0.02, 0.33), ratio=(0.3, 3.3))
>>> x = lucid.rand(3, 224, 224)
>>> y = tf(x)
>>> tuple(y.shape)
(3, 224, 224)
Per-channel ImageNet-mean fill:
>>> tf = RandomErasing(p=1.0, value=(0.485, 0.456, 0.406))