Menu

Deep learning for NeuroImaging in Python.

Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the gallery for the big picture.

class nidl.volume.transforms.augmentation.spatial.random_erasing.RandomErasing(scale: tuple[float, float] = (0.02, 0.33), ratio: tuple[float, float] = (1.0, 3.0), num_iterations: int = 1, value: float | str = 0.0, inplace: bool = False, **kwargs)[source]

Bases: VolumeTransform

Randomly erases boxes in a 3d volume.

Randomly selects one or multiple boxes in input data and erases their values (i.e. random erasing [R5], very similar to cutout [R6] but with arbitrary aspect ratio). It is an extension of torchvision.transforms.RandomErasing to the 3d case and it can eventually erase multiple random boxes. It handles np.ndarray or torch.Tensor as input and returns a consistent output (same type and shape).

Parameters:

scale : tuple of (float, float), default=(0.02, 0.33)

Range of proportion of erased area against input data.

ratio : tuple of (float, float), default=(1.0, 3.0)

Range of aspect ratio of erased area (min, max).

num_iterations : int, default=1

Number of erased areas.

value : float, “mean” or “random”, default=0.0

Erasing value. If “random”, erases each voxel with random values normally distributed. If “mean”, replaces each voxel with the mean value of the erased area, preserving the global statistics.

inplace : bool, default=False

If true, makes the transformation inplace, i.e. it modifies the input data directly.

kwargs : dict

Additional keyword arguments given to nidl.transforms.Transform.

Notes

In 3d, we define the “aspect ratio” as the ratio between each dimension size relatively to their geometric mean. It is a simple generalization from 2d to nd and we don’t particularize any dimension. The aspect ratio is sampled three times for a 3d volume (one for each dimension).

References

[R5] (1,2)

Zhong, Z., Zheng, L., Kang, G., Li, S., & Yang, Y. (2020). Random Erasing Data Augmentation. In AAAI Conference on Artificial Intelligence. https://arxiv.org/abs/1708.04896

[R6] (1,2)

DeVries, T., & Taylor, G. W. (2017). Improved Regularization of Convolutional Neural Networks with Cutout https://arxiv.org/abs/1708.04552

apply_transform(data: ndarray | Tensor) ndarray | Tensor[source]

Randomly erase boxes in the data.

Parameters:

data : np.ndarray or torch.Tensor

The input data with shape (C, H, W, D) or (H, W, D). Erased boxes are equal the same across channels.

Returns:

data : np.ndarray or torch.Tensor

Data with erased boxes.

Follow us

© 2025, nidl developers