-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
288 lines (250 loc) · 10.8 KB
/
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
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
276
277
278
279
280
281
282
283
284
285
286
287
import keras.backend as K
import tensorflow as tf
import numpy as np
import keras
from keras.utils import np_utils
def get_iou_vector(A, B):
# Numpy version
batch_size = 1
#metric = 0.0
for batch in range(batch_size):
t, p = A, B
true = np.sum(t)
pred = np.sum(p)
# deal with empty mask first
if true == 0:
iouf = ((480*640)-pred)/(480*640)
else:
# non empty mask case. Union is never empty
# hence it is safe to divide by its number of pixels
intersection = np.sum(t * p)
union = true + pred - intersection
iouf = intersection / union # real iou
return iouf
def mean_iou(A, B):
# Numpy version
batch_size = A.shape[0]
metric = 0.0
def get_iou_vector(A, B):
# Numpy version
t, p = A, B
true = np.sum(t)
pred = np.sum(p)
# deal with empty mask first
if true == 0:
iouf = ((480*640)-pred)/(480*640)
else:
# non empty mask case. Union is never empty
# hence it is safe to divide by its number of pixels
intersection = np.sum(t * p)
union = true + pred - intersection
iouf = intersection / union # real iou
return iouf
for batch in range(batch_size):
label = A[batch]
preds = B[batch]
boolmulti = preds>0.1
label_back = np.where(label > 0, 0 , 1)
label_cell = np.where(label == 2, 1, 0)
label_bead = np.where(label ==1 , 1, 0)
iouback = get_iou_vector(label_back[:,:,0],boolmulti[:,:,0])
ioubead = get_iou_vector(label_bead[:,:,0],boolmulti[:,:,1])
ioucell = get_iou_vector(label_cell[:,:,0],boolmulti[:,:,2])
finaliou = (iouback + ioubead+ioucell)/3
metric += finaliou
# teake the average over all images in batch
metric /= batch_size
#print('metric=',metric)
return metric
def iou_lovasz_multi(label,pred):
return tf.py_func(mean_iou,[label,pred],tf.float64)
# """
# Lovasz-Softmax and Jaccard hinge loss in Tensorflow
# Maxim Berman 2018 ESAT-PSI KU Leuven (MIT License)
# """
# code download from: https://github.com/bermanmaxim/LovaszSoftmax
# MIT License
# Copyright (c) 2018 Maxim Berman
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def lovasz_loss(y_true, y_pred):
y_true, y_pred = K.cast(K.squeeze(y_true, -1), 'int32'), K.cast(K.squeeze(y_pred, -1), 'float32')
#logits = K.log(y_pred / (1. - y_pred)) # the original code
logits = y_pred # https://www.kaggle.com/shaojiaxin/u-net-with-simple-resnet-blocks-v2-new-loss
loss = lovasz_hinge(logits, y_true, per_image=True, ignore=None)
return loss
# https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/69053#406866
def symmetric_lovasz_loss(y_true, y_pred):
y_true, y_pred = K.cast(K.squeeze(y_true, -1), 'int32'), K.cast(K.squeeze(y_pred, -1), 'float32')
#logits = K.log(y_pred / (1. - y_pred))
logits = y_pred
loss = (lovasz_hinge(logits, y_true) + lovasz_hinge(-logits, 1 - y_true)) / 2
return loss
def lovasz_grad(gt_sorted):
"""
Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper
"""
gts = tf.reduce_sum(gt_sorted)
intersection = gts - tf.cumsum(gt_sorted)
union = gts + tf.cumsum(1. - gt_sorted)
jaccard = 1. - intersection / union
jaccard = tf.concat((jaccard[0:1], jaccard[1:] - jaccard[:-1]), 0)
return jaccard
# --------------------------- BINARY LOSSES ---------------------------
def lovasz_hinge(logits, labels, per_image=True, ignore=None):
"""
Binary Lovasz hinge loss
logits: [B, H, W] Variable, logits at each pixel (between -\infty and +\infty)
labels: [B, H, W] Tensor, binary ground truth masks (0 or 1)
per_image: compute the loss per image instead of per batch
ignore: void class id
"""
if per_image:
def treat_image(log_lab):
log, lab = log_lab
log, lab = tf.expand_dims(log, 0), tf.expand_dims(lab, 0)
log, lab = flatten_binary_scores(log, lab, ignore)
return lovasz_hinge_flat(log, lab)
losses = tf.map_fn(treat_image, (logits, labels), dtype=tf.float32)
# Fixed python3
losses.set_shape((None,))
loss = tf.reduce_mean(losses)
else:
loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
return loss
def lovasz_hinge_flat(logits, labels):
"""
Binary Lovasz hinge loss
logits: [P] Variable, logits at each prediction (between -\infty and +\infty)
labels: [P] Tensor, binary ground truth labels (0 or 1)
ignore: label to ignore
"""
def compute_loss():
labelsf = tf.cast(labels, logits.dtype)
signs = 2. * labelsf - 1.
errors = 1. - logits * tf.stop_gradient(signs)
errors_sorted, perm = tf.nn.top_k(errors, k=tf.shape(errors)[0], name="descending_sort")
gt_sorted = tf.gather(labelsf, perm)
grad = lovasz_grad(gt_sorted)
# loss = tf.tensordot(tf.nn.relu(errors_sorted), tf.stop_gradient(grad), 1, name="loss_non_void")
# ELU + 1
loss = tf.tensordot(tf.nn.elu(errors_sorted) + 1., tf.stop_gradient(grad), 1, name="loss_non_void")
return loss
# deal with the void prediction case (only void pixels)
loss = tf.cond(tf.equal(tf.shape(logits)[0], 0),
lambda: tf.reduce_sum(logits) * 0.,
compute_loss,
strict=True,
name="loss"
)
return loss
def flatten_binary_scores(scores, labels, ignore=None):
"""
Flattens predictions in the batch (binary case)
Remove labels equal to 'ignore'
"""
scores = tf.reshape(scores, (-1,))
labels = tf.reshape(labels, (-1,))
if ignore is None:
return scores, labels
valid = tf.not_equal(labels, ignore)
vscores = tf.boolean_mask(scores, valid, name='valid_scores')
vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
return vscores, vlabels
# --------------------------- MULTICLASS LOSSES ---------------------------
def lovasz_softmax(labels, probas, classes='present', per_image=False, ignore=None, order='BHWC'):
#print(K.shape(labels))
#print(labels)
#print(type(labels))
#def lovasz_softmax(probas, labels, classes='present', per_image=False, ignore=None, order='BHWC'):
"""
Multi-class Lovasz-Softmax loss
probas: [B, H, W, C] or [B, C, H, W] Variable, class probabilities at each prediction (between 0 and 1)
Interpreted as binary (sigmoid) output with outputs of size [B, H, W].
labels: [B, H, W] Tensor, ground truth labels (between 0 and C - 1)
classes: 'all' for all, 'present' for classes present in labels, or a list of classes to average.
per_image: compute the loss per image instead of per batch
ignore: void class labels
order: use BHWC or BCHW
"""
if per_image:
def treat_image(prob_lab):
prob, lab = prob_lab
prob, lab = tf.expand_dims(prob, 0), tf.expand_dims(lab, 0)
prob, lab = flatten_probas(prob, lab, ignore, order)
return lovasz_softmax_flat(prob, lab, classes=classes)
losses = tf.map_fn(treat_image, (probas, labels), dtype=tf.float32)
loss = tf.reduce_mean(losses)
else:
loss = lovasz_softmax_flat(*flatten_probas(probas, labels, ignore, order), classes=classes)
return loss
def lovasz_softmax_flat(probas, labels, classes='present'):
"""
Multi-class Lovasz-Softmax loss
probas: [P, C] Variable, class probabilities at each prediction (between 0 and 1)
labels: [P] Tensor, ground truth labels (between 0 and C - 1)
classes: 'all' for all, 'present' for classes present in labels, or a list of classes to average.
"""
C = probas.shape[1]
losses = []
present = []
class_to_sum = list(range(C)) if classes in ['all', 'present'] else classes
for c in class_to_sum:
fg = tf.cast(tf.equal(labels, c), probas.dtype) # foreground for class c
if classes == 'present':
present.append(tf.reduce_sum(fg) > 0)
if C == 1:
if len(classes) > 1:
raise ValueError('Sigmoid output possible only with 1 class')
class_pred = probas[:, 0]
else:
class_pred = probas[:, c]
errors = tf.abs(fg - class_pred)
errors_sorted, perm = tf.nn.top_k(errors, k=tf.shape(errors)[0], name="descending_sort_{}".format(c))
fg_sorted = tf.gather(fg, perm)
grad = lovasz_grad(fg_sorted)
losses.append(
tf.tensordot(errors_sorted, tf.stop_gradient(grad), 1, name="loss_class_{}".format(c))
)
if len(class_to_sum) == 1: # short-circuit mean when only one class
return losses[0]
losses_tensor = tf.stack(losses)
if classes == 'present':
present = tf.stack(present)
losses_tensor = tf.boolean_mask(losses_tensor, present)
loss = tf.reduce_mean(losses_tensor)
return loss
def flatten_probas(probas, labels, ignore=None, order='BHWC'):
"""
Flattens predictions in the batch
"""
if len(probas.shape) == 3:
probas, order = tf.expand_dims(probas, 3), 'BHWC'
if order == 'BCHW':
probas = tf.transpose(probas, (0, 2, 3, 1), name="BCHW_to_BHWC")
order = 'BHWC'
if order != 'BHWC':
raise NotImplementedError('Order {} unknown'.format(order))
C = probas.shape[3]
probas = tf.reshape(probas, (-1, C))
labels = tf.reshape(labels, (-1,))
if ignore is None:
return probas, labels
valid = tf.not_equal(labels, ignore)
vprobas = tf.boolean_mask(probas, valid, name='valid_probas')
vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
return vprobas, vlabels