[d6d24a]: / Segmentation / utils / losses.py

Download this file

191 lines (144 with data), 6.0 kB

  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
# import numpy as np
from tensorflow.keras.losses import binary_crossentropy, categorical_crossentropy
import tensorflow.keras.backend as K
import tensorflow as tf
epsilon = 1e-5
smooth = 1
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def dice_coef(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)
return (2. * intersection + smooth) / (K.sum(y_true_f * y_true_f) + K.sum(y_pred_f * y_pred_f) + smooth)
def dsc(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
loss = 1 - dsc(y_true, y_pred)
return loss
def tversky_loss(y_true, y_pred, alpha=0.5, beta=0.5, smooth=1e-10):
""" Tversky loss function.
Parameters
----------
y_true : tensor containing target mask.
y_pred : tensor containing predicted mask.
alpha : real value, weight of '0' class.
beta : real value, weight of '1' class.
smooth : small real value used for avoiding division by zero error.
Returns
-------
tensor
tensor containing tversky loss.
"""
y_true = K.flatten(y_true)
y_pred = K.flatten(y_pred)
truepos = K.sum(y_true * y_pred)
fp_and_fn = alpha * K.sum(y_pred * (1 - y_true)) + beta * K.sum((1 - y_pred) * y_true)
answer = (truepos + smooth) / ((truepos + smooth) + fp_and_fn)
return 1 - answer
def tversky_crossentropy(y_true, y_pred):
tversky = tversky_loss(y_true, y_pred)
crossentropy = K.categorical_crossentropy(y_true, y_pred)
crossentropy = K.mean(crossentropy)
return tversky + crossentropy
def iou_loss(y_true, y_pred, smooth=1):
y_true = K.flatten(y_true)
y_pred = K.flatten(y_pred)
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
union = K.sum(y_true, -1) + K.sum(y_pred, -1) - intersection
iou = (intersection + smooth) / (union + smooth)
return iou
def bce_dice_loss(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
loss = binary_crossentropy(y_true, y_pred) + dice_coef_loss(y_true, y_pred)
return loss
def cce_dice_loss(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
loss = categorical_crossentropy(y_true, y_pred) + dice_coef_loss(y_true, y_pred)
return loss
def iou_loss_eval_3d(y_true, y_pred):
y_true = tf.slice(y_true, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
y_pred = tf.slice(y_pred, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
iou = iou_loss(y_true, y_pred)
return iou
def dice_coef_eval_3d(y_true, y_pred):
y_true = tf.slice(y_true, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
y_pred = tf.slice(y_pred, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
dice = dsc(y_true, y_pred)
return dice
def dice_loss_weighted_3d(y_true, y_pred):
dice_whole = 1 - dsc(y_true, y_pred)
y_true = tf.slice(y_true, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
y_pred = tf.slice(y_pred, [0, 0, 0, 0, 1], [-1, -1, -1, -1, 6])
dice = 1 - dsc(y_true, y_pred)
return dice + dice_whole
def precision(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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)
prec = (tp + smooth) / (tp + fp + smooth)
return prec
def recall(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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)
fn = K.sum(y_pos * y_pred_neg)
recall = (tp + smooth) / (tp + fn + smooth)
return recall
def confusion(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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 tp(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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 tn(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
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 focal_tversky(y_true, y_pred):
# https://github.com/nabsabraham/focal-tversky-unet/blob/master/losses.py
pt_1 = tversky_loss(y_true, y_pred)
gamma = 0.75
return K.pow((pt_1), gamma)
def weighted_cat_cross_entropy(y_true, y_pred, class_weights):
class_weights = tf.reduce_sum(y_true, axis=-1, keepdims=True) / tf.reduce_sum(y_true)
weights = tf.reduce_sum(class_weights * tf.cast(y_true, tf.float32), axis=-1)
unweighted_losses = categorical_crossentropy(tf.cast(y_true, tf.float32), tf.cast(y_pred, tf.float32))
weighted_losses = tf.cast(unweighted_losses, tf.float32) * tf.cast(weights, tf.float32)
loss = tf.reduce_mean(weighted_losses)
return loss