在keras中创建自定义丢失功能

时间:2022-04-01 01:43:02

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.

嗨,我一直在尝试在keras中为dice_error_coefficient创建自定义丢失函数。它在tensorboard中有它的实现,我尝试在keras中使用相同的函数和tensorflow但是当我使用model.train_on_batch或model.fit时它会一直返回NoneType,因为它在模型中的指标中使用时给出了正确的值。可以请有人帮我解决我该怎么办?我已经尝试过像ahundt这样的Keras-FCN之类的库,他已经使用了自定义丢失函数,但似乎没有任何工作。代码中的目标和输出分别是y_true和y_pred,如keras中的losses.py文件中所使用的那样。

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

1 个解决方案

#1


24  

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.

在Keras中实现参数化自定义丢失功能有两个步骤。首先,编写系数/度量的方法。其次,编写一个包装函数来按照Keras需要的方式格式化事物。

  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:

    实际上,使用Keras后端而不是tensorflow直接用于简单的自定义丢失功能(如DICE)更加清晰。以下是以这种方式实现的系数示例:

    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)
    
  2. 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.

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

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

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

最后,您可以在Keras编译中使用它,如下所示。

# 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)

#1


24  

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.

在Keras中实现参数化自定义丢失功能有两个步骤。首先,编写系数/度量的方法。其次,编写一个包装函数来按照Keras需要的方式格式化事物。

  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:

    实际上,使用Keras后端而不是tensorflow直接用于简单的自定义丢失功能(如DICE)更加清晰。以下是以这种方式实现的系数示例:

    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)
    
  2. 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.

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

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

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

最后,您可以在Keras编译中使用它,如下所示。

# 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)