RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x24576 and 16384x7)

I tried running this code and unable to complete it because of the runtime error can anyone suggest a way out for this.

class Net(nn.Module):
    def __init__(self, num_classes=7):
        super(Net,self).__init__()
        
        
        #Input shape = (64,3,256,256)
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=12,kernel_size=3,stride=1,padding=1)
        #output = (64,12,256,256)
        self.bn1=nn.BatchNorm2d(num_features=12)
        #output = (64,12,256,256)
        self.relu1=nn.ReLU()
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,128,128)
        
        
        self.conv2=nn.Conv2d(in_channels=12,out_channels=24,kernel_size=3,stride=1,padding=1)
        #output = (64,24,128,128)
        self.bn2=nn.BatchNorm2d(num_features=24)
        #output = (64,12,128,128)
        self.relu2=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,64,64)
        
        
        self.conv3=nn.Conv2d(in_channels=24,out_channels=48,kernel_size=3,stride=1,padding=1)
        #output = (64,48,128,128)
        self.bn3=nn.BatchNorm2d(num_features=48)
        #output = (64,48,128,128)
        self.relu3=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,48,32,32)
        
        
        self.conv4=nn.Conv2d(in_channels=48,out_channels=96,kernel_size=3,stride=1,padding=1)
        #output = (64,96,32,32)
        self.bn4=nn.BatchNorm2d(num_features=96)
        #output = (64,96,32,32)
        self.relu4=nn.ReLU()
        #output = (64,96,32,32)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,96,16,16)
    
    
        self.fc=nn.Linear(in_features=64*16*16,out_features=num_classes)
        
        
    def forward(self, input):
            output=self.conv1(input)
            output=self.bn1(output)
            output=self.relu1(output)
            
            output=self.pool(output)
            
            output=self.conv2(output)
            output=self.bn2(output)
            output=self.relu2(output)
            
            output=self.conv3(output)
            output=self.bn3(output)
            output=self.relu3(output)
            
            
            output=self.pool(output)
            
            
            output=self.conv4(output)
            output=self.bn4(output)
            output=self.relu4(output)
            
            #Above ouput will be in the matrix form, with shape (64,96,16,16)
            
            output=output.view(-1, 96*16*16)
            
            output=self.fc(output)    
            
            return output
        
model = Net(num_classes=7).to(device)

#Optimizer and Loss Function

optimizer = optim.SGD(model.parameters(),lr=0.001, momentum=0.9)
loss_function=nn.CrossEntropyLoss()

num_epochs=5


best_accuracy=0.0

for epoch in range(num_epochs):
    
    #Evaluation and training on training dataset
    model.train()
    train_accuracy=0.0
    train_loss=0.0
    
    for i, (images,labels) in enumerate(train_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        optimizer.zero_grad()
        
        outputs=model(images)
        loss=loss_function(outputs,labels)
        loss.backward()
        optimizer.step()
        
        
        train_loss+= loss.cpu().data*images.size(0)
        _,prediction=torch.max(outputs.data,1)
        
        train_accuracy+=int(torch.sum(prediction==labels.data))
        
    train_accuracy=train_accuracy/train_count
    train_loss=train_loss/train_count
    
    
    # Evaluation on testing dataset
    model.eval()
    
    test_accuracy=0.0
    for i, (images,labels) in enumerate(test_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        outputs=model(images)
        _,prediction=torch.max(outputs.data,1)
        test_accuracy+=int(torch.sum(prediction==labels.data))
    
    test_accuracy=test_accuracy/test_count
    
    
    print('Epoch: '+str(epoch)+' Train Loss: '+str(train_loss)+' Train Accuracy: '+str(train_accuracy)+' Test Accuracy: '+str(test_accuracy))
    
    #Save the best model
    if test_accuracy>best_accuracy:
        torch.save(model.state_dict(),'best_checkpoint.model')
        best_accuracy=test_accuracy

I got the Error


RuntimeError Traceback (most recent call last)
Input In [26], in <cell line: 3>()
13 labels=Variable(labels.cuda())
15 optimizer.zero_grad()
—> 17 outputs=model(images)
18 loss=loss_function(outputs,labels)
19 loss.backward()

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don’t have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []

Input In [24], in Net.forward(self, input)
77 #Above ouput will be in the matrix form, with shape (64,96,16,16)
79 output=output.view(-1, 961616)
—> 81 output=self.fc(output)
83 return output

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don’t have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\linear.py:103, in Linear.forward(self, input)
102 def forward(self, input: Tensor) → Tensor:
→ 103 return F.linear(input, self.weight, self.bias)

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x24576 and 16384x7)

You declare fc with 64 * 16 * 16 = 16384 and use it with 96 * 16 * 16 = 24576. If you fix that, it should work.

Best regards

Thomas

Thank you man
I will try it