当我训练UNET时,骰子系数和iou有时会大于1和iou > dice
,然后在几批处理之后它们又会恢复正常。
如picture所示。
我对它们的定义如下:
def dice_coef(y_true, y_pred, smooth=1):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def iou(y_true, y_pred, smooth=1):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
union = K.sum(y_true_f) + K.sum(y_pred_f) - intersection
return (intersection + smooth) / (union + smooth)
def dice_loss(y_true, y_pred):
return 1. - dice_coef(y_true, y_pred)
我曾尝试在y_pred中添加K.abs()
,但这会导致性能下降。我觉得既然输出是S型激活的,是否添加K.abs()
应该会得到相同的结果?另外,如您所见,我的准确性很奇怪,我一直依靠骰子来判断我的模型性能,如果有人指出这个问题,那就更好了。
答案 0 :(得分:2)
我相信您的y_true
图像可能不在0到1范围内。...您确定它们不在0到255之间吗?还是他们只有一个频道(而不是3个频道?)
这不是原因,但您使用的是批次骰子,则应使用图像骰子:
def dice_coef(y_true, y_pred, smooth=1):
y_true_f = K.batch_flatten(y_true)
y_pred_f = K.batch_flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f, axis=-1)
sums = K.sum(y_true_f, axis=-1) + K.sum(y_pred_f, axis=-1)
return (2. * intersection + smooth) / (sums + smooth)
通常,我使用K.epsilon()
来表示“平滑”(很小的东西)。
iou
也是如此:
def iou(y_true, y_pred, smooth=1):
y_true_f = K.batch_flatten(y_true)
y_pred_f = K.batch_flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f, axis=-1)
union = K.sum(y_true_f, axis=-1) + K.sum(y_pred_f, axis=-1) - intersection
return (intersection + smooth) / (union + smooth)
频道骰子的示例:
#considering shape (batch, classes, image_size, image_size)
def dice_coef(y_true, y_pred, smooth=1):
intersection = K.sum(y_true * y_pred, axis=[2,3])
sums = K.sum(y_true, axis=[2,3]) + K.sum(y_pred, axis=[2,3])
dice = (2. * intersection + smooth) / (sums + smooth)
return K.mean(dice, axis=-1)