多类别语义分割中常见 Loss 函数总结

本文总结了语义分割中常见的损失函数,包括Dice Loss、Tversky Loss、Focal Loss等,并提供了每种损失函数的详细解释及TensorFlow实现代码。

1 综述

学习并整理了一下语义分割的常见Loss,希望能为大家训练语义分割网络的时候提供一些关于Loss方面的知识,之后会不定期更新;【tensorflow实现】

看到一篇2020年论文《A survey of loss functions for semantic segmentation》,文章对目前常见语义分割中Loss functions进行了总结,大家有兴趣可以看看;

论文地址:A survey of loss functions for semantic segmentation
code地址:Semantic-Segmentation-Loss-Functions

1.1 Dice with weight

(1)Dice Loss 对正负样本严重不平衡的场景有着不错的性能,训练过程中更侧重对前景区域的挖掘;
(2)Dice Loss是一种区域相关的loss,像素点的loss以及梯度值不仅和该点的label以及预测值相关,和其他点的label以及预测值也相关;
(3)但训练loss容易不稳定,尤其是小目标的情况下;

Dice with weight代码如下:

def Dice_weight_loss(Y_pred, Y_gt, weight_loss):
    """
    multi label dice loss with weighted
    WDL=1-2*(sum(w*sum(r&p))/sum((w*sum(r+p)))),w=array of shape (C,)
    :param Y_pred: [None, self.image_depth, self.image_height, self.image_width,
                                                       self.numclass],Y_pred is softmax result
    :param Y_gt:[None, self.image_depth, self.image_height, self.image_width,
                                                       self.numclass],Y_gt is one hot result
    :param weight_loss: numpy array of shape (C,) where C is the number of classes
    :return:
    """
    weight_loss = np.array(weight_loss)
    smooth = 1.e-5
    smooth_tf = tf.constant(smooth, tf.float32)
    Y_pred = tf.cast(Y_pred, tf.float32)
    Y_gt = tf.cast(Y_gt, tf.float32)
    # Compute gen dice coef:
    numerator = Y_gt * Y_pred
    numerator = tf.reduce_sum(numerator, axis=(1, 2, 3))
    denominator = Y_gt + Y_pred
    denominator = tf.reduce_sum(denominator, axis=(1, 2, 3))
    gen_dice_coef = tf.reduce_mean(2. * (numerator + smooth_tf) / (denominator + smooth_tf), axis=0)
    loss = -tf.reduce_mean(weight_loss * gen_dice_coef)
    return loss
    

1.2 Tversky loss

论文地址为:Tversky loss function for image segmentation using 3D fully convolutional deep networks

实际上Dice Loss只是Tversky loss的一种特殊形式而已,我们先来看一下Tversky系数的定义,它是Dice系数和Jaccard系数的广义系数。
当 alpha 和 beta 均为0.5的时候,这个公式就是Dice系数,当 alpha 和 beta 均为1的时候,这个公式就是Jaccard系数。

def tversky_loss(Y_pred, Y_gt, beta, weight_loss):
    """
    multi label tversky with weighted
    Tversky loss (TL) is a generalization of Dice loss. TL adds a weight to FP and FN.
    define:TL(p,p')=(p&p')/(p&p'+b*((1-p)&p')+(1-b)*(p&(1-p')))
    :param Y_pred: [None, self.image_depth, self.image_height, self.image_width,
                                                       self.numclass],Y_pred is softmax result
    :param Y_gt:[None, self.image_depth, self.image_height, self.image_width,
                                                       self.numclass],Y_gt is one hot result
    :param beta:beta=1/2,just Dice loss,beta must(0,1)
    :return:
    """
    weight_loss = np.array(weight_loss)
    smooth = 1.e-5
    smooth_tf = tf.constant(smooth, tf.float32)
    Y_pred = tf.cast(Y_pred, tf.float32)
    Y_gt = tf.cast(Y_gt, tf.float32)
    p0 = Y_pred
    p1 = 1 - Y_pred
    g0 = Y_gt
    g1 = 1 - Y_gt
    # Compute gen dice coef:
    numerator = p0 * g0
    numerator = tf.reduce_sum
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值