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.ToTensor(), 
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# TrainSet
trainSet = torchvision.datasets.ImageFolder(addrBase1,
                                            transform = transform)       
trainLoader = torch.utils.data.DataLoader(trainSet, batch_size=batchSize, shuffle=True)

# TestSet
testSet1 = torchvision.datasets.ImageFolder(addrBase1,
                                            transform = transform)
testSet2 = torchvision.datasets.ImageFolder(addrBase2,
                                            transform = transform)
test1Loader = torch.utils.data.DataLoader(testSet1, batch_size=batchSize, shuffle=True)
test2Loader = torch.utils.data.DataLoader(testSet2, 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

transforms.Grayscale(1)

to transform.

transform = transforms.Compose(
        [transforms.ToTensor(), 
         transforms.Grayscale(1),
         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:
%E9%94%99%E8%AF%AF%E6%88%AA%E5%9B%BE

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.Grayscale(1),
    transforms.ToTensor(),
    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