'Image' object has no attribute 'shape'

JakeAndFinn: <p>So I am trying to build a data augmenter from the ImageFolder loader.<br>
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.</p>
<p>This is my code on Kaggle for it<br>
<a href="https://www.kaggle.com/matthewmillar/kernel2879505a11/edit" class="onebox" target="_blank" rel="nofollow noopener">https://www.kaggle.com/matthewmillar/kernel2879505a11/edit</a></p>
<p>But here is my transforms that I would like to use</p>
<pre><code class="lang-auto">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)
])
</code></pre>
<p>And this is my dataset and loaders</p>
<pre><code class="lang-auto"># 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)
</code></pre>
<p>And this is how I am calling it</p>
<pre><code class="lang-auto">dataiter = iter(train_loader)
images, labels = dataiter.next()
</code></pre>
<p>I think everything is right but not 100% sure.<br>
Thank you for any help with this</p>

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

JakeAndFinn: <p>IT was something to do with</p>
<pre><code>transforms.RandomErasing(p=0.4, scale=(0.09, 0.25), ratio=(0.3, 3.3), value=0, inplace=False),
</code></pre>
<p>As this works fine</p>
<pre><code class="lang-auto">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)
])
</code></pre>
<p>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.</p>

ptrblck: <p>Don’t pass on it please. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"><br>
Based on the <a href="https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.RandomErasing">docs</a> it seems the method might work on tensors, so could you add it please after <code>Normalize</code> as the last transformation?</p>
<p>I’ve never used this transformation before, so I didn’t spot the error.</p>

JakeAndFinn: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>. It took a little work and trial and error but I got it working.</p>
<pre><code class="lang-auto">train_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(10),
    transforms.ToTensor(),
    transforms.Normalize(mean, std)
])
</code></pre>
<p>From this to this</p>
<pre><code class="lang-auto">train_transform = transforms.Compose([
    transforms.Resize(size),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(10),
    transforms.ToTensor()
    transforms.RandomErasing(p=0.3)
])
</code></pre>
<p>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</p>

EM_Seyfi: <p>You should not remove normalization, just put the RandomErase after ToTensor and leave the Normalize be as is.</p>