Why random erasing should be applied as Post transfromation

Hello everyone.I really appreciate this community bcz everyone replies so fast.So my doubt is i am applying data augumentation where i found about random erasing .In actual documentation

> transform = transforms.Compose([
>>> transforms.RandomHorizontalFlip(),
>>> transforms.ToTensor(),
>>> transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
>>> transforms.RandomErasing(),
>>> ])

Here why random erasing is applied after ToTensor() and ToNormalize() functions.
When i applied random erasing before ToTensor() and ToNormalize() it is giving me an error like image object has no attribute size.Help will be greately appreciated

1 Like

The implementation of F.erase is quite simple as seen here and doesn’t need PIL or any other image library as the backend, which is most likely why it was implemented for tensors directly.

@ptrblck @Sanjayvarma11
resize = transforms.Resize(size=(INPUT_HEIGHT,
INPUT_WIDTH))
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
DATA_TRANSFORMS = {
‘train’:
transforms.Compose([
transforms.Resize(size=(256,256)),
transforms.RandomCrop(INPUT_HEIGHT,INPUT_WIDTH),
transforms.RandomHorizontalFlip(p=0.4),
transforms.RandomVerticalFlip(p=0.6),
transforms.ToTensor(),
transforms.RandomErasing(p=0.5),
normalize
]),

I put random erasing after ToTensor but before normalize, and it solves the error like ‘AttributeError: ‘Image‘ object has no attribute ‘shape’.

Should we put this RandomErasing after or before normalize? and Why?

I don’t think it would matter if the RandomErasing transformation is applied before or after the normalization, since:

  1. RandomErasing doesn’t depend on the input values and will just erase patches from it
  2. Normalize doesn’t depend on the input values as the mean and stddev are already provided.

I see, thanks for your reply. :blush: