It seems the original code has changes a couple of times while the other issues were debugged.
Based on the error message, I assume your model outputs a spatial activation in the shape [batch_size, nb_classes, height, width]?
If that’s the case, your target should have the shape [batch_size, height, width] and contain class indices in the range [0, nb_classes-1] for nn.CrossEntropyLoss.