Log loss
log loss 就是keras
中的binary_crossentropy()
对上式进行拆分可以得到
经过拆分后可以发现,predict与target越接近,损失越小,最终的损失是两类损失的加权 和。缺点就是当正样本数量远小于负样本数量时,使得训练的模型偏向于预测背景,因为这会获得更小的损失。
Dice loss
Dice loss 常用来解决前景区域所占像素非常小的分割问题,定义为:
也可以表示为:
其中TP, FP, FN
分别是真阳性、假阳性、假阴性的个数,关于TP, TN, FP, FN
的说明
二分类dice loss为:
keras
上的实现为:
1
2
3
4
5
6
def dice_coef(y_true, y_pred, smooth=1):
intersection = K.sum(y_true * y_pred, axis=[1,2,3])
union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])
return K.mean((2. * intersection + smooth) / (union + smooth), axis=0)
def dice_coef_loss(y_true, y_pred):
1 - dice_coef(y_true, y_pred, smooth=1)
多分类dice loss的实现在大多数的博客中都是这样写的:
1
2
3
4
5
6
7
8
9
def dice_coef(y_true, y_pred, smooth=1):
mean_loss = 0
for i in range(y_pred.shape(-1)):
intersection = K.sum(y_true[:,:,:,i] * y_pred[:,:,:,i], axis=[1,2,3])
union = K.sum(y_true[:,:,:,i], axis=[1,2,3]) + k.sum(y_pred[:,:,:,i],axis=[1,2,3])
mean_loss += (2. * intersection + smooth) / (union + smooth)
return K.mean(mean_loss, axis=0)
def dice_coef_loss(y_true, y_pred):
1 - dice_coef(y_true, y_pred, smooth=1)
但是参照pytorch
上的实现,我觉得上面的实现应该改为:
1
2
3
4
5
6
7
8
9
def dice_coef(y_true, y_pred, smooth=1):
mean_loss = 0
for i in range(y_pred.shape(-1)):
intersection = K.sum(y_true[:,:,:,i] * y_pred[:,:,:,i], axis=[1,2,3])
union = K.sum(y_true[:,:,:,i], axis=[1,2,3]) + k.sum(y_pred[:,:,:,i],axis=[1,2,3])
mean_loss += (2. * intersection + smooth) / (union + smooth)
return K.mean(mean_loss, axis=0)
def dice_coef_loss(y_true, y_pred):
1 - dice_coef(y_true, y_pred, smooth=1)
Focal loss
focal loss公式如下:
α 称作平衡因子,用来平衡正负样本本身的比例不均,α>0.5时可以相对增加正样本的比例;在gamma>0的情况下,focal loss更关注难分类的样本。
如 当 γ = 2,
对于正样本,即y=1时,上式只关注前项,若y’=0.9,表明这一类容易区分,那么(1-0.9)^2会很小;若y’=0.2,表明这一类难分,那么(1-0.2)^2会很大;
对于负样本,即y=0时,上式只关注后项,若y’=0.8,表明这一类难分,那么0.8^2会很大;若y’=0.1,表明这一类容易区分,那么0.1^2会很小。
BCE
+ DICE LOSS
1
2
def bce_logdice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred) - K.log(1. - dice_loss(y_true, y_pred))
IOU loss
1
2
3
4
5
6
def IoU(y_true, y_pred, eps=1e-6):
if np.max(y_true) == 0.0:
return IoU(1-y_true, 1-y_pred)
intersection = K.sum(y_true * y_pred, axis=[1,2,3])
union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
return -K.mean( (intersection + eps) / (union + eps), axis=0)
参考
[1] https://blog.csdn.net/m0_37477175/article/details/83004746?utm_source=blogxgwz9 [2] https://blog.csdn.net/wangdongwei0/article/details/84576044 [3] https://blog.csdn.net/a362682954/article/details/81226427