Solve RuntimeError: mat1 and mat2 shapes cannot be multiplied

I m trying to implement a convolutional model from scratch to better understanding as explained in lesson 4. I am using the covid-19 x-ray image data I’m using is of shapes 3, 224, 224 with 3 channels.

I have build the model as floolow

class CovidCnnModel(ImageClassificationBase):

def __init__(self):
    super().__init__()
    self.network = nn.Sequential(
        nn.Conv2d(3, out_channels = 32, kernel_size=3, padding=1), #224
        nn.ReLU(),
        nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2, 2), # output: 64 x 16 x 16
        nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), #112
        nn.ReLU(),
        nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2, 2), # output: 128 x 8 x 8
        nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1), #56
        nn.ReLU(),
        nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2, 2), # output: 256 x 4 x 4
        nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1), #28
        nn.ReLU(),
        nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2, 2), # output: 512 x 2 x 2
        nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1), #14
        nn.MaxPool2d(2, 2), # output: 512 x 2 x 2

        nn.Flatten(), 
        nn.Linear(256*4*4, 1024),
        nn.ReLU(),
        nn.Linear(1024, 512),
        nn.ReLU(),
        nn.Linear(512, 2))
               
def forward(self, xb):
    return self.network(xb)

model = CovidCnnModel() # Model initialization

for images, labels in train_loader:
print('Image.shape: ', images.shape)
out = model(images)
print('out.shape: ', out.shape)
print(‘out[0]’, out[0])
break

Now when I call the model on a batch of images it gives a runtime error. like this

please could anyone help me understand and solve this error? why I’m getting this error? how to solve it. thank in advance

Your input certainly isn’t 224 by 224 pixels. Did you apply correct transforms?

Also, the linear part is incorrect since your input here won’t be 4 by 4 pixels with 256 channels (even after applying transforms).