Okay, you are right. It doesn’t need convert to RGB to do some transforms, I removed convert('RGB') for images and masks, it still works, but when I removed
mask = TF.to_grayscale(mask)

in the transform method above. Trackback and error as follows:
Traceback (most recent call last):
  File "data_loader_test.py", line 16, in <module>
    for idx, (data, target) in enumerate(dataloader):
  File "/home/user/.local/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 314, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/user/.local/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 187, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/user/.local/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 187, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/user/.local/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 164, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 1 and 3 in dimension 1 at /pytorch/aten/src
/TH/generic/THTensorMath.cpp:3616

Emm, masks shape as [1,512, 512], it seems that they are already grayscale? and don’t need to convert to ‘L’ or ‘P’? And the function TF.to_grayscale doesn’t make masks shape changed, so where the problems are …