I assume you are working on a multi-class segmentation use case and are thus using nn.CrossEntropyLoss as the criterion. The error message doesn’t seem to fit this loss function, so you might want to switch to it. Once this is done, remove the “channel dimension” in your masks tensors, as they are not expected. nn.CrossEntropyLoss expects a model output tensors in the shape [batch_size, nb_classes, height, width] containing the logits for each pixel and a target tensor in the shape [batch_size, height, width] containing the class indices for each pixel in the range [0, nb_classes-1].