Sub topic 7
Occlusion Transforms: Random Erasing and Cutout
This chapter assumes basic Python, image tensors, supervised classification, and a PyTorch training loop. It also assumes you know that an image tensor usually has shape channels × height × width.
Why hide part of an image?
Occlusion transforms hide a random region of an image during training. The model must then use the remaining evidence instead of memorizing one highly visible feature.
For example, a classifier might recognize every training dog from its face. If training images sometimes contain a covered face, the model may learn to use body shape, fur pattern, or background context. This can reduce overfitting and improve performance when real images contain objects, shadows, or other obstructions.
An occlusion transform is label-preserving only when the hidden region does not change the correct answer. Hiding a small part of a car usually preserves the label “car.” Hiding the only object in an image may not.
Random erasing
Random erasing chooses a rectangle with a random position, area, and aspect ratio. It fills the rectangle with a constant value or random noise. The rectangle is different for different calls, so one stored image can produce many versions.
Torchvision’s RandomErasing works on tensors, not ordinary PIL images. It therefore belongs after ToTensor. In the following pipeline, zero means a neutral value after normalization because the normalized channel mean is zero.
import torch
from torchvision import transforms
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
train_tf = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std),
transforms.RandomErasing(
p=0.25,
scale=(0.02, 0.20),
ratio=(0.3, 3.3),
value=0,
),
])The probability p controls how often erasing occurs. Scale controls the erased area as a fraction of the image. Ratio controls the rectangle’s width-to-height range. Start with a modest probability and area, then measure whether validation performance improves.
Cutout
Cutout is a simpler related method. It removes one or more fixed-size square or rectangular regions. Unlike random erasing, it usually has fewer shape and fill choices.
import torch
class Cutout:
def __init__(self, size=48, p=0.5, value=0.0):
self.size = size
self.p = p
self.value = value
def __call__(self, image):
if torch.rand(()) >= self.p:
return image
_, height, width = image.shape
cut_h = min(self.size, height)
cut_w = min(self.size, width)
top = torch.randint(0, height - cut_h + 1, ()).item()
left = torch.randint(0, width - cut_w + 1, ()).item()
result = image.clone()
result[:, top:top + cut_h, left:left + cut_w] = self.value
return resultUse Cutout when you want a clear, easy-to-control obstruction. Use random erasing when you want more variation in area, shape, and position.
| Method | Main controls | Common risk |
|---|---|---|
| Random erasing | Probability, area, aspect ratio, fill | Too much image disappears |
| Cutout | Probability, fixed size, fill | A fixed square may be unrealistic |
Common errors and task limits
Do not apply a strong occlusion transform without checking the results. If most of the object disappears, the image may no longer have the original label. Small objects are especially vulnerable.
For object detection, the bounding box still claims that an object exists even if erasing hides it. For segmentation, the image and mask must remain spatially aligned, and erased pixels may need an ignore label rather than the original class. Occlusion methods therefore require task-specific handling.
Exercise
Why can RandomErasing improve classification but harm a detection dataset when used without adjustment?
Show answer
It can improve classification by preventing reliance on one feature, but it may hide an object while leaving its unchanged bounding box as a target. The image and annotation then disagree.