[1] Keras自定义Loss函数
Dice_loss 实现:
from keras import backend as K
# 防止分母为0
smooth = 1e-5
# 定义Dice损失函数
def dice_coef_loss(y_true, y_pred):
y_truef = K.flatten(y_true) # 将 y_true 拉为一维
y_predf = K.flatten(y_pred)
intersection = K.sum(y_truef * y_predf)
dice_coef = (2 * intersection + smooth) / (K.sum(y_truef) + K.sum(y_predf) + smooth)
return 1-dice_coef