-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_losses.py
166 lines (128 loc) · 6.01 KB
/
custom_losses.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import tensorflow as tf
class CustomWeightedCategoricalCrossentropy(tf.keras.losses.Loss):
def __init__(self, class_weights, epsilon = 1e-9, name = 'wce', **kwargs):
super().__init__(name=name, **kwargs)
self.params = {'class_weights':class_weights, 'epsilon':epsilon}
self.class_weights = tf.convert_to_tensor(class_weights, tf.float32)
self.epsilon = epsilon
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
return -tf.reduce_mean(tf.reduce_sum(self.class_weights* y_true* tf.math.log(y_pred + self.epsilon), axis = [-1]))
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
class CustomMyDiceLoss(tf.keras.losses.Loss):
def __init__(self, smooth = 1.0 ,name = 'my_dice_loss', **kwargs):
super().__init__(name = name, **kwargs)
self.smooth = smooth
self.params = {'smooth':smooth}
def compute_dice_coef(self, y_true, y_pred):
intersection = tf.reduce_sum(y_true * y_pred, axis=[1,2])
union = tf.reduce_sum(y_true, [1,2]) + tf.reduce_sum(y_pred, [1,2])
score = (2.0 * intersection + self.smooth) / (union + self.smooth)
return tf.reduce_mean(score, axis = -1)
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
score = self.compute_dice_coef(y_true, y_pred)
return 1 - score
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
class CustomDiceLoss(tf.keras.losses.Loss):
def __init__(self, smooth = 1.0 ,name = 'dice_loss', **kwargs):
super().__init__(name = name, **kwargs)
self.smooth = smooth
self.params = {'smooth':smooth}
def compute_dice_coef(self, y_true, y_pred):
intersection = tf.reduce_sum(y_true * y_pred, axis=[1,2,3])
union = tf.reduce_sum(y_true, [1,2,3]) + tf.reduce_sum(y_pred, [1,2,3])
score = (2.0 * intersection + self.smooth) / (union + self.smooth)
return score
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
score = self.compute_dice_coef(y_true, y_pred)
return 1 - score
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
class CustomTverskyLoss(tf.keras.losses.Loss):
def __init__(self, alpha = 0.7, smooth = 1.0 ,name = 'tversky_loss', **kwargs):
super().__init__(name = name, **kwargs)
self.smooth = smooth
self.alpha = alpha
self.params = {'smooth':smooth, 'alpha':alpha}
def compute_tversky_index(self, y_true, y_pred):
true_pos = tf.reduce_sum(y_true * y_pred)
false_neg = tf.reduce_sum(y_true * (1 - y_pred))
false_pos = tf.reduce_sum((1 - y_true) * y_pred)
ti = (true_pos + self.smooth) / (true_pos + self.alpha * false_neg + (1 - self.alpha) * false_pos + self.smooth)
return ti
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
score = self.compute_tversky_index(y_true, y_pred)
return 1 - score
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
class CustomFocalTverskyLoss(tf.keras.losses.Loss):
def __init__(self, alpha = 0.7, gamma = 0.75, smooth = 1.0 ,name = 'focal_tversky', **kwargs):
super().__init__(name = name, **kwargs)
self.smooth = smooth
self.gamma = gamma
self.alpha = alpha
self.params = {'smooth':smooth, 'alpha':alpha, 'gamma':gamma}
def compute_tversky_index(self, y_true, y_pred):
true_pos = tf.reduce_sum(y_true * y_pred)
false_neg = tf.reduce_sum(y_true * (1 - y_pred))
false_pos = tf.reduce_sum((1 - y_true) * y_pred)
ti = (true_pos + self.smooth) / (true_pos + self.alpha * false_neg + (1 - self.alpha) * false_pos + self.smooth)
return ti
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
score = self.compute_tversky_index(y_true, y_pred)
return tf.pow(1-score, self.gamma)
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
class CustomLogDiceLoss(tf.keras.losses.Loss):
def __init__(self, smooth = 1.0 ,name = 'log_dice_loss', **kwargs):
super().__init__(name = name, **kwargs)
self.smooth = smooth
self.params = {'smooth':smooth}
def compute_dice_coef(self, y_true, y_pred):
intersection = tf.reduce_sum(y_true * y_pred, axis=[1,2,3])
union = tf.reduce_sum(y_true, [1,2,3]) + tf.reduce_sum(y_pred, [1,2,3])
score = (2.0 * intersection + self.smooth) / (union + self.smooth)
return score
def call(self, y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, tf.float32)
y_pred = tf.convert_to_tensor(y_pred, tf.float32)
score = self.compute_dice_coef(y_true, y_pred)
return tf.math.log((tf.exp(1-score) + tf.exp(-(1-score))) / 2.0)
def get_config(self):
config = super().get_config()
config.update(self.params)
return config
if __name__ == "__main__":
from unet import get_unet_128
import numpy as np
model = get_unet_128(num_classes = 3)
x = np.random.uniform(0,1, (10,128,128,3))
target = np.random.randint(0,2, (10,128,128,3))
prediction = model(x, training = True)
l = CustomWeightedCategoricalCrossentropy([1,1,1])
print(l(target, prediction))
l = tf.keras.losses.CategoricalCrossentropy()
print(l(target, prediction))
l = CustomDiceLoss()
print(l(target, prediction))