Home > other >  RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x73034 and 200x120)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x73034 and 200x120)

Time:02-02

Building a Neural Network layers for Skin detection dataset, and got a error here. I know i have done some mistake but cannot figure it out. Error is am getting is after taking image size 224*224 and channels 3: RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x73034 and 200x120)

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

class Net(nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(3, 16, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(16, 26, 5)
    self.fc1 = nn.Linear(8 * 5 * 5, 120)
    self.fc2 = nn.Linear(120, 86)
    self.fc3 = nn.Linear(86, 2)

  def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = torch.flatten(x,1)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

net = Net().to(device)
print(net)

These are the layers and Net module

<ipython-input-41-8c9bafb31c44> in forward(self, x)
     16     x = self.pool(F.relu(self.conv2(x)))
     17     x = torch.flatten(x,1)
---> 18     x = F.relu(self.fc1(x))
     19     x = F.relu(self.fc2(x))
     20     x = self.fc3(x)

Can anyone help me solve this.

CodePudding user response:

As Anant said, you need to match the flattened conv2 dimension (73034) to be the input dimension for the fc1 layer.

self.fc1 = nn.Linear(73034, 120)

The formula to calculate the output of each conv layer:

[(height or width) - kernel size   2*padding] / stride   1

For the following I will use the dimensions (Channels, Height, Width) Input (3,224,224) -> conv1 -> (16,220,220) -> pool -> (16,110,110) -> conv2 -> (26,106,106) -> pool -> (26,53,53) -> flatten -> (73034)

It seems your batch size is 4, which refers to the "4" in (4x73034). If you print the dimensions of the output of conv1 or conv2 layers, the format will be (Batch, Channels, Height, Width).

CodePudding user response:

You have a mismatch between the output of torch.flatten and the input to self.fc1

Print the shape of the output of torch.flatten

x = torch.flatten(x,1)
print(x.size())

and subsequently update the definition of the self.fc1

self.fc1 = nn.Linear(8 * 5 * 5, 120)
  •  Tags:  
  • Related