`*** RuntimeError: mat1 dim 1 must match mat2 dim 0` whenever I run model(images)

梦想的初衷 提交于 2021-02-19 05:02:47

问题


    def __init__(self):
        super().__init__()

        self.conv = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=5, stride=2, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),

            nn.Conv2d(64, 64, kernel_size=3, stride=2, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            
            nn.Conv2d(64, 64, kernel_size=3, stride=2, bias=False),
            nn.BatchNorm2d(64),
  
        )

How can I deal with this error? I think the error is with self.fc, but I can't say how to fix it.


回答1:


The output from self.conv(x) is of shape torch.Size([32, 64, 2, 2]): 32*64*2*2= 8192 (this is equivalent to (self.conv_out_size). The input to fully connected layer expects a single dimension vector i.e. you need to flatten it before passing to a fully connected layer in the forward function.

i.e.

class Network():
    ...
    def foward():
    ...
        conv_out = self.conv(x)
        print(conv_out.shape)
        conv_out = conv_out.view(-1, 32*64*2*2)
        print(conv_out.shape)
        x = self.fc(conv_out)
        return x

output

torch.Size([32, 64, 2, 2])
torch.Size([1, 8192])

EDIT:

I think you're using self._get_conv_out function wrong.

It should be

    def _get_conv_out(self, shape):
        output = self.conv(torch.zeros(1, *shape)) # not (32, *size)
        return int(numpy.prod(output.size()))

then, in the forward pass, you can use

        conv_out = self.conv(x)
        # flatten the output of conv layers
        conv_out = conv_out.view(conv_out.size(0), -1)
        x = self.fc(conv_out)

For an input of (32, 1, 110, 110), the output should be torch.Size([32, 2]).




回答2:


I had the same problem however I have solved it by using a batch of 32 and tensor size of [3, 32, 32] for my images and the following configurations on my model. I am using ResNet with 9 CNN and looking for 4 outputs.

transform = transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor()])

def conv_block(in_channels, out_channels, pool=False):
    layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), 
              nn.BatchNorm2d(out_channels), 
              nn.ReLU(inplace=True)]
    if pool: layers.append(nn.MaxPool2d(2))
    return nn.Sequential(*layers)
class ResNet9(ImageClassificationBase):
    def __init__(self, in_channels, num_classes):
        super().__init__()
        
        self.conv1 = conv_block(in_channels, 64)
        self.conv2 = conv_block(64, 128, pool=True)
        self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))
        
        self.conv3 = conv_block(128, 256, pool=True)
        self.conv4 = conv_block(256, 512, pool=True)
        self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))
        
        self.classifier = nn.Sequential(nn.MaxPool2d(4), 
                                        nn.Flatten(), 
                                        nn.Dropout(0.2),
                                        nn.Linear(512, num_classes))    
    def forward(self, xb):
        out = self.conv1(xb)
        out = self.conv2(out)
        out = self.res1(out) + out
        out = self.conv3(out)
        out = self.conv4(out)
        out = self.res2(out) + out
        out = self.classifier(out)
        return out


来源:https://stackoverflow.com/questions/64868040/runtimeerror-mat1-dim-1-must-match-mat2-dim-0-whenever-i-run-modelimages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!