美文网首页
What is "Dice loss" for image se

What is "Dice loss" for image se

作者: ouchaochao | 来源:发表于2019-06-14 11:43 被阅读0次

    1. Dice loss 是什么?

    ​ Dice loss是Fausto Milletari等人在V-net中提出的Loss function,其源于Sørensen–Dice coefficient,是Thorvald SørensenLee Raymond Dice于1945年发展出的统计学指标。这种coefficient有很多別名,最响亮的就是F test的F1 score。在了解Dice loss之前我们先谈谈Sørensen–Dice coefficient是什么。

    ​ 回顾一下比较常听到的F1 score,统计学中有所谓的SensitivitySpecificity,而机器学习(模式识别)则有PrecisionRecall,它们的关系如下:

    Truth\Classified Positive Negative
    Positive True Positive False Negative
    Negative False Positive True Negative

    Precision=\frac{TP}{Tp+FP}, Recall=Sensitivity=\frac{TP}{TP+FN}, Specificity=\frac{TN}{TN+FP}

    可看到Precision和Recall的主角都是被正确选择的那一群,分別用挑选总数(TP+FP)正确总数(TP+FN)來评估正确的比例。F1 score便是想以相同权重(β=1)的Harmonic mean(调和平均)去整合這两个指标:

    \frac{1}{F^1}=\frac{1}{Precision}+\frac{1}{Reacll} \rightarrow F^1=\frac{2PR}{P+R} \rightarrow F^1=\frac{2TP}{2TP+FP+FN}

    现在回到Sørensen–Dice coefficient的常见表现方式:

    QS=\frac{2|X\cap Y|}{|X|+|Y|}=\frac{2TP}{2TP+FP+FN}

    QSQuotient of Similarity(相似商),就是coefficient的值,只会介于0~1。Image segmentation中,模型分割出的mask就是影像的挑选总数,专家标记的mask就是正确总数。对应到公式便可知挑选总数(TP+FP)正确总数(TP+FN)分別就是XY,交集便是TP,可見Dice coefficient等同F1 score,直观上是计算X与Y的相似性,本质上则同时隐含Precision和Recall两个指标。

    ​ 谈完了coefficient,Dice loss其实就是它的顛倒。当coefficient越高,代表分割結果与标准答案相似度越高,而模型则是希望用求极小值的思維去训练比较可行,因此常用的Loss function有 "1-coefficient""-coefficient"

    2. Dice loss 实现

    实现环境:

    • Windows 10
    • Python 3.6.4
      • MXNet 1.0.1

    因为是小测试就不用GPU了。公式中的交集在image segmentation中很好实现,因为通常标准答案的mask都是由0和1組成的,所以只要将两张mask作逐点乘积(Hadamard product),也就是对应点相乘起來而不作向量內积,再加总起來就好了。因为False Positive跟Negative的情况就是其中一张mask值是0,所以在后续加总时会被排除。

    ​ 另一个有趣的点是我在公式中加入了Laplace smoothing,也就是分子分母同時加1,這是启发自一个pytorch的issue comment。据他所說,Laplace smoothing可以减少Overfitting,我想是因為让整个coefficient值变大,让loss变小,就可以更快达到收敛,而避免過多的训练迭代。

    from mxnet import nd
    smooth = 1.
    def dice_loss(y_pred, y_true):
        product = nd.multiply(y_pred, y_true)
        intersection = nd.sum(product)
        coefficient = (2.*intersection +smooth) / (nd.sum(y_pred)+nd.sum(y_true) +smooth)
        loss = 1. - coefficient
        # or "-coefficient"
        return(loss)
    

    接著随机生成两个矩阵测试:

    y_pred = nd.random.uniform(0, 1, (3,3))
    y_true = nd.random.uniform(0, 2, (3,3)).astype('int8').astype('float32')
    dice_loss(y_pred, y_true)
    ---------------------------------------------------------
    
    y_pred = [[0.38574776 0.08795848 0.83927506]
              [0.21592768 0.44453627 0.10463644]
              [0.8793516  0.65118235 0.5184219 ]]
             <NDArray 3x3 @cpu(0)>
    
    y_true = [[1. 0. 0.]
              [0. 0. 0.]
              [1. 1. 1.]]
             <NDArray 3x3 @cpu(0)>
    
    product = [[0.38574776 0.         0.        ]
               [0.         0.         0.        ]
               [0.8793516  0.65118235 0.5184219 ]]
              <NDArray 3x3 @cpu(0)>
    
    intersection = [2.4347036] <NDArray 1 @cpu(0)>
    
    coefficient = [0.64307916] <NDArray 1 @cpu(0)>
    # no smooth : [0.59916145] <NDArray 1 @cpu(0)>
    
    loss = [0.35692084] <NDArray 1 @cpu(0)>
    # no smooth : [0.40083855] <NDArray 1 @cpu(0)>
    

    以上结果用计算机敲一敲就可以验证了,可以看到在有smooth的情況下,coefficient增大了而loss减少了,因此可以让神经网络更快收敛。

    3. 后记

    ​ 在2016年V-net开始使用后,Dice loss在2017年得到了一些进化,其中有篇文献实验比较了Dice loss和影像深度学习常用的Cross-entropy的性能,发现Dice loss在image segmentation真的表现比较好。

    本文源自:鄭仕群

    相关文章

      网友评论

          本文标题:What is "Dice loss" for image se

          本文链接:https://www.haomeiwen.com/subject/xluufctx.html