0

I'm building a CNN on Pytorch. I'm a little confused about the inputs. I'm getting the following error:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x246016 and 3136x1000)

Images are 250 x 250 in grayscale.

Can anyone take a look at my constructor and tell me where I'm going wrong? More points if you can explain to me why I was wrong and the why your answer is right! ;)

class CNN(nn.Module):

# Contructor
def __init__(self):
    super(CNN, self).__init__()
    self.cnn1 = nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2)
    self.conv1_bn = nn.BatchNorm2d(32)

    self.maxpool1=nn.MaxPool2d(kernel_size=2, stride=2)
    
    self.cnn2 = nn.Conv2d(32, 64, kernel_size=5,stride=1, padding=2)
    self.conv2_bn = nn.BatchNorm2d(64)
    self.maxpool2=nn.MaxPool2d(kernel_size=2, stride=2)  
    
    self.drop_out1 = nn.Dropout()
    self.fc1 = nn.Linear(7 * 7 * 64, 1000)
    self.bn_fc1 = nn.BatchNorm2d(1000)
    
    self.fc2 = nn.Linear(1000, 1)
    

    

# Prediction
def forward(self, x):
    x = self.cnn1(x)
    x = self.conv1_bn(x)
    x = torch.relu(x)
    x = self.maxpool1(x)
    x = self.cnn2(x)
    x = self.conv2_bn(x)
    x = torch.relu(x)
    x = self.maxpool2(x)
    x = x.view(x.size(0), -1)
    x = self.drop_out1(x)
    x = self.fc1(x)
    x = self.bn_fc1(x)
    x = torch.relu(x)
    x = self.fc2(x)
    x = torch.sigmoid(x)
   
    return x
  • 1
    Hi, I would take a look at the output shape of x.view(x.size(0), -1), to me it looks like your fc1 layer expects a different shape than it gets. Meaning the output of you conv/maxpool part should be bigger than `7 x 7`. – Chillston Dec 17 '21 at 00:34
  • 1
    I agree with @chillston, maybe try putting `torch.nn.AdaptiveAvgPool2d(7)` (or `torch.nn.AdaptiveMaxPool2d(7)` if you're not sure what the dimensions of the previous layer should be. – jhso Dec 17 '21 at 01:13

1 Answers1

0

Your fc1 layer is expecting a tensor of shape (-1, 7*7*64) but you are passing it a tensor of shape [-1, 246016] (-1 being the batch size).

To calculate output size of conv nets see this post or any neural networks textbook.

Ian Benlolo
  • 138
  • 1
  • 7