Neural network training error

I m trying to train a convolutional neural network and i’m getting an error, and i don’t know how to solve it

Network:

class CNN(nn.Module):
def init(self):
super(CNN, self).init()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3)
self.conv2 = nn.Conv2d(10, 20, kernel_size=3)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(720, 1024)
self.fc2 = nn.Linear(1024, 2)

def forward(self, x):
    x = F.relu(F.max_pool2d(self.conv1(x), 2))
    x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
    x = x.view(x.size(0), -1)
    x = F.relu(self.fc1(x))
    x = F.dropout(x, training=self.training)
    x = self.fc2(x)
    return x

Error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (25x1615440 and 720x1024)

What size image are you putting in?

The error is occurring because the output of your convolutions and max pooling do not match the input size of your fc1 Linear layer. A print statement should show this. Either you should run an adaptive pooling layer to reduce the size of the convolution outputs to 6x6, or you can increase the size of your Linear layer input to 1615440.

1 Like

This is the shape of my images
torch.Size([3, 1024, 1280])

Try this:

class CNN(nn.Module):
    def **init**(self):
        super(CNN, self).**init**()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=3)
        self.conv2_drop = nn.Dropout2d()
        self.avg_pool=nn.AdaptiveAvgPool2d((6,6)) #adapative pooling
        self.fc1 = nn.Linear(720, 1024)
        self.fc2 = nn.Linear(1024, 2)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x=self.avg_pool(x) #apply
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return x

Note: The above should only prevent the error and allow some inference.

For reference:
https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html