Bird
Raised Fist0
Computer Visionml~20 mins

Random erasing in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Random Erasing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of random erasing in image data augmentation?

Random erasing is a technique used in training image models. What does it mainly help with?

AIt helps the model learn to ignore small occlusions or missing parts in images, improving robustness.
BIt increases the image resolution by adding random pixels.
CIt converts images to grayscale to reduce color bias.
DIt crops the image to a fixed size to standardize input.
Attempts:
2 left
💡 Hint

Think about how hiding parts of an image during training might help the model.

❓ Predict Output
intermediate
2:00remaining
What is the output shape after applying random erasing to a batch of images?

Given a batch of 16 RGB images of size 64x64, what will be the shape of the batch after applying random erasing augmentation?

Computer Vision
import torch
import torchvision.transforms as transforms

transform = transforms.Compose([
    transforms.RandomErasing(p=1.0, scale=(0.02, 0.33), ratio=(0.3, 3.3))
])

batch = torch.randn(16, 3, 64, 64)  # batch of 16 images
augmented_batch = torch.stack([transform(img) for img in batch])
print(augmented_batch.shape)
Atorch.Size([16, 3, 64, 64])
Btorch.Size([16, 3, 32, 32])
Ctorch.Size([16, 1, 64, 64])
Dtorch.Size([3, 64, 64])
Attempts:
2 left
💡 Hint

Random erasing modifies pixels but does not change image size or batch size.

❓ Hyperparameter
advanced
1:30remaining
Which hyperparameter controls the size of the erased area in random erasing?

In random erasing, which hyperparameter defines the proportion of the image area that can be erased?

Amean
Bratio
Cp
Dscale
Attempts:
2 left
💡 Hint

Think about which parameter sets the size range of the erased rectangle.

❓ Metrics
advanced
1:30remaining
How does random erasing typically affect model accuracy on test data?

When random erasing is applied during training, what is the usual effect on the model's accuracy on unseen test images?

AIt usually decreases accuracy because it removes important image parts.
BIt has no effect on accuracy since it only changes training images.
CIt usually improves accuracy by making the model more robust to occlusions.
DIt causes the model to overfit the training data.
Attempts:
2 left
💡 Hint

Consider how training with harder examples affects generalization.

🔧 Debug
expert
2:00remaining
What error does this random erasing code raise?

Consider this code snippet using torchvision's RandomErasing. What error will it raise?

Computer Vision
import torchvision.transforms as transforms

transform = transforms.RandomErasing(p=1.0, scale=(0.5, 0.1))

# Applying transform to a tensor image
import torch
img = torch.randn(3, 32, 32)
transformed_img = transform(img)
ARuntimeError: input tensor must be 4D
BValueError: scale should be a tuple (min, max) with min <= max
CTypeError: RandomErasing expects a PIL Image, not a tensor
DNo error, code runs successfully
Attempts:
2 left
💡 Hint

Check the order of values in the scale tuple.

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