CNN matrix shape issue when adding new convolutional layer

I have been adding more & more convolutional layers to my model to see how they effect model size & accuracy. It was all going fine until I added my 6th convolutional layer.

class MyCNN(nn.Module):

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

        # convolution layers
        self._body = nn.Sequential(
            
            # Conv - 1
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(64, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

            # Conv - 2
            nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(128, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

            # Conv - 3
            nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(256, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

            # Conv - 4
            nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(512, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

            # Conv - 5
            nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(1024, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

            # Conv - 6                
            nn.Conv2d(in_channels=1024, out_channels=2048, kernel_size=3),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(2048, momentum=0.3),
            nn.MaxPool2d(kernel_size=2),

        )

        # Fully connected layers 
        self._head = nn.Sequential(

            # nn.Linear(in_features=128*54*54, out_features=512),
            # nn.Linear(in_features=512*12*12, out_features=512),
            # nn.Linear(in_features=1024*5*5, out_features=512),
            nn.Linear(in_features=2048*2*2, out_features=512),
            nn.ReLU(inplace=True),
            nn.Linear(in_features=512, out_features=7)

        )

    def forward(self, x):

        x = self._body(x)
        x = x.view(x.size()[0], -1)
        x = self._head(x)
        
        return x

As can be seen in the code above I have been adding a conv-layer then recalculating the length of the vector to put into the fully connected layers. For each successive addition the calculations have been right & the model trains. But now I’ve added the 6th layer I get the following error,

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x2048 and 8192x512)

Why has it decided that my input feature vector is 32*2048 and not 2048*2*2 that I have coded?

Hi Dr!

It works for me. Maybe check the code you actually ran for typos.

Here’s a test script that incorporates your code:

import torch
print (torch.__version__)

import torch.nn as nn

_ = torch.manual_seed (2024)

class MyCNN(nn.Module):
    
    def __init__ (self):
        super().__init__()
        
        # convolution layers
        self._body = nn.Sequential(
            
            # Conv - 1
            nn.Conv2d (in_channels = 3, out_channels = 64, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (64, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
            # Conv - 2
            nn.Conv2d (in_channels = 64, out_channels = 128, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (128, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
            # Conv - 3
            nn.Conv2d (in_channels = 128, out_channels = 256, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (256, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
            # Conv - 4
            nn.Conv2d (in_channels = 256, out_channels = 512, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (512, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
            # Conv - 5
            nn.Conv2d (in_channels = 512, out_channels = 1024, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (1024, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
            # Conv - 6                
            nn.Conv2d (in_channels = 1024, out_channels = 2048, kernel_size = 3),
            nn.ReLU (inplace = True),
            nn.BatchNorm2d (2048, momentum = 0.3),
            nn.MaxPool2d (kernel_size = 2),
            
        )
        
        # Fully connected layers 
        self._head = nn.Sequential(
            
            # nn.Linear (in_features = 128*54*54, out_features = 512),
            # nn.Linear (in_features = 512*12*12, out_features = 512),
            # nn.Linear (in_features = 1024*5*5, out_features = 512),
            nn.Linear (in_features = 2048*2*2, out_features = 512),
            nn.ReLU (inplace = True),
            nn.Linear (in_features = 512, out_features = 7)
            
        )
    
    def forward (self, x):
        
        x = self._body (x)
        x = x.view (x.size()[0], -1)
        x = self._head (x)
        
        return x


myCNN = MyCNN()

t = torch.randn (32, 3, 256, 256)
print ('t.shape =', t.shape)

u = myCNN (t)
print ('u.shape =', u.shape)

And here is its output:

2.2.1
t.shape = torch.Size([32, 3, 256, 256])
u.shape = torch.Size([32, 7])

Best.

K. Frank

hmmmm… how odd. I’ll check it over again when I’m back in tomorrow

Nope its not having it.

Rewrote the model to eliminate typos but it keeps insisting that I’m trying to use a 32 x 2048 and not a 2 x 2 x 2048

I go the dimensions of the last conv layer wrong, I was expecting 2048 x 2 x 2 but the Max Pooling is spitting out 2048 x 1 x 1