How to build a grayscale graph data set (some problems in building process)

I tried to my data set and test set, the original pictures are grayscales with format ‘.bmp’.
I used the following mothod :

addrBase1 = '../Train/'
addrBase2 = '../Test/'

transform = transforms.Compose(
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# TrainSet
trainSet = torchvision.datasets.ImageFolder(addrBase1,
                                            transform = transform)       
trainLoader =, batch_size=batchSize, shuffle=True)

# TestSet
testSet1 = torchvision.datasets.ImageFolder(addrBase1,
                                            transform = transform)
testSet2 = torchvision.datasets.ImageFolder(addrBase2,
                                            transform = transform)
test1Loader =, batch_size=batchSize, shuffle=True)
test2Loader =, batch_size=batchSize, shuffle=True)

and then, I got the Initial datas with 3 channels, which means, parameter of Convolution layer is 3Nk*k.

I want to get 1 channels data, so, I added


to transform.

transform = transforms.Compose(
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

But when i run the codes, ‘Spyder’ told me the following messages:

What i want to solve are:

  1. How to build a grayscale graph data set?

  2. How to figure out the problem ’ img should be PIL Image. Got <class ‘torch.Tensor’> ’

Forgive me for my poor expression and thanks a lot in advances.

transforms.Grayscale works on PIL.Images so you should put it before transforms.ToTensor.
Also, transforms.Normalize would then only take a single value for the mean and standard deviation.

transform = transforms.Compose([
    transforms.Normalize((0.5,), (0.5,))
1 Like

Dear Mr.ptrblck,

Thank you very much. Your answer solves my question perfectly.

I’m reflecting on why I didn’t think of it.

There are two reasons:

  1. I’m still not familiar with the pytorch framework;

  2. By looking at the tips in the source code, I did not find: the setting order of parameters is the same as the execution order;

I admire your solid foundation and execution.I have a small request.Could you please share your method of learning such a framework (as a beginner, I feel very ignorant about many things)?

We’ve all started at some point, so don’t worry about being a beginner. :wink:
I think the best way to learn a new framework is just to get your hands dirty:

  • be an active member if this discussion board: have a look at some questions here and try to answer them (if possible with a small code sample showing the solution)
  • also working on some issues helps a lot as you will get familiar with the implementation (chose a simple one at first and have a look at the tests of similar methods to get a feeling of the new feature)
  • try to implement some papers in PyTorch and compare the results to implementations in other frameworks if available
1 Like

Thank you for your reply. I will try to get my hands dirty and keep up with you and other active member.:blush:

1 Like