Source code for radio.models.keras.losses

""" Contains losses used in keras models. """
from keras import backend as K


[docs]def dice_loss(y_true, y_pred, smooth=1e-6): """ Loss function base on dice coefficient. Parameters ---------- y_true : keras tensor tensor containing target mask. y_pred : keras tensor tensor containing predicted mask. smooth : float small real value used for avoiding division by zero error. Returns ------- keras tensor tensor containing dice loss. """ y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) answer = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) return -answer
[docs]def tversky_loss(y_true, y_pred, alpha=0.3, beta=0.7, smooth=1e-10): """ Tversky loss function. Parameters ---------- y_true : keras tensor tensor containing target mask. y_pred : keras tensor tensor containing predicted mask. alpha : float real value, weight of '0' class. beta : float real value, weight of '1' class. smooth : float small real value used for avoiding division by zero error. Returns ------- keras tensor tensor containing tversky loss. """ y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) truepos = K.sum(y_true * y_pred) fp_and_fn = alpha * K.sum(y_pred * (1 - y_true)) + beta * K.sum((1 - y_pred) * y_true) answer = (truepos + smooth) / ((truepos + smooth) + fp_and_fn) return -answer
[docs]def jaccard_coef_logloss(y_true, y_pred, smooth=1e-10): """ Loss function based on jaccard coefficient. Parameters ---------- y_true : keras tensor tensor containing target mask. y_pred : keras tensor tensor containing predicted mask. smooth : float small real value used for avoiding division by zero error. Returns ------- keras tensor tensor containing negative logarithm of jaccard coefficient. """ y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) truepos = K.sum(y_true * y_pred) falsepos = K.sum(y_pred) - truepos falseneg = K.sum(y_true) - truepos jaccard = (truepos + smooth) / (smooth + truepos + falseneg + falsepos) return -K.log(jaccard + smooth)