-
Notifications
You must be signed in to change notification settings - Fork 2
/
blob_loss.py
386 lines (326 loc) · 12.6 KB
/
blob_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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import torch
def vprint(*args):
verbose = False
if verbose:
print(*args)
def compute_compound_loss(
criterion_dict: dict,
raw_network_outputs: torch.Tensor,
label: torch.Tensor,
blob_loss_mode=False,
masked=True,
):
"""
This computes a compound loss by looping through a criterion dict!
"""
# vprint("outputs:", outputs)
losses = []
for entry in criterion_dict.values():
name = entry["name"]
vprint("loss name:", name)
criterion = entry["loss"]
weight = entry["weight"]
sigmoid = entry["sigmoid"]
if blob_loss_mode == False:
vprint("computing main loss!")
if sigmoid == True:
sigmoid_network_outputs = torch.sigmoid(raw_network_outputs)
individual_loss = criterion(sigmoid_network_outputs, label)
else:
individual_loss = criterion(raw_network_outputs, label)
elif blob_loss_mode == True:
vprint("computing blob loss!")
if masked == True: # this is the default blob loss
if sigmoid == True:
sigmoid_network_outputs = torch.sigmoid(raw_network_outputs)
individual_loss = compute_blob_loss_multi(
criterion=criterion,
network_outputs=sigmoid_network_outputs,
multi_label=label,
)
else:
individual_loss = compute_blob_loss_multi(
criterion=criterion,
network_outputs=raw_network_outputs,
multi_label=label,
)
elif masked == False: # without masking for ablation study
if sigmoid == True:
sigmoid_network_outputs = torch.sigmoid(raw_network_outputs)
individual_loss = compute_no_masking_multi(
criterion=criterion,
network_outputs=sigmoid_network_outputs,
multi_label=label,
)
else:
individual_loss = compute_no_masking_multi(
criterion=criterion,
network_outputs=raw_network_outputs,
multi_label=label,
)
weighted_loss = individual_loss * weight
losses.append(weighted_loss)
vprint("losses:", losses)
loss = sum(losses)
return loss
def compute_blob_loss_multi(
criterion,
network_outputs: torch.Tensor,
multi_label: torch.Tensor,
):
"""
1. loop through elements in our batch
2. loop through blobs per element compute loss and divide by blobs to have element loss
2.1 we need to account for sigmoid and non/sigmoid in conjunction with BCE
3. divide by batch length to have a correct batch loss for back prop
"""
batch_length = multi_label.shape[0]
vprint("batch_length:", batch_length)
element_blob_loss = []
# loop over elements
for element in range(batch_length):
if element < batch_length:
end_index = element + 1
elif element == batch_length:
end_index = None
element_label = multi_label[element:end_index, ...]
vprint("element label shape:", element_label.shape)
vprint("element_label:", element_label.shape)
element_output = network_outputs[element:end_index, ...]
# loop through labels
unique_labels = torch.unique(element_label)
blob_count = len(unique_labels) - 1
vprint("found this amount of blobs in batch element:", blob_count)
label_loss = []
for ula in unique_labels:
if ula == 0:
vprint("ula is 0 we do nothing")
else:
# first we need one hot labels
vprint("ula greater than 0:", ula)
label_mask = element_label > 0
# we flip labels
label_mask = ~label_mask
# we set the mask to true where our label of interest is located
# vprint(torch.count_nonzero(label_mask))
label_mask[element_label == ula] = 1
# vprint(torch.count_nonzero(label_mask))
vprint("label_mask", label_mask)
# vprint("torch.unique(label_mask):", torch.unique(label_mask))
the_label = element_label == ula
the_label_int = the_label.int()
vprint("the_label:", torch.count_nonzero(the_label))
# debugging
# masked_label = the_label * label_mask
# vprint("masked_label:", torch.count_nonzero(masked_label))
masked_output = element_output * label_mask
try:
# we try with int labels first, but some losses require floats
blob_loss = criterion(masked_output, the_label_int)
except:
# if int does not work we try float
blob_loss = criterion(masked_output, the_label.float())
vprint("blob_loss:", blob_loss)
label_loss.append(blob_loss)
# compute mean
vprint("label_loss:", label_loss)
# mean_label_loss = 0
vprint("blobs in crop:", len(label_loss))
if not len(label_loss) == 0:
mean_label_loss = sum(label_loss) / len(label_loss)
# mean_label_loss = sum(label_loss) / \
# torch.count_nonzero(label_loss)
vprint("mean_label_loss", mean_label_loss)
element_blob_loss.append(mean_label_loss)
# compute mean
vprint("element_blob_loss:", element_blob_loss)
mean_element_blob_loss = 0
vprint("elements in batch:", len(element_blob_loss))
if not len(element_blob_loss) == 0:
mean_element_blob_loss = sum(element_blob_loss) / len(element_blob_loss)
# element_blob_loss) / torch.count_nonzero(element_blob_loss)
vprint("mean_element_blob_loss", mean_element_blob_loss)
return mean_element_blob_loss
def compute_no_masking_multi(
criterion,
network_outputs: torch.Tensor,
multi_label: torch.Tensor,
):
"""
1. loop through elements in our batch
2. loop through blobs per element compute loss and divide by blobs to have element loss
2.1 we need to account for sigmoid and non/sigmoid in conjunction with BCE
3. divide by batch length to have a correct batch loss for back prop
"""
batch_length = multi_label.shape[0]
vprint("batch_length:", batch_length)
element_blob_loss = []
# loop over elements
for element in range(batch_length):
if element < batch_length:
end_index = element + 1
elif element == batch_length:
end_index = None
element_label = multi_label[element:end_index, ...]
vprint("element label shape:", element_label.shape)
vprint("element_label:", element_label.shape)
element_output = network_outputs[element:end_index, ...]
# loop through labels
unique_labels = torch.unique(element_label)
blob_count = len(unique_labels) - 1
vprint("found this amount of blobs in batch element:", blob_count)
label_loss = []
for ula in unique_labels:
if ula == 0:
vprint("ula is 0 we do nothing")
else:
# first we need one hot labels
vprint("ula greater than 0:", ula)
the_label = element_label == ula
the_label_int = the_label.int()
vprint("the_label:", torch.count_nonzero(the_label))
# we compute the loss with no mask
try:
# we try with int labels first, but some losses require floats
blob_loss = criterion(element_output, the_label_int)
except:
# if int does not work we try float
blob_loss = criterion(element_output, the_label.float())
vprint("blob_loss:", blob_loss)
label_loss.append(blob_loss)
# compute mean
vprint("label_loss:", label_loss)
# mean_label_loss = 0
vprint("blobs in crop:", len(label_loss))
if not len(label_loss) == 0:
mean_label_loss = sum(label_loss) / len(label_loss)
# mean_label_loss = sum(label_loss) / \
# torch.count_nonzero(label_loss)
vprint("mean_label_loss", mean_label_loss)
element_blob_loss.append(mean_label_loss)
# compute mean
vprint("element_blob_loss:", element_blob_loss)
mean_element_blob_loss = 0
vprint("elements in batch:", len(element_blob_loss))
if not len(element_blob_loss) == 0:
mean_element_blob_loss = sum(element_blob_loss) / len(element_blob_loss)
# element_blob_loss) / torch.count_nonzero(element_blob_loss)
vprint("mean_element_blob_loss", mean_element_blob_loss)
return mean_element_blob_loss
def compute_loss(
blob_loss_dict: dict,
criterion_dict: dict,
blob_criterion_dict: dict,
raw_network_outputs: torch.Tensor,
binary_label: torch.Tensor,
multi_label: torch.Tensor,
):
"""
This function computes the total loss. It has a global main loss and the blob loss term which is computed separately for each connected component. The binary_label is the binarized label for the global part. The multi label features separate integer labels for each connected component.
Example inputs should look like:
blob_loss_dict = {
"main_weight": 1,
"blob_weight": 0,
}
criterion_dict = {
"bce": {
"name": "bce",
"loss": BCEWithLogitsLoss(reduction="mean"),
"weight": 1.0,
"sigmoid": False,
},
"dice": {
"name": "dice",
"loss": DiceLoss(
include_background=True,
to_onehot_y=False,
sigmoid=True,
softmax=False,
squared_pred=False,
),
"weight": 1.0,
"sigmoid": False,
},
}
blob_criterion_dict = {
"bce": {
"name": "bce",
"loss": BCEWithLogitsLoss(reduction="mean"),
"weight": 1.0,
"sigmoid": False,
},
"dice": {
"name": "dice",
"loss": DiceLoss(
include_background=True,
to_onehot_y=False,
sigmoid=True,
softmax=False,
squared_pred=False,
),
"weight": 1.0,
"sigmoid": False,
},
}
"""
main_weight = blob_loss_dict["main_weight"]
blob_weight = blob_loss_dict["blob_weight"]
# main loss
if main_weight > 0:
vprint("main_weight greater than zero:", main_weight)
# vprint("main_label:", main_label)
main_loss = compute_compound_loss(
criterion_dict=criterion_dict,
raw_network_outputs=raw_network_outputs,
label=binary_label,
blob_loss_mode=False,
)
if blob_weight > 0:
vprint("blob_weight greater than zero:", blob_weight)
blob_loss = compute_compound_loss(
criterion_dict=blob_criterion_dict,
raw_network_outputs=raw_network_outputs,
label=multi_label,
blob_loss_mode=True,
)
# final loss
if blob_weight == 0 and main_weight > 0:
vprint(
"main_weight:",
main_weight,
"// blob_weight:",
blob_weight,
"// computing main loss only",
)
loss = main_loss
blob_loss = 0
elif main_weight == 0 and blob_weight > 0:
vprint(
"main_weight:",
main_weight,
"// blob_weight:",
blob_weight,
"// computing blob loss only",
)
loss = blob_loss
main_loss = 0 # we set this to 0
elif main_weight > 0 and blob_weight > 0:
vprint(
"main_weight:",
main_weight,
"// blob_weight:",
blob_weight,
"// computing blob loss",
)
loss = main_loss * main_weight + blob_loss * blob_weight
else:
vprint("defaulting to equal weighted blob loss")
loss = main_loss + blob_loss
vprint("blob loss:", blob_loss)
vprint("main loss:", main_loss)
vprint("effective loss:", loss)
return loss, main_loss, blob_loss
def get_loss_value(loss):
if loss == 0:
return 0
return loss.item()