RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x16 and 4096x1024)
I am new to Deep Learning and have created a model to classify my images.
Currently, this project raises an error on Google Colab or Kaggle (CPU and GPU) but not on my personal computer (with CPU).
Model:
class CNN(nn.Module):
  def __init__(self):
    super(CNN,self).__init__()
    self.network1 = nn.Sequential(

        nn.Conv2d(3, 32, kernel_size = 3, padding = 1),
        nn.ReLU(),
        nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),
        nn.ReLU(),
        nn.MaxPool2d(2,2),
        # nn.AdaptiveAvgPool2d((128,128)),
    
        nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),
        nn.ReLU(),
        nn.Conv2d(128 ,128, kernel_size = 3, stride = 1, padding = 1),
        nn.ReLU(),
        nn.MaxPool2d(2,2),
        
        nn.Conv2d(128, 256, kernel_size = 3, stride = 1, padding = 1),
        nn.ReLU(),
        nn.Conv2d(256, 256, kernel_size = 3, stride = 1, padding = 1),
        nn.ReLU(),
        nn.AdaptiveMaxPool2d((4,4))
    )

    self.network2 = nn.Sequential(
        nn.Flatten(),
        nn.Linear(256*4*4, 1024),
        nn.ReLU(),
        nn.Linear(1024, 512),
        nn.ReLU(),
        nn.Linear(512, n_classes)
    )

    self.sigm = nn.Sigmoid()

  def forward(self,x):
    x = self.network1(x)
    x = self.network2(x)

    return self.sigm(x)

Training:
epoch = 0

model.train()
criterion = nn.BCELoss()

while True:
    batch_losses = []
    
    for imgs, labels in data:
        imgs, labels = imgs.float().to(device), labels.to(device)

        optimizer.zero_grad()

        model_result = model(imgs)
        loss = criterion(model_result, labels.type(torch.float))

        batch_loss_value = loss.item()
        loss.backward()
        optimizer.step()

        batch_losses.append(batch_loss_value)


    loss_value = np.mean(batch_losses)
    print(&quot;epoch:{:2d} iter:{:3d} train: loss:{:.3f}&quot;.format(epoch, iteration, loss_value))
    if epoch % SAVE_FREQ == 0:
        checkpoint_save(model, epoch)
    epoch += 1
    if EPOCHS &lt; epoch:
        break

ERROR:
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_33/872363799.py in &lt;module&gt;
     14         optimizer.zero_grad()
     15 
---&gt; 16         model_result = model(imgs)
     17         loss = criterion(model_result, labels.type(torch.float))
     18 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   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):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/tmp/ipykernel_33/1050848783.py in forward(self, x)
     32     self.sigm = nn.Sigmoid()
     33   def forward(self,x):
---&gt; 34     x = self.network(x)
     35     return self.sigm(x)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   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):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
    139     def forward(self, input):
    140         for module in self:
--&gt; 141             input = module(input)
    142         return input
    143 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   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):
-&gt; 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -&gt; str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x16 and 4096x1024)

Input shape is (3, 406, 565)
The summary for this model with this shape is:
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 32, 406, 565]             896
              ReLU-2         [-1, 32, 406, 565]               0
            Conv2d-3         [-1, 64, 406, 565]          18,496
              ReLU-4         [-1, 64, 406, 565]               0
         MaxPool2d-5         [-1, 64, 203, 282]               0
            Conv2d-6        [-1, 128, 203, 282]          73,856
              ReLU-7        [-1, 128, 203, 282]               0
            Conv2d-8        [-1, 128, 203, 282]         147,584
              ReLU-9        [-1, 128, 203, 282]               0
        MaxPool2d-10        [-1, 128, 101, 141]               0
           Conv2d-11        [-1, 256, 101, 141]         295,168
             ReLU-12        [-1, 256, 101, 141]               0
           Conv2d-13        [-1, 256, 101, 141]         590,080
             ReLU-14        [-1, 256, 101, 141]               0
AdaptiveAvgPool2d-15            [-1, 256, 4, 4]               0
          Flatten-16                 [-1, 4096]               0
           Linear-17                 [-1, 1024]       4,195,328
             ReLU-18                 [-1, 1024]               0
           Linear-19                  [-1, 512]         524,800
             ReLU-20                  [-1, 512]               0
           Linear-21                   [-1, 18]           9,234
          Sigmoid-22                   [-1, 18]               0
================================================================
Total params: 5,855,442
Trainable params: 5,855,442
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 2.63
Forward/backward pass size (MB): 712.84
Params size (MB): 22.34
Estimated Total Size (MB): 737.80
----------------------------------------------------------------

Solution
In my case, the problem was that I didn't consider the batch size in my image shape and that my dataset had few images with grayscale and few with an alpha channel.

In my case the problem was that first of all I didn't consider the batch size in my image shape and secondly that my dataset had few images with grayscale and few with an alpha channel.
