平滑骰子损失如何区分? [英] How is the smooth dice loss differentiable?
问题描述
我正在通过最小化普遍用于此问题的dice_loss
函数来在keras中训练U-Net:此处修改和此处
I am training a U-Net in keras by minimizing the dice_loss
function that is popularly used for this problem: adapted from here and here
def dsc(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
score = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
return score
def dice_loss(y_true, y_pred):
return (1 - dsc(y_true, y_pred))
此实现不同于传统骰子损失,因为它具有使可微"的平滑术语.我只是不明白如何在分母中添加smooth
项而不是诸如1e-7
之类的词会更好,因为它实际上会改变损耗值.我已经在具有常规dice
实现的测试集上使用经过训练的unet模型进行了检查,如下所示:
This implementation is different from the traditional dice loss because it has a smoothing term to make it "differentiable". I just don't understand how adding the smooth
term instead of something like 1e-7
in the denominator makes it better because it actually changes the loss values. I have checked this by using a trained unet model on a test set with a regular dice
implementation as follows:
def dice(im1,im2):
im1 = np.asarray(im1).astype(np.bool)
im2 = np.asarray(im2).astype(np.bool)
intersection = np.logical_and(im1, im2)
return np.float(2. * intersection.sum()) / (im1.sum() + im2.sum() + 1e-7))
有人可以解释为什么通常使用平滑骰子损失吗?
Can someone explain why the smooth dice loss is conventionally used?
推荐答案
在损失中添加smooth
并不能使其可区分.使其与众不同的是
1.放宽预测的阈值:您不将y_pred
强制转换为np.bool
,而是将其保留为介于0到1之间的连续值
2.请勿将set运算用作np.logical_and
,而应使用逐元素乘积来近似不可微交运算.
Adding smooth
to the loss does not make it differentiable. What makes it differentiable is
1. Relaxing the threshold on the prediction: You do not cast y_pred
to np.bool
, but leave it as a continuous value between 0 and 1
2. You do not use set operations as np.logical_and
, but rather use element-wise product to approximate the non-differenetiable intersection operation.
只有当y_pred
和y_true
不包含任何前景像素时,才添加smooth
以避免被零除.
You only add smooth
to avoid devision by zero when both y_pred
and y_true
do not contain any foreground pixels.
这篇关于平滑骰子损失如何区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!