Apparently, torchvision transformation transforms.ToPILImage() never check if the image has more than 4 channels and silently propagate this type of data to the PIL.Image.fromarray method with mode='RGB'. This method if provided with mode ‘RGB’ again doesn’t check number of channels (though it checks if mode is not provided) and change the number of channels to 3.