AttributeError: 'Image' object has no attribute 'shape'

I tried to apply RandomErase transform for every images using the following code

my_transforms = transforms.Compose([




  transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))                                    


dataset = datasets.ImageFolder(root=DATA_PATH, transform=my_transforms)

I tried to display first image from the dataset using following line


And I get this error

AttributeError: 'Image' object has no attribute 'shape'

You need to send the

dataset = datasets.ImageFolder(root=DATA_PATH, transform=transform)
data_loader =, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs)

You can iterate over the dataset to find the instances

image_sample = next.itere(data_loader)

Still getting the same error. If I remove transforms.RandomErasing(p=1) from my transforms it works. But I need to put random erasing each image.

RandomErasing is a transform on a Tensor, not an Image, per the docs.

You should try again with the transforms in this order:


Hope this helps!

Yes, that works. Thanks a lot.