Closed. This question needs to be more focused。它目前不接受答案。
想改进这个问题吗?更新问题,使其只关注一个问题editing this post
去年关门了。
我正在尝试训练一个用于多类分割的网络,我想使用骰子系数(参见this)作为损失函数,而不是交叉熵。
您可以查看公式here(其中S是分段,G是基本真理)
一个简单朴素的解决方案是取每个类的骰子系数的平均值,并将其用于损失函数。这种方法不会区分面积较大的类和像素数较小的类(体素)。
有人有什么建议吗?

最佳答案

如果您有多个类,并且这些类是没有顺序关系的范畴,例如,我们并不意味着dog小于cat仅仅因为我们分别设置了labels12,您应该已经使用了一个热编码标签。因此,你完全可以用下面的函数计算骰子系数,

import numpy as np

def dice_coef(y_true, y_pred, epsilon=1e-6):
"""Altered Sorensen–Dice coefficient with epsilon for smoothing."""
y_true_flatten = np.asarray(y_true).astype(np.bool)
y_pred_flatten = np.asarray(y_pred).astype(np.bool)

if not np.sum(y_true_flatten) + np.sum(y_pred_flatten):
    return 1.0

return (2. * np.sum(y_true_flatten * y_pred_flatten)) /\
       (np.sum(y_true_flatten) + np.sum(y_pred_flatten) + epsilon)

这里的额外位是,如果predicted和true标签都是空的,我们将得到1

10-07 13:20