For the posted model output shape your target should have the shape [batch_size, h, w] and contain class indices in [0, nb_classes-1] to be able to use nn.CrossEntropyLoss for a multi-class segmentation.