中制作自定义损失函数

中制作自定义损失函数

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

问题描述

我一直在尝试在 keras 中为 dice_error_coefficient 制作自定义损失函数.它在 tensorboard 中有它的实现,我尝试在 keras 和 tensorflow 中使用相同的函数,但是当我使用 model.train_on_batch 时它一直返回 NoneTypemodel.fit ,因为它在模型中的指标中使用时会给出适当的值.请有人帮我解决我该怎么办?我曾尝试关注 ahundt 的 Keras-FCN 等库,他在其中使用了自定义损失函数,但似乎都不起作用.代码中的target和output分别是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. 对于简单的自定义损失函数(如 DICE),直接使用 Keras 后端而不是 tensorflow 实际上要简洁得多.以下是以这种方式实现的系数示例:

  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 compile中使用如下.

    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 中制作自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-27 20:34