Patrice:

The targets are not in the form of 0 (background) and 1(foreground) as it should be in the case of binary segmentation. Instead, the values of the foreground are float values ranging from 0.1 to 1.


Thanks for mentioning it again.
The dice loss is used for discrete data, so I doubt it can work for a regression task.
Since your labels are in the range [0, 1], I would again recommend to use nn.BCEWithLogitsLoss.
Alternatively, since you are already rounding the targets to 0 and 1 to be able to use F.cross_entropy, you could also apply the same for the dice loss calculation.