1
$\begingroup$

I implemented Dice loss class in pytorch:

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:

  1. where inputs is the prediction from the network without applying activation (in my case sigmoid), only convolution with a kernel of size 1.
  2. where inputs are 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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.