0
0
Computer Visionml~15 mins

Random erasing in Computer Vision - Deep Dive

Choose your learning style9 modes available
Overview - Random erasing
What is it?
Random erasing is a technique used in training computer vision models where parts of an image are randomly covered or erased. This helps the model learn to recognize objects even when parts are missing or obscured. It works by selecting a random rectangle in the image and replacing its pixels with random values or a constant color. This simple change makes the model more robust and less likely to rely on specific details.
Why it matters
Without random erasing, models can become too focused on small details that might not always be visible in real life, like a logo or a specific pattern. This makes them fragile when images are noisy or partially blocked. Random erasing forces the model to learn more general features, improving its ability to recognize objects in varied and challenging situations. This leads to better performance in real-world applications like self-driving cars or medical image analysis.
Where it fits
Before learning random erasing, you should understand basic image data augmentation techniques like flipping, cropping, and color jittering. After mastering random erasing, you can explore more advanced augmentation methods and regularization techniques that improve model generalization, such as Cutout, Mixup, or adversarial training.
Mental Model
Core Idea
Random erasing teaches a model to ignore missing or obscured parts of an image by randomly hiding sections during training.
Think of it like...
It's like practicing reading a book with some words blacked out randomly, so you learn to understand the story even if parts are missing.
Original Image
┌─────────────────────────┐
│                         │
│   [Picture of object]    │
│                         │
└─────────────────────────┘

