RuntimeError: shape '[-1, 44944]' is invalid for input of size 1628160

DJ_1992: <p>Hi,<br>
I am trying to run a basic CNN for binary classification of medical images. I am getting this error:</p>
<pre><code>x = x.view(-1, 16 * 53 * 53)
</code></pre>
<p>RuntimeError: shape ‘[-1, 44944]’ is invalid for input of size 1628160</p>
<p>CNN:</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 6, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 53 * 53, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 2)</p>
<pre><code>def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1, 16 * 53 * 53)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x
</code></pre>
<p>Images - 3x224X224<br>
Batch Size - 32<br>
Though it works with x = x.view(-1, 16 * 53 * 60). But I don’t understand it why it wouldn’t work with 53*53.<br>
I am new to Pytorch and if anyone can help me out I really appreciate it…</p>

IliasPap: <p>Firstly , your init function should be renamed to</p>
<pre><code class="lang-auto">    def __init__(self):
        super(Net, self).__init__()
</code></pre>
<p>I tested it works perfectly for me with</p>
<pre><code class="lang-auto">    x = x.view(-1, 16 * 53 * 53)
</code></pre>

DJ_1992: <p>I dont know why it is still not working for me. Here is my transforms and dataloader if it helps…</p>
<p>train_dir = ‘train’<br>
valid_dir = ‘valid’<br>
test_dir = ‘test’</p>
<p>dirs = {<br>
‘train’: train_dir,<br>
‘valid’: valid_dir,<br>
‘test’: test_dir<br>
}</p>
<h1>Data Transforms:</h1>
<p>data_transforms = {<br>
‘train’: transforms.Compose([</p>
<pre><code>    transforms.RandomRotation(40),
    transforms.RandomHorizontalFlip(),
    transforms.RandomVerticalFlip(),
    transforms.ColorJitter(brightness = [1,1.2]),
    transforms.RandomAffine(20,translate =(0.1,0.1),scale=(0.9,1.25)),
    transforms.Resize(224),

    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], 
                                        [0.229, 0.224, 0.225])
]),
'valid': transforms.Compose([

    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], 
                                        [0.229, 0.224, 0.225])
]),
</code></pre>
<p>}</p>
<h1>Load the datasets with ImageFolder - No testset at the moment</h1>
<p>image_datasets = {x: datasets.ImageFolder(dirs[x],   transform=data_transforms[x]) for x in [‘train’, ‘valid’]}</p>
<h1>load the data into batches</h1>
<p>dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x],drop_last=True, batch_size=32, shuffle=True) for x in [‘train’, ‘valid’]}<br>
dataset_sizes = {x: len(image_datasets[x])<br>
for x in [‘train’, ‘valid’]}</p>
<p>class_names = image_datasets[‘train’].classes</p>

IliasPap: <aside class="quote no-group" data-username="DJ_1992" data-post="3" data-topic="72599">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/bbe5ce/48.png" class="avatar"> DJ_1992:</div>
<blockquote>
<p>(), transforms.ColorJitter(brightness = [1,1.2]),</p>
</blockquote>
</aside>
<p>Check the shape of your image after the  transformations , maybe the output size is not 224x224 and there is a problem with view( )</p>

DJ_1992: <p>Thanks, it worked…<br>
I was using transforms.Resize(224) instead of transforms.Resize([224,224])</p>