python - 准确度良好,但在图像分割中没有骰子损失
问题描述
我正在使用带有 Keras 的 Tensorflow 上的 U-Net 之类的架构进行图像分割,但我是深度学习的新手。
我有这个数据集具有以下设置的形状:
- 火车 : X : (175250, 30, 30, 6) Y: (175250, 30, 30)
- 验证: X: (29200, 30, 30, 6) Y: (29200, 30, 30)
- 测试:X:(29200、30、30、6) Y:(29200、30、30)
得到了这些图像和每个通道的一些示例,进一步向下。
--> 每组平均有 20% 的正例和 80% 的负例
我运行了一些系列,但为了获得最佳过滤器组合,它为 BCE 绘制了高精度的图:
自定义函数的绘图,Dice_Coeff 的 Dice_Loss:
以及从使用测试图像训练的最佳模型生成的一些图像:
问题是当我改变骰子损失和系数时,没有像我们在图像图中看到的那样好的预测,现在它不在我们可能看到的图像预测中。
为什么它在骰子损失中表现如此糟糕?您还推荐什么其他功能?
我的骰子损失和系数函数:
def dice_coeff(y_true, y_pred, smooth=1): intersection = K.sum(K.abs(y_true * y_pred), axis=-1) return (2. * intersection + smooth) / (K.sum(K.square(y_true),-1) + K.sum(K.square(y_pred),-1) + smooth) def dice_loss(y_true, y_pred): return 1-dice_coeff(y_true, y_pred)
解决方案
你试过用软骰子吗?
最近在计算机视觉方面的工作提出了软代理来缓解差异并直接优化所需的度量,或者通过松弛(soft-Dice,soft-Jaccard)。
还有其他流行的图像分割损失函数:
图像分割任务中最常用的损失函数是逐像素交叉熵损失。这种损失单独检查每个像素,将类预测(深度像素向量)与我们的 one-hot 编码目标向量进行比较。
用于图像分割任务的另一个流行的损失函数是基于Dice 系数(您已经尝试过),它本质上是两个样本之间重叠的度量。该度量的范围从 0 到 1,其中 Dice 系数为 1 表示完全重叠。
如果您想知道,在计算 Dice 系数时,分子中有一个 2,因为我们的分母“双重计算”了两组之间的共同元素。为了制定可以最小化的损失函数,我们将简单地使用 1−Dice。
这种损失函数被称为软骰子损失,因为我们直接使用预测概率而不是做阈值并将它们转换为二进制掩码。
为每个类别分别计算软骰子损失,然后平均得出最终分数。下面提供了一个示例实现。
def soft_dice_loss(y_true, y_pred, epsilon=1e-6):
"""Soft dice loss calculation for arbitrary batch size, number of classes, and number of spatial dimensions.
Assumes the `channels_last` format.
# Arguments
y_true: b x X x Y( x Z...) x c One hot encoding of ground truth
y_pred: b x X x Y( x Z...) x c Network output, must sum to 1 over c channel (such as after softmax)
epsilon: Used for numerical stability to avoid divide by zero errors
"""
# skip the batch and class axis for calculating Dice score
axes = tuple(range(1, len(y_pred.shape)-1))
numerator = 2. * np.sum(y_pred * y_true, axes)
denominator = np.sum(np.square(y_pred) + np.square(y_true), axes)
return 1 - np.mean(numerator / (denominator + epsilon)) # average over classes and batch
您可以尝试这些功能并找出最适合您的模型的功能。
推荐阅读
- python-3.x - 如何改进这些简单的掷骰子函数来生成直方图?
- flask - Keras 模型部署到烧瓶文本预测,但没有预测帮助我
- python-3.x - Scrapy 没有进入 parse()
- git - Git/Github 没有看到存储库中的更改
- firebase - 如何通过 cli 输出 Firestore 配置
- python - 如何从 python shell 命令启动节点服务器?
- twilio - 暂停 Twilio SMS 消息
- python - 尝试使用 pygame 在屏幕上绘制,无法使用任何方法
- javascript - TypeError: _this.props.match 未定义 ReactJS
- javascript - 生成随机素数