RuntimeError: shape '[-1, 50176]' is invalid for input of size 66304

I am trying to setup a CNN model. My images are 224x224.

import torch.nn as nn
import torch.nn.functional as F

# define the CNN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # convolutional layer
        self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
        # max pooling layer
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(64 * 28*28 , 500)
        # linear layer (500 -> 10)
        self.fc2 = nn.Linear(500, 10)
        # dropout layer (p=0.25)
        self.dropout = nn.Dropout(0.25)
        
    def forward(self, x):
        # add sequence of convolutional and max pooling layers
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        # flatten image input
        print(x.shape)
        x = x.view(-1, 64 * 28*28 )
        print(x.shape)
        # add dropout layer
        x = self.dropout(x)
        # add 1st hidden layer, with relu activation function
        x = F.relu(self.fc1(x))
        # add dropout layer
        x = self.dropout(x)
        # add 2nd hidden layer, with relu activation function
        x = self.fc2(x)
        return x
    
# instantiate the CNN
model_scratch = Net()
print(model_scratch)
# move tensors to GPU if CUDA is available
if use_cuda:
    model_scratch.cuda()

My Train and Validation loop:

# the following import is required for training to be robust to truncated images
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

def train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path):
    """returns trained model"""
    # initialize tracker for minimum validation loss
    valid_loss_min = np.Inf 
    
    for epoch in range(1, n_epochs+1):
        # initialize variables to monitor training and validation loss
        train_loss = 0.0
        valid_loss = 0.0
        print("====================EPOCHE", epoch)
        ###################
        # train the model #
        ###################
        model.train()
        print('model trained')
        for batch_idx, (data, target) in enumerate(loaders['train']):
            print("Loop over Train", batch_idx)
            print("USE_CUDA",use_cuda)
            # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            # clear the gradients of all optimized variables
            optimizer.zero_grad()
            # forward pass: compute predicted outputs by passing inputs to the model
            output = model(data)
            # calculate the batch loss
            loss = criterion(output, target)
            # backward pass: compute gradient of the loss with respect to model parameters
            loss.backward()
            # perform a single optimization step (parameter update)
            optimizer.step()
            # update training loss
            train_loss += loss.item()*data.size(0)
            ## find the loss and update the model parameters accordingly
            ## record the average training loss, using something like
            ## train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
            
        ######################    
        # validate the model #
        ######################
        model.eval()
        for batch_idx, (data, target) in enumerate(loaders['valid']):
            # move to GPU
            print("USE_CUDA2",use_cuda)
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## update the average validation loss
            # forward pass: compute predicted outputs by passing inputs to the model
            output = model(data)
            # calculate the batch loss
            loss = criterion(output, target)
            # update average validation loss 
            valid_loss += loss.item()*data.size(0)

        
        # calculate average losses
        train_loss = train_loss/len(train_loader.dataset)
        valid_loss = valid_loss/len(valid_loader.dataset)
        
        
        # print training/validation statistics 
        print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
            epoch, train_loss, valid_loss))

        # save model if validation loss has decreased
        if valid_loss <= valid_loss_min:
            print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
            valid_loss_min,
            valid_loss))
            torch.save(model.state_dict(), 'model_scratch.pt')
            valid_loss_min = valid_loss
            
    # return trained model
    return model


# train the model
model_scratch = train(100, loader_scratch, model_scratch, optimizer_scratch, 
                      criterion_scratch, use_cuda, 'model_scratch.pt')

# load the model that got the best validation accuracy
model_scratch.load_state_dict(torch.load('model_scratch.pt'))

I get the above error, and don’t really understand how to flatten my data right:

torch.Size([1, 64, 37, 28])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-70-ef616b23c264> in <module>
     81 # train the model
     82 model_scratch = train(100, loader_scratch, model_scratch, optimizer_scratch, 
---> 83                       criterion_scratch, use_cuda, 'model_scratch.pt')
     84 
     85 # load the model that got the best validation accuracy

