-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
275 lines (223 loc) · 10.8 KB
/
utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from tensorflow.keras.losses import Loss
import tensorflow as tf
class WeightedBinaryCE(Loss):
# initialize instance attributes
def __init__(self, classWeights, labelSmoothing=0.0):
super(WeightedBinaryCE, self).__init__()
self.labelSmoothing = tf.constant(labelSmoothing, dtype=tf.dtypes.float32)
self.classWeights = tf.constant(classWeights, dtype=tf.dtypes.float32)
# Compute loss
def call(self, y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
if self.labelSmoothing > 0:
y_true = y_true * (1 - self.labelSmoothing) + self.labelSmoothing / 2
y_pred = tf.cast(y_pred, tf.float32)
y_pred = tf.clip_by_value(
y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon()
)
term_0 = tf.math.multiply(
self.classWeights * tf.math.subtract(1.0, y_true),
tf.math.log(tf.math.subtract(1.0, y_pred) + tf.keras.backend.epsilon()),
)
term_1 = tf.math.multiply(
self.classWeights * y_true, tf.math.log(y_pred + tf.keras.backend.epsilon())
)
losses = term_0 + term_1
return -tf.reduce_sum(losses, axis=0) / tf.reduce_sum(self.classWeights)
class DiceLoss(Loss):
# initialize instance attributes
def __init__(self):
super(DiceLoss, self).__init__()
# Compute loss
def call(self, y_true, y_pred, smooth=1e-6):
inputs = tf.squeeze(tf.cast(y_pred, tf.dtypes.float32))
targets = tf.squeeze(tf.cast(y_true, tf.dtypes.float32))
intersection = tf.math.reduce_sum(tf.math.multiply(targets, inputs))
dice = (2 * intersection + smooth) / (
tf.math.reduce_sum(targets) + tf.math.reduce_sum(inputs) + smooth
)
return 1 - dice
# α controls the amount of Dice term contribution in the loss f
# β ∈ [0, 1] controls the level of model penalization for false positives/negatives: when β is set to
# a value smaller than 0.5, FP are penalized more than FN
class WeightedComboLoss(Loss):
# initialize instance attributes
def __init__(self, labelWeights, alpha=0.5, beta=0.5, labelSmoothing=0.0):
super(WeightedComboLoss, self).__init__()
self.classWeights = tf.constant(labelWeights, dtype=tf.dtypes.float32)
self.alpha = tf.constant(alpha, dtype=tf.dtypes.float32)
self.beta = tf.constant(beta, dtype=tf.dtypes.float32)
self.labelSmoothing = tf.constant(labelSmoothing, dtype=tf.dtypes.float32)
# Compute loss
def call(self, y_true, y_pred, smooth=1e-6):
inputs = tf.squeeze(tf.cast(y_pred, tf.dtypes.float32))
targets = tf.squeeze(tf.cast(y_true, tf.dtypes.float32))
intersection = tf.math.reduce_sum(tf.math.multiply(targets, inputs))
dice = (2 * intersection + smooth) / (
tf.math.reduce_sum(targets) + tf.math.reduce_sum(inputs) + smooth
)
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
y_pred = tf.clip_by_value(
y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon()
)
if self.labelSmoothing > 0:
y_true = y_true * (1 - self.labelSmoothing) + self.labelSmoothing / 2
term_0 = tf.math.multiply(
(1 - self.beta) * self.classWeights * tf.math.subtract(1.0, y_true),
tf.math.log(tf.math.subtract(1.0, y_pred) + tf.keras.backend.epsilon()),
)
term_1 = tf.math.multiply(
self.beta * self.classWeights * y_true,
tf.math.log(y_pred + tf.keras.backend.epsilon()),
)
losses = term_0 + term_1
weightedCE = -tf.reduce_sum(losses, axis=0) / tf.reduce_sum(self.classWeights)
combo = (self.alpha * weightedCE) - ((1 - self.alpha) * dice)
return combo
class WeightedF1(tf.keras.metrics.Metric):
def __init__(self, classWeights, threshold=0.5):
super(WeightedF1, self).__init__()
self.classWeights = tf.constant(classWeights, dtype=tf.dtypes.float32)
self.threshold = tf.constant(threshold, dtype=tf.dtypes.float32)
self.f1 = self.add_weight(name="f1", initializer="zeros")
self.total = self.add_weight("total", initializer="zeros")
def update_state(self, y_true, y_pred, sample_weight=None):
m = tf.math.count_nonzero(tf.reduce_max(y_pred, axis=1))
n = tf.shape(y_pred)[0]
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.math.greater_equal(y_pred, self.threshold)
tp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, self.dtype)
tp = tf.math.multiply(tp, self.classWeights)
tp = tf.cast(tp, self.dtype)
tn = tf.math.logical_and(tf.equal(y_true, False), tf.equal(y_pred, False))
tn = tf.cast(tn, self.dtype)
tn = tf.math.multiply(tn, self.classWeights)
tn = tf.cast(tn, self.dtype)
fp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
fp = tf.cast(fp, self.dtype)
fp = tf.math.multiply(fp, self.classWeights)
fp = tf.cast(fp, self.dtype)
fn = tf.math.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
fn = tf.cast(fn, self.dtype)
fn = tf.math.multiply(fn, self.classWeights)
fn = tf.cast(fn, self.dtype)
pr = tf.math.divide(
tf.math.reduce_sum(tp, axis=1),
tf.math.reduce_sum(tp + fp + tf.keras.backend.epsilon(), axis=1),
)
m = tf.cast(m, pr.dtype)
if m > 0:
wPr = tf.math.reduce_sum(pr) / m
else:
wPr = tf.constant(0, tf.dtypes.float32)
re = tf.math.divide(
tf.math.reduce_sum(tp, axis=1),
tf.math.reduce_sum(tp + fn + tf.keras.backend.epsilon(), axis=1),
)
n = tf.cast(n, re.dtype)
wRe = tf.math.reduce_sum(re) / n
res = tf.math.divide(2 * wPr * wRe, wPr + wRe + tf.keras.backend.epsilon())
self.f1.assign_add(res)
self.total.assign_add(1)
def result(self):
return tf.math.divide(self.f1, self.total)
class WeightedPrecision(tf.keras.metrics.Metric):
def __init__(self, classWeights, threshold=0.5):
super(WeightedPrecision, self).__init__()
self.classWeights = tf.constant(classWeights, dtype=tf.dtypes.float32)
self.threshold = tf.constant(threshold, dtype=tf.dtypes.float32)
self.prec = self.add_weight(name="prec", initializer="zeros")
self.total = self.add_weight("total", initializer="zeros")
def update_state(self, y_true, y_pred, sample_weight=None):
m = tf.math.count_nonzero(tf.reduce_max(y_pred, axis=1))
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.math.greater_equal(y_pred, self.threshold)
tp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, self.dtype)
tp = tf.math.multiply(tp, self.classWeights)
tp = tf.cast(tp, self.dtype)
fp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
fp = tf.cast(fp, self.dtype)
fp = tf.math.multiply(fp, self.classWeights)
fp = tf.cast(fp, self.dtype)
pr = tf.math.divide(
tf.math.reduce_sum(tp, axis=1),
tf.math.reduce_sum(tp + fp + tf.keras.backend.epsilon(), axis=1),
)
m = tf.cast(m, pr.dtype)
if m > 0:
wPr = tf.math.reduce_sum(pr) / m
else:
wPr = tf.constant(0, tf.dtypes.float32)
self.prec.assign_add(wPr)
self.total.assign_add(1)
def result(self):
return tf.math.divide(self.prec, self.total)
class WeightedRecall(tf.keras.metrics.Metric):
def __init__(self, classWeights, threshold=0.5):
super(WeightedRecall, self).__init__()
self.classWeights = tf.constant(classWeights, dtype=tf.dtypes.float32)
self.threshold = tf.constant(threshold, dtype=tf.dtypes.float32)
self.rec = self.add_weight(name="rec", initializer="zeros")
self.total = self.add_weight("total", initializer="zeros")
def update_state(self, y_true, y_pred, sample_weight=None):
n = tf.shape(y_pred)[0]
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.math.greater_equal(y_pred, self.threshold)
tp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, self.dtype)
tp = tf.math.multiply(tp, self.classWeights)
tp = tf.cast(tp, self.dtype)
fn = tf.math.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
fn = tf.cast(fn, self.dtype)
fn = tf.math.multiply(fn, self.classWeights)
fn = tf.cast(fn, self.dtype)
re = tf.math.divide(
tf.math.reduce_sum(tp, axis=1),
tf.math.reduce_sum(tp + fn + tf.keras.backend.epsilon(), axis=1),
)
n = tf.cast(n, re.dtype)
wRe = tf.math.reduce_sum(re) / n
self.rec.assign_add(wRe)
self.total.assign_add(1)
def result(self):
return tf.math.divide(self.rec, self.total)
class WeightedAccuracy(tf.keras.metrics.Metric):
def __init__(self, classWeights, threshold=0.5):
super(WeightedAccuracy, self).__init__()
self.classWeights = tf.constant(classWeights, dtype=tf.dtypes.float32)
self.threshold = tf.constant(threshold, dtype=tf.dtypes.float32)
self.acc = self.add_weight(name="acc", initializer="zeros")
self.total = self.add_weight("total", initializer="zeros")
def update_state(self, y_true, y_pred, sample_weight=None):
n = tf.shape(y_pred)[0]
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.math.greater_equal(y_pred, self.threshold)
tp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, self.dtype)
tp = tf.math.multiply(tp, self.classWeights)
tp = tf.cast(tp, self.dtype)
tn = tf.math.logical_and(tf.equal(y_true, False), tf.equal(y_pred, False))
tn = tf.cast(tn, self.dtype)
tn = tf.math.multiply(tn, self.classWeights)
tn = tf.cast(tn, self.dtype)
fp = tf.math.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
fp = tf.cast(fp, self.dtype)
fp = tf.math.multiply(fp, self.classWeights)
fp = tf.cast(fp, self.dtype)
fn = tf.math.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
fn = tf.cast(fn, self.dtype)
fn = tf.math.multiply(fn, self.classWeights)
fn = tf.cast(fn, self.dtype)
acc = tf.math.divide(
tf.math.reduce_sum(tp + tn, axis=1),
tf.math.reduce_sum(tp + fn + tn + fp + tf.keras.backend.epsilon(), axis=1),
)
n = tf.cast(n, acc.dtype)
wAcc = tf.math.reduce_sum(acc) / n
self.acc.assign_add(wAcc)
self.total.assign_add(1)
def result(self):
return tf.math.divide(self.acc, self.total)