Bird
Raised Fist0
Computer Visionml~20 mins

Random erasing in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Random erasing
Problem:You are training an image classifier on a small dataset. The model achieves 95% training accuracy but only 70% validation accuracy.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Training loss: 0.15, Validation loss: 0.85
Issue:The model is overfitting. It performs very well on training data but poorly on unseen validation data.
Your Task
Reduce overfitting by applying random erasing data augmentation to improve validation accuracy to at least 80% while keeping training accuracy below 92%.
You can only add random erasing augmentation to the training data pipeline.
Do not change the model architecture or optimizer settings.
Keep the number of training epochs and batch size the same.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import torch
import torchvision
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim

# Define transforms with random erasing for training
train_transforms = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
    transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value='random'),
])

val_transforms = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
])

# Load dataset (e.g., CIFAR10 for example)
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=train_transforms)
val_dataset = datasets.CIFAR10(root='./data', train=False, download=True, transform=val_transforms)

train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=64, shuffle=False)

# Simple CNN model
class SimpleCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
        self.fc1 = nn.Linear(64 * 8 * 8, 128)
        self.fc2 = nn.Linear(128, 10)
        self.relu = nn.ReLU()

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = x.view(-1, 64 * 8 * 8)
        x = self.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Initialize model, loss, optimizer
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
num_epochs = 10
for epoch in range(num_epochs):
    model.train()
    running_loss = 0.0
    correct = 0
    total = 0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item() * inputs.size(0)
        _, predicted = outputs.max(1)
        total += labels.size(0)
        correct += predicted.eq(labels).sum().item()

    train_loss = running_loss / total
    train_acc = 100. * correct / total

    model.eval()
    val_loss = 0.0
    val_correct = 0
    val_total = 0
    with torch.no_grad():
        for inputs, labels in val_loader:
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            val_loss += loss.item() * inputs.size(0)
            _, predicted = outputs.max(1)
            val_total += labels.size(0)
            val_correct += predicted.eq(labels).sum().item()

    val_loss /= val_total
    val_acc = 100. * val_correct / val_total

    print(f'Epoch {epoch+1}/{num_epochs} - Train loss: {train_loss:.3f}, Train acc: {train_acc:.2f}%, Val loss: {val_loss:.3f}, Val acc: {val_acc:.2f}%')
Added RandomErasing transform with 50% probability to the training data pipeline.
Kept model architecture, optimizer, batch size, and epochs unchanged.
Results Interpretation

Before: Training accuracy 95%, Validation accuracy 70%, Training loss 0.15, Validation loss 0.85

After: Training accuracy 90%, Validation accuracy 82%, Training loss 0.30, Validation loss 0.60

Random erasing helps reduce overfitting by making the model see images with random parts erased during training. This forces the model to learn more robust features, improving validation accuracy while slightly lowering training accuracy.
Bonus Experiment
Try combining random erasing with other augmentations like random rotation and color jitter to see if validation accuracy improves further.
💡 Hint
Add transforms.RandomRotation and transforms.ColorJitter before RandomErasing in the training pipeline.

Practice

(1/5)
1. What is the main purpose of random erasing in computer vision training?
easy
A. To hide random parts of images to improve model robustness
B. To increase image resolution during training
C. To convert images to grayscale
D. To add noise to the entire image

Solution

  1. Step 1: Understand random erasing concept

    Random erasing hides random parts of images during training to simulate missing or blocked areas.
  2. Step 2: Identify the main goal

    This helps the model learn to recognize objects even if parts are missing, improving robustness.
  3. Final Answer:

    To hide random parts of images to improve model robustness -> Option A
  4. Quick Check:

    Random erasing = hiding parts for robustness [OK]
Hint: Random erasing hides parts, not changes resolution or color [OK]
Common Mistakes:
  • Confusing random erasing with image resizing
  • Thinking it adds noise to the whole image
  • Believing it converts images to grayscale
2. Which of the following is the correct way to apply random erasing using PyTorch's torchvision transforms?
easy
A. transforms.RandomErase(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3))
B. transforms.RandomErased(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3))
C. transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3))
D. transforms.RandomEraseArea(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3))

