-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
49 lines (37 loc) · 1.38 KB
/
loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import keras
import keras.backend as K
import tensorflow as tf
smooth = 1.
def dice_coef(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)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def _CE(y_true, y_pred):
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
CE_loss = - y_true[...] * K.log(y_pred)
CE_loss = K.mean(K.sum(CE_loss, axis = -1))
# dice_loss = tf.Print(CE_loss, [CE_loss])
return CE_loss
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
class modelLoss():
def __init__(self, lambda_, alpha, width, height, batchsize):
self.lambda_ = lambda_
self.width = width
self.height = height
self.batchsize = batchsize
self.alpha = alpha
def test(self, y_true, y_pred):
# rename and split values
# [batch, width, height, channel]
img = y_true[:,:,:,0:3]
seg = y_true[:,:,:,3:6]
disp0 = K.expand_dims(y_pred[:,:,:,0],-1)
disp1 = K.expand_dims(y_pred[:,:,:,1],-1)
disp2 = K.expand_dims(y_pred[:,:,:,2],-1)
disp3 = K.expand_dims(y_pred[:,:,:,3],-1)
return None
def applyLoss(self, y_true, y_pred):
return _CE(y_true, y_pred)
#return L_p