<ipython-input-70-ef616b23c264> in train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path)
     27             optimizer.zero_grad()
     28             # forward pass: compute predicted outputs by passing inputs to the model
---> 29             output = model(data)
     30             # calculate the batch loss
     31             loss = criterion(output, target)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

<ipython-input-67-ebe7a14cbba0> in forward(self, x)
     26         # flatten image input
     27         print(x.shape)
---> 28         x = x.view(-1, 64 * 28*28 )
     29         print(x.shape)
     30         # add dropout layer

RuntimeError: shape '[-1, 50176]' is invalid for input of size 66304

Ok, You got the error because you were trying to resize the data into a wrong shape. Try this,

x = x.view(x.size(0), -1) 

keeping the batch size the same, and flattening out the remaining dimension.

Thanks for the reply, when I reshape it like you said, I get this error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-17-ef616b23c264> in <module>
     81 # train the model
     82 model_scratch = train(100, loader_scratch, model_scratch, optimizer_scratch, 
---> 83                       criterion_scratch, use_cuda, 'model_scratch.pt')
     84 
     85 # load the model that got the best validation accuracy

<ipython-input-17-ef616b23c264> in train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path)
     27             optimizer.zero_grad()
     28             # forward pass: compute predicted outputs by passing inputs to the model
---> 29             output = model(data)
     30             # calculate the batch loss
     31             loss = criterion(output, target)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

<ipython-input-16-8e80a47d55dc> in forward(self, x)
     31         x = self.dropout(x)
     32         # add 1st hidden layer, with relu activation function
---> 33         x = F.relu(self.fc1(x))
     34         # add dropout layer
     35         x = self.dropout(x)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~\Anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     90     @weak_script_method
     91     def forward(self, input):
---> 92         return F.linear(input, self.weight, self.bias)
     93 
     94     def extra_repr(self):

~\Anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1404     if input.dim() == 2 and bias is not None:
   1405         # fused op is marginally faster
-> 1406         ret = torch.addmm(bias, input, weight.t())
   1407     else:
   1408         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [1 x 53760], m2: [50176 x 500] at C:/w/1/s/tmp_conda_3.7_044431/conda/conda-bld/pytorch_1556686009173/work/aten/src\THC/generic/THCTensorMathBlas.cu:268

I then realized that the shape of my images are not proportional 1:1, I changed the transform pipe like this:

min_img_size = 224  # The min size, as noted in the PyTorch pretrained models doc, is 224 px.
transform_pipeline = transforms.Compose([transforms.Resize((min_img_size, min_img_size)),
                                     transforms.ToTensor(),
                                     transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                          std=[0.229, 0.224, 0.225])])

But now got this error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-20-ef616b23c264> in <module>
     81 # train the model
     82 model_scratch = train(100, loader_scratch, model_scratch, optimizer_scratch, 
---> 83                       criterion_scratch, use_cuda, 'model_scratch.pt')
     84 
     85 # load the model that got the best validation accuracy

<ipython-input-20-ef616b23c264> in train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path)
     31             loss = criterion(output, target)
     32             # backward pass: compute gradient of the loss with respect to model parameters
---> 33             loss.backward()
     34             # perform a single optimization step (parameter update)
     35             optimizer.step()

~\Anaconda3\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
    105                 products. Defaults to ``False``.
    106         """
--> 107         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    108 
    109     def register_hook(self, hook):

~\Anaconda3\lib\site-packages\torch\autograd\__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     91     Variable._execution_engine.run_backward(
     92         tensors, grad_tensors, retain_graph, create_graph,
---> 93         allow_unreachable=True)  # allow_unreachable flag
     94 
     95 

RuntimeError: cublas runtime error : the GPU program failed to execute at C:/w/1/s/tmp_conda_3.7_044431/conda/conda-bld/pytorch_1556686009173/work/aten/src/THC/THCBlas.cu:25