I get "RuntimeError: number of dims don't match in permute", when I try to solve it, a new runtime error "RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 500, 353, 3] to have 3 channels, but got 500 channels instead" occurs

I am converting NHWC to NCHW format because I receive the following error:


RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 500, 353, 3] to have 3 channels, but got 500 channels instead

My solution is to use permute like so:


 torch.permute(imgdata, (0, 3, 1, 2))

But I get this error:


RuntimeError: number of dims don't match in permute

I tried to edit the permute code like this:

torch.permute(imgdata, (0, 2, 1))

but it just goes back to the first error RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 500, 353, 3] to have 3 channels, but got 500 channels instead

Here is part of my code that is relevant to the errors:

for imname, metaitem in  metadata_test.items():
  
        metaitem = metadata_test[imname]
        impath = metaitem['impath']
        impath = cv2.imread(impath)
        
        imgdata = torch.Tensor(impath)

        torch.permute(imgdata, (0, 3, 1, 2))
        pred = model(imgdata)

Any ideas for me? Thank you in advance

Your first approach works for me:

img = torch.randn(1, 500, 353, 3)
img = img.permute(0, 3, 1, 2)
print(img.shape)
# torch.Size([1, 3, 500, 353])

so check why the imgdata tensor has only 3 dimensions and if e.g. the batch dimension might be missing assuming you are trying to permute the tensor inside the Dataset.__getitem__ method. If so, just permute the HWC dims to CHW.

1 Like

Hello @ptrblck , I don’t have batch size, I am loading one sample at a time from a subset of a dataset called metadata_test.items()

I tried unsqueezing like this

imgdata = torch.unsqueeze(imgdata, 0)

but it doesn’t work. Should I create a class like torch.utils.data.Dataset ? I am also not using dataloader where I would define batch_size

Oh it worked now with this code instead of permute:

imgdata = torch.einsum('n h w c -> n c h w', imgdata)

but permute should also work, I will try to know why it doesn’t.
Thanks for helping me @ptrblck