Hi Double!



 DoubleRedX:

pred shape: torch.Size([16, 4, 240, 240])  （batchsize, num_classes, width, height）
label shape: torch.Size([16, 240, 240])  (batchsize, width, height)(the element of tensor is in [0, 1, 2, 3], represent pixel class)
…
there is no bug when I am using nn.CrossEntropyLoss() as my loss_fn


At issue is that your DiceLoss function is expecting labels that
are different in form than those used by CrossEntropyLoss (and
there is no reason to have expected them to be the same).
For a given batch sample and given pixel, CrossEntropyLoss
compares a vector of num_classes logits (akin to probabilities)
to a single integer class label.
In contrast, your DiceLoss compares a vector of predicted class
probabilities with a vector of targets class probabilities.  (Either of
these could happen to be – or be restricted to – probabilities that
are all exactly either 0.0 or 1.0 (but if you convert your predictions
to 0.0–1.0 probabilities, you will lose differentiability and not be
able to train).


class DiceLoss(nn.Module):
def forward(self, inputs, targets, smooth=1):
inputs = functional.sigmoid(inputs)


You will most likely prefer to use softmax() to convert your logit
predictions to probabilities:
        inputs = torch.nn.functional.softmax (inputs, dim = 1)

You will need to convert your integer-class-label targets to
(0.0–1.0) class probabilities, most conveniently with one_hot():
        targets = torch.nn.functional.one_hot (inputs, num_classes = num_classes).transpose (-1, 1)

(Please think about the shapes and structures of your tensors to be
sure that you understand why the .transpose() is necessary.)
Best.
K. Frank