samin_hamidi:

I am not certain if converting a mask image into a tensor is reasonable as my mask label contains class indexes (each pixel gets discrete values between 0 and 3). When I transform this image into a tensor, the values will be between 0 and 1.


That’s a correct assumption and therefore I would transform the mask to a tensor via torch.from_numpy to keep the class indices. Some transformations, e.g. resizing or ToTensor might destroy your class indices.



 samin_hamidi:

should I drop the C value and create a tensor of shape [N, W, H].


Yes, that’s also correct. nn.CrossEntropyLoss as well as nn.NLLLoss expect a target without the channel dimension, which contains just the class indices.
In you case you could just call target = target.squeeze(1).