alex_bn_lee

导航

【632】keras 自定义损失函数

[1] Keras自定义Loss函数

[2] 【602】语义分割评价指标 IoU mIoU precision recall F1 的计算

[3] keras训练和加载自定义的损失函数

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

 

posted on 2021-08-05 17:29  McDelfino  阅读(578)  评论(0编辑  收藏  举报