PyTorch RGB_TO_GRAY

username: Sherzod_Bek
cooked: <p>Hello,<br>
I have 10 input color-images ([10, 3, 224, 224]), I want to convert to gray image in PyTorch…</p>
<p>torch.Size([10, 3, 224, 224])   →   torch.Size([10, 224, 224])</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/f/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813" title="rgb2gray"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/f/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813_2_690x285.jpeg" alt="rgb2gray" data-base62-sha1="zK1wSe80CTmEJQAAuv8cyNrFQmD" width="690" height="285" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/f/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813_2_690x285.jpeg, https://discuss.pytorch.org/uploads/default/original/2X/f/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/f/fa7ff6e7da8ccc2d0c3421a4058cb658cdf96813.jpeg 2x" data-dominant-color="D3D5CB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">rgb2gray</span><span class="informations">803×332 84.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>How can I do that, should I chose one channel from RGB or is there any function to convert RGB to GRAY…?</p>

username: Filos92
cooked: <p>The size after turning into Gray isn’t [10,244,244] but [10,1,244,244]<br>
You can do this easily with</p>
<p><code>torchvision.transforms.</code> <code>Grayscale</code> ( <em>num_output_channels=1</em> )</p>
<p>here is a little tutorial for this:<br>
<a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/data_loading_tutorial.html</a></p>
<p>If you need the form [10,224,224] you cann use</p>
<pre><code>pictures.view(10,244,244)</code></pre>

username: tom
cooked: <p>So the lazy way to do this is <code>gray = img.mean(1)</code> (but be careful when you have an alpha channel).<br>
However, that isn’t a good way, as RGB are <em>not</em> equally bright. People have thought about this and came up with <a href="https://en.wikipedia.org/wiki/Luma_(video)" rel="nofollow noopener">various weights</a>. A great way to apply these weights is to carry out a pointwise convolution (that you also see in ResNets and friends to change the number of channels, here 3 channels in, one out, 1x1 pixel, use <code>torch.nn.functional.conv_2d</code> with a weight of shape <code>1, 3, 1, 1</code>). But weighted average is not the end of the story either, there is gamma correction etc.</p>
<p>There are many more links at <a href="https://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity" rel="nofollow noopener">stack overflow</a>, particularly noteworthy is <a href="http://cadik.posvete.cz/color_to_gray_evaluation/" rel="nofollow noopener">this study</a>.</p>
<p>Best regards</p>
<p>Thomas</p>
<p>P.S.: <a class="mention" href="/u/filos92">@Filos92</a>’s way works on PIL images not tensors, but uses whatever PIL provides for us (hopefully a good choice). The more PyTorch-y way to get rid of the singleton dimension would be <code>img_gray.squeeze(1)</code> rather than <code>.view</code>.</p>