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

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:

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