3d vs 2d convolution when using grayscale images

Idodo: <p>When we do 2d convolution with RGB images we are, actually, doing 3d convolution. For this we still use the pytorch 2d_conv layers.</p>
<p>When we do 3d convolution of a set of RGB images, we are doing 4d convolution and can use the 3d conv layer.</p>
<p>My question is: what is the difference, if any, between using the 3d conv layer for a set of grayscale images, as opposed to giving the set of images to pytorch as color channels and using the 2d conv layer?</p>
<p>To be clear, if using 2d conv, my tensor will look like:<br>
[batch size, set of grayscale images, n rows, n cols]</p>
<p>And in the case of 3d conv:<br>
[batch size, set of grayscale images, 1 grayscale channel, n rows, n cols]</p>

Idodo: <p>After further research I understand that although we have a 3d volume in an RGB image, we are still doing 2d convolution. The convolution dimensionality refers to the filter dimensions, so in doing 2d convolution on an RGB image we are actually convolving each filter by each channel. In the case of 3d convolution we would alternatively be convolving all 3 channels at the same time by each 3d filter.</p>
<p>Is this accurate?</p>
<p>In the case of 2d convolution of grayscale images, is there a difference between using a batch of say 100 images and using an image of “100 channels”? (besides losing the flexibility of mixing images between batches)</p>

ptrblck: <aside class="quote no-group" data-username="Idodo" data-post="2" data-topic="67689">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/a5b964/48.png" class="avatar"> Idodo:</div>
<blockquote>
<p>so in doing 2d convolution on an RGB image we are actually convolving each filter by each channel. In the case of 3d convolution we would alternatively be convolving all 3 channels at the same time by each 3d filter.</p>
</blockquote>
</aside>
<p>I don’t think this is completely correct.<br>
For a “standard” 2-dimensional convolution, each filter will use all of the input channels.<br>
The number of output channels of the convolution layer defines the number of filters.<br>
Have a look at <a href="http://cs231n.github.io/convolutional-networks/#conv">CS231n</a> which explains the applied logic pretty well.<br>
Grouped of depthwise convolutions work a bit different regarding the processing of the input channels, but lets stick to the vanilla use case for now.</p>
<p>3-dimensional convolutions also use all input channels for each kernels. However, now you are performing the convolution in a volume (depth, height, width), so the kernel moves in all three dimensions this time.</p>
<aside class="quote no-group" data-username="Idodo" data-post="2" data-topic="67689">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/a5b964/48.png" class="avatar"> Idodo:</div>
<blockquote>
<p>In the case of 2d convolution of grayscale images, is there a difference between using a batch of say 100 images and using an image of “100 channels”? (besides losing the flexibility of mixing images between batches)</p>
</blockquote>
</aside>
<p>Yes, these approaches would yield different results, as in the first use case, the filters would have a single input channel, while in the latter approach they would have 100 input channels.</p>

Idodo: <p>Alright, I’ve figured it out.</p>
<p>When doing 2d convolution, the filter moves in only 2 directions (H &amp; W) of the image. That’s why the operation is called 2d convolution although a 3d filter is used to process 3d volumetric data.</p>
<p>In 3d convolution we also use a 3d filter to process 3d volumetric data, but we move in 3 directions (H, W &amp; D).</p>
<p>It was confusing to me to use a 3d filter in 2d convolution, but the aforementioned explanation should clarify the difference.</p>

ptrblck: <p>In a 3-dimensional convolution, you would use a 4-dimensional filter, which still uses all input channel, but moves in all 3 volumetric dimensions.<br>
The method is very similar to a 2-dimensional convolution with an additional depth dimension the filter moves along.</p>

Idodo: <p>Right,</p>
<p>in my particular case I use grayscale images so the channel dimension, although there, is 1.</p>
<p>Thanks!</p>