After Random Erasing
┌─────────────────────────┐
│                         │
│   [Picture with a       │
│    random block missing]│
│                         │
└─────────────────────────┘
Build-Up - 7 Steps
1
FoundationWhat is Data Augmentation
🤔
Concept: Data augmentation means changing training images slightly to help models learn better.
Imagine you have 100 pictures of cats. If you flip some horizontally or change brightness, you get more variety without taking new photos. This helps the model see many versions of cats and not memorize just one picture.
Result
The model sees more diverse images and learns to recognize cats in different situations.
Understanding data augmentation is key because random erasing is a special kind of augmentation that hides parts of images.
2
FoundationWhy Models Need Robustness
🤔
Concept: Models should recognize objects even if parts are missing or changed.
If a model only learns from perfect pictures, it might fail when the object is partly hidden or the photo is blurry. Real-world images often have noise, shadows, or occlusions.
Result
Models trained with robustness techniques perform better on new, imperfect images.
Knowing why robustness matters helps you appreciate why random erasing is useful.
3
IntermediateHow Random Erasing Works
🤔Before reading on: do you think random erasing removes the same part of every image or different parts each time? Commit to your answer.
Concept: Random erasing hides a random rectangle in each image during training to simulate occlusion.
For each training image, a rectangle is chosen randomly in size and position. The pixels inside this rectangle are replaced with random colors or a fixed value like black. This happens differently every time the image is used in training.
Result
The model sees many versions of the same image with different parts missing, forcing it to learn more general features.
Understanding the randomness in position and size is crucial because it prevents the model from memorizing erased areas.
4
IntermediateParameters Controlling Erasing
🤔Before reading on: do you think the erased area is usually very large or small compared to the whole image? Commit to your answer.
Concept: Random erasing uses parameters like area ratio and aspect ratio to control the size and shape of the erased region.
You can set minimum and maximum proportions of the image area to erase, and limits on the rectangle's shape (width vs height). This controls how much and what shape of the image is hidden, balancing difficulty for the model.
Result
Fine-tuning these parameters affects how much the model learns to handle occlusion without losing too much information.
Knowing these parameters helps you customize random erasing for different datasets and tasks.
5
IntermediateRandom Erasing vs Other Augmentations
🤔Before reading on: do you think random erasing is similar to cropping or more like adding noise? Commit to your answer.
Concept: Random erasing is unique because it hides parts of the image rather than changing the whole image or adding noise everywhere.
Unlike flipping or color changes, random erasing simulates occlusion by removing information in a localized area. This is different from cropping, which removes edges, or noise, which affects all pixels slightly.
Result
This makes random erasing especially good at teaching models to handle missing parts rather than just variations in appearance.
Understanding this difference clarifies when to use random erasing alongside other augmentations.
6
AdvancedImplementing Random Erasing in Code
🤔Before reading on: do you think random erasing should be applied before or after normalizing images? Commit to your answer.
Concept: Random erasing is usually applied after basic augmentations but before normalization in the data pipeline.
Here is a simple Python example using PyTorch: import torch import torchvision.transforms as transforms import random class RandomErasing: def __init__(self, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)): self.p = p self.scale = scale self.ratio = ratio def __call__(self, img): if random.uniform(0, 1) > self.p: return img area = img.size(1) * img.size(2) for _ in range(100): target_area = random.uniform(*self.scale) * area aspect_ratio = random.uniform(*self.ratio) h = int(round((target_area * aspect_ratio) ** 0.5)) w = int(round((target_area / aspect_ratio) ** 0.5)) if w < img.size(2) and h < img.size(1): x1 = random.randint(0, img.size(1) - h) y1 = random.randint(0, img.size(2) - w) img[:, x1:x1+h, y1:y1+w] = torch.randn(img.size(0), h, w) return img return img result = "Random parts of images are erased during training, increasing robustness."
Result
The model trains on images with random erased patches, improving generalization.
Knowing how to implement random erasing helps you customize and integrate it into your training pipeline effectively.
7
ExpertSurprising Effects on Model Behavior
🤔Before reading on: do you think random erasing always improves accuracy or can it sometimes hurt performance? Commit to your answer.
Concept: Random erasing can sometimes reduce training accuracy but improve test accuracy by preventing overfitting.
Because random erasing hides parts of images, the model may find training harder and show lower accuracy during training. However, this forces the model to learn more general features, which often leads to better accuracy on new, unseen data. Also, random erasing can interact with batch normalization and other layers in subtle ways, affecting training dynamics.
Result
Models trained with random erasing often generalize better, even if training looks harder or slower.
Understanding this tradeoff helps set realistic expectations and tune training schedules when using random erasing.
Under the Hood
Random erasing works by modifying the input image tensor during training. It selects a random rectangle area and replaces pixel values with noise or a constant. This forces the convolutional neural network to rely less on specific local features and more on distributed patterns. Internally, this acts like a form of regularization, similar to dropout but applied to input data rather than neurons. It increases the diversity of training samples and reduces overfitting by preventing the model from memorizing exact pixel patterns.
Why designed this way?
Random erasing was designed to simulate real-world occlusions and noise that models face in practice. Earlier augmentations changed global image properties but did not mimic missing parts. Alternatives like Cutout erase fixed-size patches, but random erasing's variable size and position make it more flexible. The design balances simplicity, efficiency, and effectiveness without requiring extra data or complex computations.
Input Image
   │
   ▼
Random Rectangle Selection
   │
   ▼
Erase Pixels in Rectangle (replace with noise or constant)
   │
   ▼
Modified Image
   │
   ▼
Model Training on Modified Image
Myth Busters - 4 Common Misconceptions
Quick: Does random erasing always improve model accuracy on training data? Commit yes or no.
Common Belief:Random erasing always makes the model more accurate during training.
Tap to reveal reality
Reality:Random erasing often lowers training accuracy because it makes the task harder, but it improves accuracy on new, unseen data.
Why it matters:Expecting training accuracy to always improve can lead to stopping training too early or thinking the method is broken.
Quick: Is random erasing the same as adding random noise to the whole image? Commit yes or no.
Common Belief:Random erasing is just adding noise to the entire image.
Tap to reveal reality
Reality:Random erasing hides only a random patch, not the whole image, simulating occlusion rather than noise everywhere.
Why it matters:Confusing these can lead to using the wrong augmentation and missing the benefits of occlusion simulation.
Quick: Does random erasing require extra labeled data to work? Commit yes or no.
Common Belief:Random erasing needs additional labeled images to be effective.
Tap to reveal reality
Reality:Random erasing works by modifying existing images and does not require extra data or labels.
Why it matters:Believing this might discourage use due to perceived data costs.
Quick: Can random erasing be applied during model testing? Commit yes or no.
Common Belief:Random erasing should be applied during both training and testing for consistency.
Tap to reveal reality
Reality:Random erasing is only applied during training; applying it during testing would hide important information and reduce accuracy.
Why it matters:Applying it during testing would confuse the model and degrade performance.
Expert Zone
1
Random erasing interacts with batch normalization differently depending on erased area size, sometimes requiring tuning of batch norm parameters.
2
The choice between replacing erased pixels with random noise or a constant value affects model learning dynamics and can be dataset-dependent.
3
Random erasing can be combined with other augmentations like Mixup or CutMix, but the order of application influences final model performance.
When NOT to use
Random erasing is less effective or harmful when training on very small datasets where every pixel matters, or on tasks requiring precise pixel-level details like medical image segmentation. Alternatives like Cutout or Mixup might be better in those cases.
Production Patterns
In production, random erasing is often integrated into data pipelines using libraries like Albumentations or torchvision transforms. It is combined with other augmentations and used with careful parameter tuning. Monitoring training curves is important to adjust erasing probability and size to avoid underfitting.
Connections
Dropout
Both are regularization techniques that randomly hide information during training to prevent overfitting.
Understanding random erasing as input-level dropout helps grasp how hiding parts of data forces models to learn more robust features.
Robustness in Human Vision
Random erasing mimics how humans recognize objects even when parts are blocked or missing.
Knowing how humans handle occlusion explains why teaching models to ignore missing parts improves their real-world performance.
Error Correction in Communication Systems
Both involve handling missing or corrupted information to recover the original message or meaning.
Seeing random erasing like error correction shows how systems can learn to fill gaps and still function well despite incomplete data.
Common Pitfalls
#1Applying random erasing during model evaluation or testing.
Wrong approach:def evaluate(model, data_loader): for images, labels in data_loader: images = random_erasing(images) # Wrong: augmenting test data outputs = model(images) # compute accuracy
Correct approach:def evaluate(model, data_loader): for images, labels in data_loader: outputs = model(images) # No augmentation during testing # compute accuracy
Root cause:Misunderstanding that augmentations like random erasing are only for training to improve robustness, not for testing.
#2Setting erased area too large, removing most of the image.
Wrong approach:random_erasing = RandomErasing(p=1.0, scale=(0.5, 0.9), ratio=(0.5, 2.0)) # Erases half to almost all image
Correct approach:random_erasing = RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)) # Balanced erasing size
Root cause:Not tuning parameters leads to excessive information loss, making learning too hard.
#3Applying random erasing before normalization causing pixel value issues.
Wrong approach:transform = transforms.Compose([ transforms.RandomErasing(), transforms.ToTensor(), transforms.Normalize(mean, std) ])
Correct approach:transform = transforms.Compose([ transforms.ToTensor(), transforms.RandomErasing(), transforms.Normalize(mean, std) ])
Root cause:Applying random erasing before converting to tensor or normalization can cause unexpected pixel value ranges.
Key Takeaways
Random erasing is a simple but powerful data augmentation technique that hides random parts of images during training to improve model robustness.
It helps models learn to recognize objects even when parts are missing or obscured, simulating real-world occlusions.
Random erasing differs from other augmentations by focusing on localized erasure rather than global changes or noise.
Proper tuning of erasing size, shape, and probability is essential to balance learning difficulty and information preservation.
Random erasing is a form of input-level regularization that complements other techniques like dropout and Mixup for better generalization.