Custom dataset and loader, size missmatch error

I wrote my own dataset to load MNIST data, The images are getting loaded, but these 2D images (i.e only 1 channel), this causes problem in torch.nn.Conv2d which expects a 4D Tensor, I tried loading the images as 3D Tensor (32X32X1), but it still doesn’t work and gives size mis-match error, please help. I have also tried 3D tensor (1X32X32), it still doesn’t work kindly check this link for images and details, forum won’t allow me to put everything here.

size miss match and loading problems

You must consider that data must be loaded in this shape:
bs = batch_size
ch= channels
w and h = width and height respectively

If you do not consider batch_size, set it to 1. In this regard, when you want to feed only one image to conv2d, the size of input tensor must be 1 x ch x w x h. In MNIST dataset you must consider the size as bs x 1 x 28 x 28.

I hope I could guide you :slight_smile:

1 Like

If you want to pass only one batch, then you could use the unsqueeze option

data = data.unsqueeze(0)

However like @mortezamg63 said, its better you give batches to the network

1 Like

You can check my dataloader in github:

1 Like