1
$\begingroup$

What is the difference between dice loss vs jaccard loss in semantic segmentation task?

Dice loss:

Dice = (2*|X & Y|)/ (|X|+ |Y|) =  2*sum(|A*B|)/(sum(A^2)+sum(B^2))

def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    dice_coef = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
    return 1.0-dice_coef

Not sure why Dice = (2*|X & Y|)/ (|X|+ |Y|) = 2*sum(|A*B|)/(sum(A^2)+sum(B^2)) ?

And seems implementation differ:

https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py#L27 https://gist.github.com/wassname/7793e2058c5c9dacb5212c0ac0b18a8a

Jaccard loss:

Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|) = sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|))

def jaccard_loss(y_true, y_pred):
    intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
    sum_ = K.sum(K.abs(y_true) + K.abs(y_pred), axis=-1)
    jac = (intersection + smooth) / (sum_ - intersection + smooth)
    return (1 - jac) * smooth

https://github.com/keras-team/keras-contrib/blob/master/keras_contrib/losses/jaccard.py

$\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.