Solution

  1. Step 1: Recall PyTorch transform name

    The correct class name is RandomErasing in torchvision.transforms.
  2. Step 2: Check method parameters

    Parameters like p, scale, and ratio are valid for RandomErasing.
  3. Final Answer:

    transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)) -> Option C
  4. Quick Check:

    Correct class name and parameters = transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)) [OK]
Hint: Remember exact class name: RandomErasing (no spaces or misspellings) [OK]
Common Mistakes:
  • Misspelling the class name as RandomErase or RandomErased
  • Using incorrect parameter names
  • Confusing with other augmentation transforms
3. Consider this PyTorch code snippet applying random erasing:
import torch
import torchvision.transforms as transforms
transform = transforms.Compose([
    transforms.RandomErasing(p=1.0, scale=(0.9, 0.9), ratio=(1, 1))
])
image = torch.ones((3, 10, 10))
output = transform(image)
print(output[:, 4:6, 4:6])
What will be the printed output?
medium
A. An error because RandomErasing expects PIL images
B. A 3x2x2 tensor of zeros
C. A 3x2x2 tensor with random values
D. A 3x2x2 tensor of ones

Solution

  1. Step 1: Understand RandomErasing behavior

    With p=1.0, random erasing always applies. The erased area is set to zero by default.
  2. Step 2: Analyze scale and ratio

    Scale=(0.9, 0.9) and ratio=(1, 1) fix the erased area to 90% of image size (~9x9 block) at random location near edge.
  3. Step 3: Check printed slice

    The printed slice output[:, 4:6, 4:6] (center 2x2) is always covered by the large erased area, so it will be zeros.
  4. Final Answer:

    A 3x2x2 tensor of zeros -> Option B
  5. Quick Check:

    RandomErasing sets erased area to zero [OK]
Hint: Erased area pixels become zero tensors [OK]
Common Mistakes:
  • Assuming erased area keeps original values
  • Thinking erased area has random noise
  • Believing RandomErasing only works on PIL images
4. You wrote this code to apply random erasing but get an error:
transform = transforms.RandomErasing(p=0.5)
image = Image.open('cat.jpg')
output = transform(image)

What is the likely cause of the error?
medium
A. RandomErasing requires grayscale images only
B. The probability p must be 1.0
C. Image file path is incorrect
D. RandomErasing expects a tensor, not a PIL image

Solution

  1. Step 1: Check input type for RandomErasing

    RandomErasing in torchvision expects a tensor input, not a PIL image.
  2. Step 2: Identify missing transform step

    Missing transforms.ToTensor() before RandomErasing causes type error.
  3. Final Answer:

    RandomErasing expects a tensor, not a PIL image -> Option D
  4. Quick Check:

    RandomErasing input = tensor, not PIL [OK]
Hint: Always convert PIL image to tensor before RandomErasing [OK]
Common Mistakes:
  • Passing PIL images directly to RandomErasing
  • Assuming p must be 1.0 to work
  • Thinking RandomErasing only works on grayscale
5. You want to improve your image classifier's robustness by combining random erasing with another augmentation. Which combination below is best practice?
hard
A. Use random erasing after converting images to tensors and normalize them
B. Apply random erasing before resizing images to fixed size
C. Apply random erasing on raw PIL images without any other transforms
D. Use random erasing only during testing, not training

Solution

  1. Step 1: Understand transform order

    Random erasing requires tensor input, so it must come after ToTensor().
  2. Step 2: Combine with normalization

    Normalization after tensor conversion is standard; random erasing fits well after these steps during training.
  3. Step 3: Avoid applying random erasing before resizing or during testing

    Applying before resizing can cause errors; random erasing is a training augmentation only.
  4. Final Answer:

    Use random erasing after converting images to tensors and normalize them -> Option A
  5. Quick Check:

    Random erasing after ToTensor and normalize = best practice [OK]
Hint: Random erasing after ToTensor and normalize during training [OK]
Common Mistakes:
  • Applying random erasing on raw PIL images
  • Using random erasing during testing phase
  • Applying random erasing before resizing images
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard