I implemented Dice loss class in
import torch import torch.nn as nn class DiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceLoss, self).__init__() def forward(self, inputs, targets, smooth=1): smooth = 1. input_flat = inputs.contiguous().view(-1) target_flat = targets.contiguous().view(-1) intersection = (input_flat * target_flat).sum() A_sum = torch.sum(input_flat * input_flat) B_sum = torch.sum(target_flat * target_flat) dsc = (2. * intersection + smooth) / (A_sum + B_sum + smooth) return 1 - dsc
Now I tested it in 2 scenarios:
inputsis the prediction from the network without applying activation (in my case sigmoid), only convolution with a kernel of size 1.
inputsare the result of the network including activation of the sigmoid.
Now I get comparable results between the 2 ways, but I was wondering what is the "Right" way out of the 2.