Could you check the shape of labels?
nn.CrossEntropyLoss can be used for a multi-class classification or segmentation and expects the targets to have the shape [batch_size] for the former and [batch_size, height, width] for the latter case.
The targets should also contain the class indices in the range [0, nb_classes-1].
Based on the error message and your example pictures, I guess you might pass the target as a one-hot encoded mask in the shape [batch_size, nb_classes, height, width].
If that’s the case, use labels = torch.argmax(labels, dim=1) to create the expected target tensor.