<jupyter_start><jupyter_text>Random Erasing Data Augmentation From the abstract of the [paper](https://arxiv.org/abs/1708.04896):```In training, Random Erasing randomly selects a rectangle region in an image and erases its pixels with random values. In this process, training images with various levels of occlusion are generated, which reduces the risk of over-fitting and makes the model robust to occlusion. Random Erasing is parameter learning free, easy to implement, and can be integrated with most of the CNN-based recognition models.```  As seen from image above, this `RandomErase` data augmentation, randomly selects a region from the input image, erases the existing image in that region and fills the region with random values.  Training models with `RandomErase` using `timm`'s training script To train a model using the `RandomErase` data augmentation using `timm`'s training script, simply add the `--reprob` flag with a probability value. ```pythonpython train.py ../imagenette2-320 --reprob 0.4```Running the above command applies the `RandomErase` data augmentation to the input images with a probability of `0.4`.  Using `RandomErase` data augmentation in custom training scripts Section `1.1` provides an example of using `RandomErase` data augmentation to train a nueral net using `timm`'s training script.  But often you might want to simply just use `RandomErase` augmentation using your own custom training loop. This section explains how one could achieve that.  The `RandomErase` data augmentation inside `timm` is implemented inside `RandomErasing` class. All we do in the code below, is first create an input image tensor, and visualize it.  > NOTE: This variant of RandomErasing is intended to be applied to either a batch or single image tensor after it has been normalized by dataset mean and std. This is different from `RandAugment` where the class expects a `PIL.Image` as input.<jupyter_code>from PIL import Image
from timm.data.random_erasing import RandomErasing
from torchvision import transforms
from matplotlib import pyplot as plt

img = Image.open("../../imagenette2-320/train/n01440764/ILSVRC2012_val_00000293.JPEG")
x   = transforms.ToTensor()(img)
plt.imshow(x.permute(1, 2, 0))<jupyter_output><empty_output><jupyter_text>Great, as we can see it is the same image of a "tench" as shown pretty much everywhere inside this documentation. Let's now apply the `RandomErasing` augmentation and visualize the results.<jupyter_code>random_erase = RandomErasing(probability=1, mode='pixel', device='cpu')
plt.imshow(random_erase(x).permute(1, 2, 0))<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
StarcoderdataJupyterStructuredCleanDedup