Convert RGB to gray?

username: 1414b35e42c77e0a57dd
cooked: <p><code>torchvision.transforms.functional.to_grayscale()</code> can only applied to PIL Image.<br>
then how can I convert torch.Tensor RGB to gray?</p>

username: etekiller
cooked: <p>You can convert the Tensor to a PIL image, apply that transform, then convert it back to a Tensor.</p>

username: shubhvachher
cooked: <p>Well, like <a class="mention" href="/u/etekiller">@etekiller</a> says, you can try to “Compose Transforms”. An example would be <a href="https://github.com/psyec1/Lipreading-PyTorch/blob/ebfca05ea004fa3cd63980470268a043922432cb/data/preprocess.py#L56" rel="nofollow noopener">here</a>. And then use <a href="https://pytorch.org/docs/master/torchvision/transforms.html#torchvision.transforms.ToPILImage" rel="nofollow noopener">image to PIL transform</a> to solve your issue. This though will likely add a ton of time overhead to your data loading pipeline!</p>
<p>If you’re not using many transforms from pytorch on your data during “Composing your transform”, why not try using normal tensor operations to go from RGB to Gray? Try the function OpenCV uses like <a href="https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html" rel="nofollow noopener">here</a>. You can find the reasons for such an averaging <a href="https://www.tutorialspoint.com/dip/grayscale_to_rgb_conversion.htm" rel="nofollow noopener">here</a></p>

username: 1414b35e42c77e0a57dd
cooked: <p><a class="mention" href="/u/shubhvachher">@shubhvachher</a> <a class="mention" href="/u/etekiller">@etekiller</a><br>
Thanks a lot. actually, I want to convert MNIST-M channel 3 to 1.<br>
Easiest way to make this is using transforms.Compose() like this.</p>
<pre><code class="lang-auto">    pre_process = transforms.Compose(
        [transforms.Grayscale(num_output_channels=1), transforms.ToTensor(),
         transforms.Normalize(mean=[0.5], std=[0.5])])
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/c/ca44a4384070854b22d2fe1a66f8ffdd72fcdba3.png" alt="1st" data-base62-sha1="sRlvMW28nxAyInNToU0CDOcyysz" width="242" height="242"><br>
But I have to do this in dataloader for loop statement, so I write like this.<br>
and  there are some differences between two methods.</p>
<pre><code class="lang-auto">    pre_process = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])

    for i, (images, labels) in enumerate(loader_mnistm):
        images, labels = images.to(device), labels.to(device)

        images = images.cpu()
        images = [torchvision.transforms.ToPILImage()(x) for x in images]
        images = [torchvision.transforms.Grayscale()(x) for x in images]
        images = [torchvision.transforms.ToTensor()(x) for x in images]
        images = torch.stack(images).to(device)
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/73a313f579df468de3788974723c7c2b02b21691.png" alt="2nd" data-base62-sha1="guYiIZYjPLg4MGw1uKMA6zFqpON" width="242" height="242"><br>
What`s wrong with my code?</p>

username: etekiller
cooked: <p>Sorry, I am not sure I fully get what you’re trying to do. If you simply want to apply those transforms to all images you can just pass the transforms into the Dataset’s (or any of the child classes) constructor parameter.</p>
<p>In my case I pass it like this:</p>
<pre><code class="lang-auto">dataset_in = datasets.ImageFolder('./rooms', transform = transform_in )
</code></pre>
<p>Where <code>transform_in</code> is the transform created via Compose.</p>

username: 1414b35e42c77e0a57dd
cooked: <p>Finally I Solve it.<br>
Thanks a lot !</p>

username: pratik_mazumder
cooked: <p>Hi, what was the issue and how did you solve it?</p>