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.
RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False)
RandomErasing(p=0.5)RandomErasing(p=1.0, scale=(0.1, 0.2), ratio=(1, 1), value=0.5)
RandomErasing(p=0.3, inplace=True)
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]}")
random erasing in computer vision training?RandomErasing in torchvision.transforms.p, scale, and ratio are valid for RandomErasing.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?p=1.0, random erasing always applies. The erased area is set to zero by default.output[:, 4:6, 4:6] (center 2x2) is always covered by the large erased area, so it will be zeros.transform = transforms.RandomErasing(p=0.5)
image = Image.open('cat.jpg')
output = transform(image)transforms.ToTensor() before RandomErasing causes type error.ToTensor().Pass the test and this pattern moves unknown