How to use conv2d, if input channel value is 3rd dimention?


(P Ydv) #1

What I understood till now is that for a 3D array with dimensions A x B x C. Conv2d function of torch.nn, will take A as in_channel. However I am having a different situation. I am having 3D nifti images and loaded them using nibabel.load function and then converted them into numpy array, and then finally converted them to tensor using torch.from_numpy. So in mine case images have dimension, 512 x 512 x 50. Here Height = width = 512, and depth = 50. But if I use nn.conv2d function, then assumes that in_channels value is 512, because it is first dimension, but it is not. So how can I use conv2d function and tell it that in_channel value is the third dimension?


#2

You can’t tell nn.Conv2d that the channels are in another dimension. You would rather permute your input to match the expected dimensions. Also, nn.Conv2d expects a 4-dimensional input and your example is missing the batch dimension.
In case your current input would have the shape [batch_size, height, width, channels], just use x = x.permute(0, 3, 1, 2).


(P Ydv) #3

Thanks for reply, I know it is like N x A x B x C, where N is batch size but I ignored it for this question.


#4

Ah alright. I just wanted to mention it to give you the code sample. Otherwise I thought it would look strange to address 4 dimensions in the permute call :wink: