I guess you are working on a multi-class segmentation?
If so, the model output should have the shape [batch_size, nb_classes, height, width], while the target should have the shape [batch_size, height, width] and container class indices in the range [0, nb_classes-1] as shown here:
batch_size, nb_classes, height, width = 2, 3, 4, 4
output = torch.randn(batch_size, nb_classes, height, width)
target = torch.randint(0, nb_classes, (batch_size, height, width))

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

Based on your error message I guess your target has 4 dimensions, which is wrong. If you are using a one-hot encoded target, use target = torch.argmax(target, dim=1) to create the expected shape (assuming the class dimension is in dim1).