在keras中创建自定义损失函数 [英] Make a custom loss function in keras

查看:434
本文介绍了在keras中创建自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在keras中为dice_error_coefficient建立一个自定义损失函数.它在 tensorboard 中有其实现,我尝试在tensorflow的keras中使用相同的功能,但是当我使用 model.train_on_batch 时,它始终返回 NoneType .或 model.fit ,因为它在模型的指标中使用时会给出正确的值.可以请别人帮我做什么吗?我尝试过使用ahundt编写的Keras-FCN之类的库,其中他使用了自定义损失函数,但似乎都不起作用.代码的目标和输出分别是keras的loss.py文件中使用的y_true和y_pred.

Hi I have been trying to make a custom loss function in keras for dice_error_coefficient. It has its implementations in tensorboard and I tried using the same function in keras with tensorflow but it keeps returning a NoneType when I used model.train_on_batch or model.fit where as it gives proper values when used in metrics in the model. Can please someone help me out with what should i do? I have tried following libraries like Keras-FCN by ahundt where he has used custom loss functions but none of it seems to work. The target and output in the code are y_true and y_pred respectively as used in the losses.py file in keras.

def dice_hard_coe(target, output, threshold=0.5, axis=[1,2], smooth=1e-5):
    """References
    -----------
    - `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`_
    """

    output = tf.cast(output > threshold, dtype=tf.float32)
    target = tf.cast(target > threshold, dtype=tf.float32)
    inse = tf.reduce_sum(tf.multiply(output, target), axis=axis)
    l = tf.reduce_sum(output, axis=axis)
    r = tf.reduce_sum(target, axis=axis)
    hard_dice = (2. * inse + smooth) / (l + r + smooth)
    hard_dice = tf.reduce_mean(hard_dice)
    return hard_dice

推荐答案

在Keras中实现参数化的自定义损失函数有两个步骤.首先,编写系数/度量的方法.其次,编写包装函数以格式化Keras需要的格式.

There are two steps in implementing a parameterized custom loss function in Keras. First, writing a method for the coefficient/metric. Second, writing a wrapper function to format things the way Keras needs them to be.

  1. 使用Keras后端代替tensorflow直接运行简单的自定义损失函数(如DICE)实际上要干净得多.这是以这种方式实现的系数的示例:

  1. It's actually quite a bit cleaner to use the Keras backend instead of tensorflow directly for simple custom loss functions like DICE. Here's an example of the coefficient implemented that way:

import keras.backend as K
def dice_coef(y_true, y_pred, smooth, thresh):
    y_pred = y_pred > thresh
    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)

  • 现在是棘手的部分. Keras损失函数只能将(y_true,y_pred)作为参数.因此,我们需要一个单独的函数来返回另一个函数.

  • Now for the tricky part. Keras loss functions must only take (y_true, y_pred) as parameters. So we need a separate function that returns another function.

    def dice_loss(smooth, thresh):
      def dice(y_true, y_pred)
        return -dice_coef(y_true, y_pred, smooth, thresh)
      return dice
    

  • 最后,您可以在Keras编译中按如下方式使用它.

    Finally, you can use it as follows in Keras compile.

    # build model 
    model = my_model()
    # get the loss function
    model_dice = dice_loss(smooth=1e-5, thresh=0.5)
    # compile model
    model.compile(loss=model_dice)
    

    这篇关于在keras中创建自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