Vision: Transform for grayscale images.

Created on 11 Oct 2017  路  9Comments  路  Source: pytorch/vision

When using RGB images I wrote the transform like

transform_list = [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]

And it worked perfectly.

But for grayscale images I there is no documentation for transform given on this page http://pytorch.org/docs/master/torchvision/transforms.html.

Can anyone help me on how to write a transform for grayscale images ??

Thanks in advance.

Most helpful comment

Use this: transforms.Normalize((0.5, ), (0.5, )) instead. This is because grayscale images have only one channel.

All 9 comments

Use this: transforms.Normalize((0.5, ), (0.5, )) instead. This is because grayscale images have only one channel.

@vishwakftw is correct. I'll send a PR which adds tests for this and updates the docs to reflect this.

Use this: transforms.Normalize((0.5, ), (0.5, )) instead. This is because grayscale images have only one channel.

Why is there a need of , after 0.5 in Normalize?

Use this: transforms.Normalize((0.5, ), (0.5, )) instead. This is because grayscale images have only one channel.

Why is there a need of , after 0.5 in Normalize?

Same question here. And it indeed does not work if lack of comma

Because Python requires comma to make it a 1-tuple. Without comma, Python will understand it as a singular number (0.5). In the code of Normalization, it requires an N-tuple for N channels, such as a 3-tuple for 3 channels

But this doesn't solve the issue, I am loading gray scale images with batch size of 16 with dim 150 * 150 and performing the transforms as shown in below images

image
and i am getting the one instance of dataloader as (16, 3, 100, 100), 100 is because of the resize but channel as 3 ?

image

I think the problem is that ImageFolder loads the images as colour images although they are grayscale. You can simply add another transformation that makes them grayscale again: transforms.Grayscale(1),

Yes, i have done that and it worked. But i just don't get, why it didn't worked in the first place.

@Mayurji with latest versions of torchvision, if you pass a single value for the Normalize function and feed a 3-channel RBG Image, Normalize will still work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

datumbox picture datumbox  路  3Comments

a-maci picture a-maci  路  3Comments

ibtingzon picture ibtingzon  路  3Comments

zhang-zhenyu picture zhang-zhenyu  路  3Comments

Wadaboa picture Wadaboa  路  3Comments