-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_segementation_loss.py
173 lines (136 loc) · 6.34 KB
/
tf_segementation_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
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
import tensorflow as tf
def binary_crossentropy(Y_pred, Y_gt):
epsilon = 1.e-5
Y_pred = tf.clip_by_value(Y_pred, epsilon, 1. - epsilon)
logits = tf.log(Y_pred / (1 - Y_pred))
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=Y_gt, logits=logits)
loss = tf.reduce_mean(loss)
return loss
def dice_loss_3d(Y_gt, Y_pred):
Z, H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
pred_flat = tf.reshape(Y_pred, [-1, H * W * C * Z])
true_flat = tf.reshape(Y_gt, [-1, H * W * C * Z])
intersection = 2 * tf.reduce_sum(pred_flat * true_flat, axis=1) + smooth
denominator = tf.reduce_sum(pred_flat, axis=1) + tf.reduce_sum(true_flat, axis=1) + smooth
loss = 1 - tf.reduce_mean(intersection / denominator)
return loss
def dice_loss_2d(Y_gt, Y_pred):
H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
pred_flat = tf.reshape(Y_pred, [-1, H * W * C])
true_flat = tf.reshape(Y_gt, [-1, H * W * C])
intersection = 2 * tf.reduce_sum(pred_flat * true_flat, axis=1) + smooth
denominator = tf.reduce_sum(pred_flat, axis=1) + tf.reduce_sum(true_flat, axis=1) + smooth
loss = 1 - tf.reduce_mean(intersection / denominator)
return loss
def tversky_loss_3d(Y_gt, Y_pred, alpha=0.7):
Z, H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
y_pred_pos = tf.reshape(Y_pred, [-1, H * W * C * Z])
y_true_pos = tf.reshape(Y_gt, [-1, H * W * C * Z])
true_pos = tf.reduce_sum(y_true_pos * y_pred_pos, axis=1)
false_neg = tf.reduce_sum(y_true_pos * (1 - y_pred_pos), axis=1)
false_pos = tf.reduce_sum((1 - y_true_pos) * y_pred_pos, axis=1)
tversky = (true_pos + smooth) / (true_pos + alpha * false_neg + (1 - alpha) * false_pos + smooth)
loss = 1 - tf.reduce_mean(tversky)
return loss
def tversky_loss_2d(Y_gt, Y_pred, alpha=0.7):
H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
y_pred_pos = tf.reshape(Y_pred, [-1, H * W * C])
y_true_pos = tf.reshape(Y_gt, [-1, H * W * C])
true_pos = tf.reduce_sum(y_true_pos * y_pred_pos, axis=1)
false_neg = tf.reduce_sum(y_true_pos * (1 - y_pred_pos), axis=1)
false_pos = tf.reduce_sum((1 - y_true_pos) * y_pred_pos, axis=1)
tversky = (true_pos + smooth) / (true_pos + alpha * false_neg + (1 - alpha) * false_pos + smooth)
loss = 1 - tf.reduce_mean(tversky)
return loss
def focal_tversky_3d(Y_gt, Y_pred, alpha=0.7, gamma=0.75):
Z, H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
y_pred_pos = tf.reshape(Y_pred, [-1, H * W * C * Z])
y_true_pos = tf.reshape(Y_gt, [-1, H * W * C * Z])
true_pos = tf.reduce_sum(y_true_pos * y_pred_pos, axis=1)
false_neg = tf.reduce_sum(y_true_pos * (1 - y_pred_pos), axis=1)
false_pos = tf.reduce_sum((1 - y_true_pos) * y_pred_pos, axis=1)
tversky = (true_pos + smooth) / (true_pos + alpha * false_neg + (1 - alpha) * false_pos + smooth)
loss = 1 - tf.reduce_mean(tversky)
loss = tf.pow(loss, gamma)
return loss
def focal_tversky_2d(Y_gt, Y_pred, alpha=0.7, gamma=0.75):
H, W, C = Y_gt.get_shape().as_list()[1:]
smooth = 1e-5
y_pred_pos = tf.reshape(Y_pred, [-1, H * W * C])
y_true_pos = tf.reshape(Y_gt, [-1, H * W * C])
true_pos = tf.reduce_sum(y_true_pos * y_pred_pos, axis=1)
false_neg = tf.reduce_sum(y_true_pos * (1 - y_pred_pos), axis=1)
false_pos = tf.reduce_sum((1 - y_true_pos) * y_pred_pos, axis=1)
tversky = (true_pos + smooth) / (true_pos + alpha * false_neg + (1 - alpha) * false_pos + smooth)
loss = 1 - tf.reduce_mean(tversky)
loss = tf.pow(loss, gamma)
return loss
def generalised_dice_loss_3d(Y_gt, Y_pred):
smooth = 1e-5
w = tf.reduce_sum(Y_gt, axis=[1, 2, 3])
w = 1 / (w ** 2 + smooth)
numerator = Y_gt * Y_pred
numerator = w * tf.reduce_sum(numerator, axis=[1, 2, 3])
numerator = tf.reduce_sum(numerator, axis=1)
denominator = Y_pred + Y_gt
denominator = w * tf.reduce_sum(denominator, axis=[1, 2, 3])
denominator = tf.reduce_sum(denominator, axis=1)
gen_dice_coef = 2 * numerator / (denominator + smooth)
loss = tf.reduce_mean(1 - gen_dice_coef)
return loss
def generalised_dice_loss_2d_ein(Y_gt, Y_pred):
Y_gt = tf.cast(Y_gt, 'float32')
Y_pred = tf.cast(Y_pred, 'float32')
w = tf.einsum("bwhc->bc", Y_gt)
w = 1 / ((w + 1e-10) ** 2)
intersection = w * tf.einsum("bwhc,bwhc->bc", Y_pred, Y_gt)
union = w * (tf.einsum("bwhc->bc", Y_pred) + tf.einsum("bwhc->bc", Y_gt))
divided = 1 - 2 * (tf.einsum("bc->b", intersection) + 1e-10) / (tf.einsum("bc->b", union) + 1e-10)
loss = tf.reduce_mean(divided)
return loss
def generalised_dice_loss_2d(Y_gt, Y_pred):
smooth = 1e-5
w = tf.reduce_sum(Y_gt, axis=[1, 2])
w = 1 / (w ** 2 + smooth)
numerator = Y_gt * Y_pred
numerator = w * tf.reduce_sum(numerator, axis=[1, 2])
numerator = tf.reduce_sum(numerator, axis=1)
denominator = Y_pred + Y_gt
denominator = w * tf.reduce_sum(denominator, axis=[1, 2])
denominator = tf.reduce_sum(denominator, axis=1)
gen_dice_coef = 2 * numerator / (denominator + smooth)
loss = tf.reduce_mean(1 - gen_dice_coef)
return loss
def surface_loss_3d(Y_gt, Y_pred):
multipled = tf.reduce_sum(Y_gt * Y_pred, axis=[0,1, 2, 3, 4])
loss = tf.reduce_mean(multipled)
return loss
def surface_loss_2d(Y_gt, Y_pred):
multipled = tf.reduce_sum(Y_gt * Y_pred, axis=[0,1, 2, 3])
loss = tf.reduce_mean(multipled)
return loss
def focal_loss_sigmodv1(Y_gt, Y_pred,alpha=0.25, gamma=2):
epsilon = 1e-5
pt_1 = tf.where(tf.equal(Y_gt, 1), Y_pred, tf.ones_like(Y_pred))
pt_0 = tf.where(tf.equal(Y_gt, 0), Y_pred, tf.zeros_like(Y_pred))
# clip to prevent NaN's and Inf's
pt_1 = tf.clip_by_value(pt_1, epsilon, 1. - epsilon)
pt_0 = tf.clip_by_value(pt_0, epsilon, 1. - epsilon)
loss_1 = -alpha * tf.pow(1. - pt_1, gamma) * tf.log(pt_1)
loss_0 = -(1 - alpha) * tf.pow(pt_0, gamma) * tf.log(1. - pt_0)
loss = tf.reduce_sum(loss_1 + loss_0)
return loss
def focal_loss_sigmodv2(Y_gt, Y_pred,alpha=0.25, gamma=2):
epsilon = 1e-5
y_pred = tf.clip_by_value(y_pred, epsilon, 1 - epsilon)
logits = tf.log(y_pred / (1 - y_pred))
weight_a = alpha * tf.pow((1 - y_pred), gamma) * y_true
weight_b = (1 - alpha) * tf.pow(y_pred, gamma) * (1 - y_true)
loss = tf.log1p(tf.exp(-logits)) * (weight_a + weight_b) + logits * weight_b
return tf.reduce_sum(loss)
return loss