'Image' object has no attribute 'shape'

So I am trying to build a data augmenter from the ImageFolder loader.
But what I have is giving me the ‘Image’ object has no attribute ‘shape’ error. I think I have everything correct but not sure about it.

This is my code on Kaggle for it
https://www.kaggle.com/matthewmillar/kernel2879505a11/edit

But here is my transforms that I would like to use

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
size = (224,224)

train_transform = transforms.Compose([
    transforms.Resize(224),
    transforms.RandomHorizontalFlip(),
    transforms.RandomErasing(p=0.4, scale=(0.09, 0.25), ratio=(0.3, 3.3), value=0, inplace=False),
    transforms.RandomRotation(45),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])

val_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])

And this is my dataset and loaders

# Make image loaders

# Hyperparmas
batch_size = 32
num_workers = 0

train_dataset = torchvision.datasets.ImageFolder(TRAIN_SET, transform=train_transform)
test_dataset = torchvision.datasets.ImageFolder(TEST_SET, transform=val_transform)

train_loader = torch.utils.data.DataLoader(train_dataset, 
                                           batch_size=batch_size, 
                                           shuffle=True, 
                                           num_workers=num_workers)

test_loader = torch.utils.data.DataLoader(test_dataset,
                                         batch_size=batch_size,
                                         shuffle=True,
                                         num_workers=num_workers)

And this is how I am calling it

dataiter = iter(train_loader)
images, labels = dataiter.next()

I think everything is right but not 100% sure.
Thank you for any help with this

Could you post the complete stack trace please, so that we can have a look where this error is raised?
Based on the error message it seems as if the transformations are not applied.

1 Like

IT was something to do with

transforms.RandomErasing(p=0.4, scale=(0.09, 0.25), ratio=(0.3, 3.3), value=0, inplace=False),

As this works fine

train_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(10),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])

val_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])

I have seen on stack overflow that this can cause some issues and a way to work with it is to go into the source code and change size to shape to get it to work. I dont feel comfertable with that so I will pass.

Don’t pass on it please. :wink:
Based on the docs it seems the method might work on tensors, so could you add it please after Normalize as the last transformation?

I’ve never used this transformation before, so I didn’t spot the error.

1 Like

Thank you @ptrblck. It took a little work and trial and error but I got it working.

train_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(10),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])

From this to this

train_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(10),
    transforms.ToTensor()
    transforms.RandomErasing(p=0.3)
])

I had to remove the normalization for it to work I think, but as you mention putting it on the end after the tensor seems to do the trick thank you

1 Like

You should not remove normalization, just put the RandomErase after ToTensor and leave the Normalize be as is.

2 Likes