arcus-azure / arcus.ml

A python package that streamlines and accelerates Machine Learning development
https://machine-learning.arcus-azure.net
MIT License
2 stars 4 forks source link

Provide custom loss functions for neural networks #35

Open SamVanhoutte opened 4 years ago

SamVanhoutte commented 4 years ago

Provide a module with custom loss functions that can be reused. Two to start with:

SamVanhoutte commented 4 years ago

Sample code for dice_coef (which is used to validate the results of images , for example in image segmentation & medical masks):

# Dice coefficient loss

def dice_coef(y_true, y_pred, smooth=1):
    intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
    return (2. * intersection + smooth) / (K.sum(K.square(y_true),-1) + K.sum(K.square(y_pred),-1) + smooth)

def dice_coef_loss(y_true, y_pred):
    return 1-dice_coef(y_true, y_pred)

def dice_coef_loss_new(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)
    score = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
    return score