【问题标题】:How to calculate multi class dice coefficient for multiclass image segmentation? [closed]如何计算多类图像分割的多类骰子系数? [关闭]
【发布时间】:2018-04-15 12:02:01
【问题描述】:
我正在尝试训练一个用于多类分割的网络,我想使用骰子系数(参见this)作为损失函数而不是交叉熵。
你可以看看公式here(其中S是分割,G是ground truth。)
一个天真的简单解决方案是取每个类的 dice 系数的平均值并将其用于损失函数。这种方法不会区分面积较大的类别与像素(体素)数量较少的类别。
有人有什么建议吗?
【问题讨论】:
标签:
python
image-processing
machine-learning
deep-learning
image-segmentation
【解决方案1】:
如果您有多个类,并且这些是没有序数关系的分类,例如我们并不暗示 dog 小于 cat 只是因为我们分别设置了标签 1 和 2,您应该已经在使用 one-hot 编码标签。因此,您可以使用以下函数计算 Dice 系数,
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)
这里额外的一点是,如果预测标签和真实标签都是空的,我们会得到1。