我刚刚在keras中实现了广义骰子损失(骰子损失的多类版本),如ref中所述:
(我的目标定义为:(batch_size,image_dim1,image_dim2,image_dim3,nb_of_classes))
def generalized_dice_loss_w(y_true, y_pred):
# Compute weights: "the contribution of each label is corrected by the inverse of its volume"
Ncl = y_pred.shape[-1]
w = np.zeros((Ncl,))
for l in range(0,Ncl): w[l] = np.sum( np.asarray(y_true[:,:,:,:,l]==1,np.int8) )
w = 1/(w**2+0.00001)
# Compute gen dice coef:
numerator = y_true*y_pred
numerator = w*K.sum(numerator,(0,1,2,3))
numerator = K.sum(numerator)
denominator = y_true+y_pred
denominator = w*K.sum(denominator,(0,1,2,3))
denominator = K.sum(denominator)
gen_dice_coef = numerator/denominator
return 1-2*gen_dice_coef
Run Code Online (Sandbox Code Playgroud)
但是一定有问题。我正在处理3D图像,必须将其细分为4类(1个背景类和3个对象类,我有一个不平衡的数据集)。首先奇怪的事情:当我的火车损失和准确性训练中提高(和收敛非常快),我确认损失/精度是恒定的低谷时期(见图片)。其次,在对测试数据进行预测时,仅预测了背景类:我得到了恒定的体积。
我使用了完全相同的数据和脚本,但存在分类交叉熵损失,并得到了合理的结果(对象类别已细分)。这意味着我的实现存在问题。知道会是什么吗?
另外,我认为具有通用的骰子丢失实现对keras社区很有用,因为它似乎已用于大多数最近的语义分割任务中(至少在医学图像社区中)。
PS:对我来说,如何定义权重似乎很奇怪;我得到大约10 ^ -10的值。还有其他人尝试实现吗?我也测试了我的功能,但没有权重,但是遇到了同样的问题。
machine-learning deep-learning keras loss-function semantic-segmentation