-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss_functions.py
137 lines (113 loc) · 5.1 KB
/
loss_functions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import tensorflow as tf
import keras.backend as K
from keras.losses import binary_crossentropy
beta = 0.25
alpha = 0.25
gamma = 2
epsilon = 1e-5
smooth = 1
class Semantic_loss_functions(object):
def __init__(self):
print ("semantic loss functions initialized")
def dice_coef(self, 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 + K.epsilon()) / (
K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
def sensitivity(self, y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
return true_positives / (possible_positives + K.epsilon())
def specificity(self, y_true, y_pred):
true_negatives = K.sum(
K.round(K.clip((1 - y_true) * (1 - y_pred), 0, 1)))
possible_negatives = K.sum(K.round(K.clip(1 - y_true, 0, 1)))
return true_negatives / (possible_negatives + K.epsilon())
def convert_to_logits(self, y_pred):
y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(),
1 - tf.keras.backend.epsilon())
return tf.math.log(y_pred / (1 - y_pred))
def weighted_cross_entropyloss(self, y_true, y_pred):
y_pred = self.convert_to_logits(y_pred)
pos_weight = beta / (1 - beta)
loss = tf.nn.weighted_cross_entropy_with_logits(logits=y_pred,
targets=y_true,
pos_weight=pos_weight)
return tf.reduce_mean(loss)
def focal_loss_with_logits(self, logits, targets, alpha, gamma, y_pred):
weight_a = alpha * (1 - y_pred) ** gamma * targets
weight_b = (1 - alpha) * y_pred ** gamma * (1 - targets)
return (tf.math.log1p(tf.exp(-tf.abs(logits))) + tf.nn.relu(
-logits)) * (weight_a + weight_b) + logits * weight_b
def focal_loss(self, y_true, y_pred):
y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(),
1 - tf.keras.backend.epsilon())
logits = tf.math.log(y_pred / (1 - y_pred))
loss = self.focal_loss_with_logits(logits=logits, targets=y_true,
alpha=alpha, gamma=gamma, y_pred=y_pred)
return tf.reduce_mean(loss)
def depth_softmax(self, matrix):
sigmoid = lambda x: 1 / (1 + K.exp(-x))
sigmoided_matrix = sigmoid(matrix)
softmax_matrix = sigmoided_matrix / K.sum(sigmoided_matrix, axis=0)
return softmax_matrix
def generalized_dice_coefficient(self, 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)
score = (2. * intersection + smooth) / (
K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
return score
def dice_loss(self, y_true, y_pred):
loss = 1 - self.generalized_dice_coefficient(y_true, y_pred)
return loss
def bce_dice_loss(self, y_true, y_pred):
loss = binary_crossentropy(y_true, y_pred) + \
self.dice_loss(y_true, y_pred)
return loss / 2.0
def confusion(self, y_true, y_pred):
smooth = 1
y_pred_pos = K.clip(y_pred, 0, 1)
y_pred_neg = 1 - y_pred_pos
y_pos = K.clip(y_true, 0, 1)
y_neg = 1 - y_pos
tp = K.sum(y_pos * y_pred_pos)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
prec = (tp + smooth) / (tp + fp + smooth)
recall = (tp + smooth) / (tp + fn + smooth)
return prec, recall
def true_positive(self, y_true, y_pred):
smooth = 1
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pos = K.round(K.clip(y_true, 0, 1))
tp = (K.sum(y_pos * y_pred_pos) + smooth) / (K.sum(y_pos) + smooth)
return tp
def true_negative(self, y_true, y_pred):
smooth = 1
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
y_neg = 1 - y_pos
tn = (K.sum(y_neg * y_pred_neg) + smooth) / (K.sum(y_neg) + smooth)
return tn
def tversky_index(self, y_true, y_pred):
y_true_pos = K.flatten(y_true)
y_pred_pos = K.flatten(y_pred)
true_pos = K.sum(y_true_pos * y_pred_pos)
false_neg = K.sum(y_true_pos * (1 - y_pred_pos))
false_pos = K.sum((1 - y_true_pos) * y_pred_pos)
alpha = 0.7
return (true_pos + smooth) / (true_pos + alpha * false_neg + (
1 - alpha) * false_pos + smooth)
def tversky_loss(self, y_true, y_pred):
return 1 - self.tversky_index(y_true, y_pred)
def focal_tversky(self, y_true, y_pred):
pt_1 = self.tversky_index(y_true, y_pred)
gamma = 0.75
return K.pow((1 - pt_1), gamma)
def log_cosh_dice_loss(self, y_true, y_pred):
x = self.dice_loss(y_true, y_pred)
return tf.math.log((tf.exp(x) + tf.exp(-x)) / 2.0)