I am currently training a UNet for the Severstal Kaggle competition.
For the competition, I’ve chosen the Dice coefficient as the metric. When creating my learner and training it the dice coefficient is more often than not above 1.
I was also facing the same issue, after digging for a while came to know that the built-in dice metric was not correct for segmentation with more than 2 classes. I think it was built for binary classification of pixels and that to classes being 0 and 1. So I made a new dice metric by changing the existing one.
Dice coefficient for multi_class_segmentation
def dice_multi(input:Tensor, targs:Tensor, iou:bool=False, eps:float=1e-8)->Rank0Tensor:
n = targs.shape[0]
targs = targs.squeeze(1)
input = input.argmax(dim=1).view(n,-1)
targs = targs.view(n,-1)
targs1 = (targs>0).float()
input1 = (input>0).float()
ss = (input == targs).float()
intersect = (ss * targs1).sum(dim=1).float()
union = (input1+targs1).sum(dim=1).float()
if not iou: l = 2. * intersect / union
else: l = intersect / (union-intersect+eps)
l[union == 0.] = 1.
return l.mean()