-
Notifications
You must be signed in to change notification settings - Fork 5
/
combo_loss.py
23 lines (19 loc) · 779 Bytes
/
combo_loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from keras import backend as K
ce_w = 0.5
ce_d_w = 0.5
e = K.epsilon()
smooth = 1
'''
ce_w values smaller than 0.5 penalize false positives more while values larger than 0.5 penalize false negatives more
ce_d_w is level of contribution of the cross-entropy loss in the total loss.
'''
def Combo_loss(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
d = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
y_pred_f = K.clip(y_pred_f, e, 1.0 - e)
out = - (ce_w * y_true_f * K.log(y_pred_f)) + ((1 - ce_w) * (1.0 - y_true_f) * K.log(1.0 - y_pred_f))
weighted_ce = K.mean(out, axis=-1)
combo = (ce_d_w * weighted_ce) - ((1 - ce_d_w) * d)
return combo