How to use the "ImageFolder" function to load single channel image

Hello! I am new to Pytorch. and l wanna know how to use to load grey-scale image. When i used it to load image data to train Lenet-5 model , it shows that the dataset has three channels and do not fit the model’s input . I tried to translate the image to grey-scale image. but it did not work. would you please tell me the reason and help me to solve this problem? thanks!

1 Like

This line here in torchvision is reason for 3 channel loading. You can write your own function to load image like at this line and pass to loader parameter of ImageFolder.

Just a headsup: If you need to load MNIST, there’s a torch vision dataset for that.

Hope this helps.

2 Likes

You could also do a transform to Grayscale

dataset = torchvision.datasets.ImageFolder(
        root = data_path,
        #    By default the imageFolder loads images with 3 channels and we expect the image to be grayscale.
        #    So let's transform the image to grayscale
        transform =  transforms.Compose([transforms.Grayscale(), transforms.ToTensor()])
    )
4 Likes