dice_loss_for_keras · GitHub
gist.github.com › wassname › 7793e2058c5c9dacb5212c0Here is a dice loss for keras which is smoothed to approximate a linear (L1) loss. It ranges from 1 to 0 (no error), and returns results similar to binary crossentropy """ # define custom loss and metric functions : from keras import backend as K: def dice_coef (y_true, y_pred, smooth = 1): """ Dice = (2*|X & Y|)/ (|X|+ |Y|) = 2*sum(|A*B|)/(sum(A^2)+sum(B^2))
Dice score function · Issue #3611 · keras-team/keras · GitHub
github.com › keras-team › kerasAug 28, 2016 · def dice_coef (y_true, y_pred, smooth = 1): y_true_f = K. flatten (y_true) y_pred_f = K. flatten (y_pred) intersection = K. sum (y_true_f * y_pred_f) return (2. * intersection + smooth) / (K. sum (y_true_f) + K. sum (y_pred_f) + smooth) def dice_coef_loss (y_true, y_pred): return-dice_coef (y_true, y_pred) # ... model. compile (optimizer = optimizer, loss = dice_coef_loss, metrics = [dice_coef]) # ...
Generalized dice loss for multi-class segmentation · Issue ...
github.com › keras-team › kerasReturns ----- loss_gt_(y_true, y_pred): A custom keras loss function This function takes as input the predicted and ground labels, uses them to calculate the dice loss. """ def loss_gt_(y_true, y_pred): intersection = K.sum(K.abs(y_true * y_pred), axis=[-3,-2,-1]) dn = K.sum(K.square(y_true) + K.square(y_pred), axis=[-3,-2,-1]) + 1e-8 return -K.mean(2 * intersection / dn, axis=[0,1]) return loss_gt_