0
0
Computer Visionml~5 mins

Random erasing in Computer Vision

Choose your learning style9 modes available
Introduction
Random erasing helps a computer see images better by hiding small parts randomly during training. This makes the computer learn to recognize objects even if parts are missing or covered.
When training a model to recognize objects in photos that might be partly blocked.
When you want to make your image recognition model stronger and less likely to make mistakes.
When you have a small set of images and want to create more variety without adding new pictures.
When you want to prevent your model from memorizing exact details and instead learn general features.
Syntax
Computer Vision
RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False)
p: chance to apply random erasing on an image during training.
scale and ratio control the size and shape of the erased area.
Examples
Randomly erases part of the image half of the time during training.
Computer Vision
RandomErasing(p=0.5)
Always erases a square patch between 10% and 20% of the image size, filling it with gray color (0.5).
Computer Vision
RandomErasing(p=1.0, scale=(0.1, 0.2), ratio=(1, 1), value=0.5)
Erases part of the image 30% of the time and modifies the image directly without making a copy.
Computer Vision
RandomErasing(p=0.3, inplace=True)
Sample Model
This code downloads a dog image, resizes it, converts it to a tensor, and applies random erasing with 100% chance. It erases a small square patch filled with zeros (black). Then it prints the shape and some pixel values from the erased area to show the effect.
Computer Vision
import torch
from torchvision import transforms
from PIL import Image
import requests
from io import BytesIO

# Load an example image
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/320px-YellowLabradorLooking_new.jpg'
response = requests.get(url)
img = Image.open(BytesIO(response.content)).convert('RGB')

# Define transform with Random Erasing
transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.ToTensor(),
    transforms.RandomErasing(p=1.0, scale=(0.1, 0.1), ratio=(1, 1), value=0)
])

# Apply transform
img_tensor = transform(img)

# Show tensor shape and a small part of tensor values
print(f"Image tensor shape: {img_tensor.shape}")
print(f"Sample pixel values at erased area (first 5 values): {img_tensor[:, 50:55, 50:55].flatten()[:5]}")
OutputSuccess
Important Notes
Random erasing is usually applied only during training, not when testing or using the model.
The erased area can be filled with zeros, random values, or the mean pixel value to simulate different occlusions.
Choosing the right probability and size of erasing is important to avoid hurting model performance.
Summary
Random erasing hides random parts of images during training to help models learn better.
It makes models more robust to missing or blocked parts in real images.
Use it as a simple way to improve image recognition without adding new data.