Ok, then you shouldn’t use one-hot encoding, but keep the target segmentation map as [batch_size, height, width], where each pixel position holds a class label.
I’ve created a small code snippet to illustrate, what I’m meaning:
batch_size = 1
n_classes = 5
h, w = 24, 24

x = torch.randn(batch_size, n_classes, h, w) # channels correspond to the class logits
output = F.log_softmax(x, dim=1)
target = torch.empty(batch_size, h, w, dtype=torch.long).random_(n_classes)

criterion = nn.NLLLoss()
loss = criterion(output, target)