Error in Neural Network from scratch

Hi I’m trying to create my first simple_cnn on my dataset.

The shape of my images are: torch.Size([64, 3, 120, 340])

And my cnn is:

def conv(ni, nf, ks=3, act=True):
    layers = [nn.Conv2d(ni, nf, stride=2, kernel_size=ks, padding=ks//2)]
    layers.append(nn.BatchNorm2d(nf))
    if act: layers.append(nn.ReLU())
    return nn.Sequential(*layers)

simple_cnn= sequential(
        conv(3 ,64),          
        conv(64 ,128),
        conv(128 ,128),         
        conv(128 ,128),          
        conv(128 ,128),
        Flatten(),
        nn.Linear(2048,2048),
        nn.Linear(2048,2048),
        nn.Linear(2048,2)
    )

learn= Learner(dls,simple_cnn,loss_func=CrossEntropyLossFlat(), metrics=accuracy)
learn.fine_tune(20)

But I have the following error: RuntimeError: mat1 dim 1 must match mat2 dim 0

Can someone help me?

hi, just before the Flatten the shape is [64, 128, 4, 11]

to debug this yourself try

b = torch.randn(64, 3, 120, 340)

simple_cnn= sequential(
    conv(3 ,64),          
    conv(64 ,128),
    conv(128 ,128),         
    conv(128 ,128),          
    conv(128 ,128)
)

simple_cnn(b).shape

after the flatten the shape is [64, 5632] (5632=128 * 4 * 11)
so replace all 2048 with 5632

Hi Oscar
You have stride = 2 so the convolution output should be smaller than the convolution input.
Note your image is 120 x 340 you need to resize them in data loader to be square say 120X120
For stride =2
say you image was 120120 you would have
120x120 → 60x60(conv(1,4)->30x30(conv4,8)->15
15 conv (8,16) ->8x8 c(16,32) 4x4 2x2 1x1

See chapter 15 of the book.

Regards Conwyn