-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosses.py
429 lines (364 loc) · 16.4 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import torch
'''
loss functions
'''
def loss_bce(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert not torch.any(observed_labels == -1)
assert P['train_set_variant'] == 'clean'
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = neg_log(1.0 - preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_bce_ls(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert not torch.any(observed_labels == -1)
assert P['train_set_variant'] == 'clean'
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = (1.0 - P['ls_coef']) * neg_log(preds[observed_labels == 1]) + P['ls_coef'] * neg_log(1.0 - preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = (1.0 - P['ls_coef']) * neg_log(1.0 - preds[observed_labels == 0]) + P['ls_coef'] * neg_log(preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_iun(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true']
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[true_labels == 0] = neg_log(1.0 - preds[true_labels == 0]) # FIXME
reg_loss = None
return loss_mtx, reg_loss
def loss_iu(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert torch.any(observed_labels == 1) # must have at least one observed positive
assert torch.any(observed_labels == -1) # must have at least one observed negative
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == -1] = neg_log(1.0 - preds[observed_labels == -1])
reg_loss = None
return loss_mtx, reg_loss
def loss_pr(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
batch_size = int(batch['label_vec_obs'].size(0))
num_classes = int(batch['label_vec_obs'].size(1))
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
for n in range(batch_size):
preds_neg = preds[n, :][observed_labels[n, :] == 0]
for i in range(num_classes):
if observed_labels[n, i] == 1:
torch.nonzero(observed_labels[n, :])
loss_mtx[n, i] = torch.sum(torch.clamp(1.0 - preds[n, i] + preds_neg, min=0))
reg_loss = None
return loss_mtx, reg_loss
def loss_an(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true'].to(Z['device'])
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = neg_log(1.0 - preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_an_ls(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = (1.0 - P['ls_coef']) * neg_log(preds[observed_labels == 1]) + P['ls_coef'] * neg_log(1.0 - preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = (1.0 - P['ls_coef']) * neg_log(1.0 - preds[observed_labels == 0]) + P['ls_coef'] * neg_log(preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_wan(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = neg_log(1.0 - preds[observed_labels == 0]) / float(P['num_classes'] - 1)
reg_loss = None
return loss_mtx, reg_loss
def loss_epr(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss w.r.t. observed positives:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
# compute regularizer:
reg_loss = expected_positive_regularizer(preds, P['expected_num_pos'], norm='2') / (P['num_classes'] ** 2)
return loss_mtx, reg_loss
def loss_role(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
estimated_labels = batch['label_vec_est']
# input validation:
assert torch.min(observed_labels) >= 0
# (image classifier) compute loss w.r.t. observed positives:
loss_mtx_pos_1 = torch.zeros_like(observed_labels)
loss_mtx_pos_1[observed_labels == 1] = neg_log(preds[observed_labels == 1])
# (image classifier) compute loss w.r.t. label estimator outputs:
estimated_labels_detached = estimated_labels.detach()
loss_mtx_cross_1 = estimated_labels_detached * neg_log(preds) + (1.0 - estimated_labels_detached) * neg_log(1.0 - preds)
# (image classifier) compute regularizer:
reg_1 = expected_positive_regularizer(preds, P['expected_num_pos'], norm='2') / (P['num_classes'] ** 2)
# (label estimator) compute loss w.r.t. observed positives:
loss_mtx_pos_2 = torch.zeros_like(observed_labels)
loss_mtx_pos_2[observed_labels == 1] = neg_log(estimated_labels[observed_labels == 1])
# (label estimator) compute loss w.r.t. image classifier outputs:
preds_detached = preds.detach()
loss_mtx_cross_2 = preds_detached * neg_log(estimated_labels) + (1.0 - preds_detached) * neg_log(1.0 - estimated_labels)
# (label estimator) compute regularizer:
reg_2 = expected_positive_regularizer(estimated_labels, P['expected_num_pos'], norm='2') / (P['num_classes'] ** 2)
# compute final loss matrix:
reg_loss = 0.5 * (reg_1 + reg_2)
loss_mtx = 0.5 * (loss_mtx_pos_1 + loss_mtx_pos_2)
loss_mtx += 0.5 * (loss_mtx_cross_1 + loss_mtx_cross_2)
return loss_mtx, reg_loss
def loss_EM(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true'].to(Z['device'])
# input validation:
assert torch.min(observed_labels) >= 0
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
return loss_mtx, None
def loss_EM_PL(batch, P, Z):
# unpack:
preds = batch['preds']
#print("preds: ", preds)
observed_labels = batch['label_vec_obs']
#print("observed_labels: ", observed_labels)
true_labels = batch['label_vec_true']
#print("true_labels: ", true_labels)
gamma_neg = 4
gamma_pos = 2
clip = 0.05
if P['epoch'] > P['warmup_epoch']:
pseudo_labels = batch['logits_pl']
#print("pseudo_labels: ", pseudo_labels)
similarity = batch['similarity']
#print("similarity: ", similarity)
final_labels = torch.where(observed_labels == 0, pseudo_labels, observed_labels)
#print("final_labels: ", final_labels)
#input validation:
assert torch.min(final_labels) >= -1
loss_mtx = torch.zeros_like(preds)
#####
#observed positive label
#####
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1]) #+ neg_log(similarity[observed_labels == 1])
#loss_mtx[final_labels == 1] = neg_log(preds[final_labels == 1]) #+ neg_log(similarity[final_labels == 1])
#####
#Unknown Labels
#####
loss_mtx[final_labels == 0] = -P['alpha'] * (preds[final_labels == 0] * neg_log(preds[final_labels == 0]) + (1 - preds[final_labels == 0]) * neg_log(1 - preds[final_labels == 0]))#-P['alpha'] *(similarity[final_labels == 0] * neg_log(similarity[final_labels == 0]) + (1 - similarity[final_labels == 0]) * neg_log(1 - similarity[final_labels == 0]))
#####
#Pseudo-Label
#####
#positive pseudo-label
mask_pos = (observed_labels == 0) & (pseudo_labels == 1)
#print(mask)
loss_mtx[mask_pos] = P['beta_pos'] * (0.9* neg_log(preds[mask_pos]) + 0.1* neg_log(1-preds[mask_pos]))
#loss_mtx[pseudo_labels==1] = P['beta_pos'] * neg_log(preds[pseudo_labels==1]) #+ P['beta_pos'] * neg_log(similarity[pseudo_labels==1]))
'''
#negative pseudo-label
#may also need to introduce the similarity_score to make sure train the model has the ability to discover the labels which 100% sure negative.
#negative pseudo-label
mask_neg = (observed_labels == 0) & (pseudo_labels ==-1)
loss_mtx[mask_neg] = P['beta_neg'] *(0.1 * neg_log(preds[mask_neg]) + 0.9 * neg_log(1 - preds[mask_neg]))
#loss_mtx[mask_neg] = P['beta_neg'] * neg_log(1 - preds[mask_neg])# - P['beta_neg'] * neg_log(similarity[mask_neg]))
'''
else:
#Using EM loss to warmup the whole model
#print("The warmup starting...")
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
return loss_mtx, None
def loss_EM_PL_ASL(batch, P, Z):
# unpack:
preds = batch['preds']
preds_neg = 1 - preds
#print("preds: ", preds)
observed_labels = batch['label_vec_obs']
#print("observed_labels: ", observed_labels)
true_labels = batch['label_vec_true']
#print("true_labels: ", true_labels)
gamma_neg = P['negative']
gamma_pos = P['positive']
gamma_unknown = P['unknown']
clip = 0.05
if clip is not None and clip > 0:
preds_neg = (preds_neg + clip).clamp(max=1)
if P['epoch'] > P['warmup_epoch']:
pseudo_labels = batch['logits_pl']
#print("pseudo_labels: ", pseudo_labels)
similarity = batch['similarity']
#print("similarity: ", similarity)
final_labels = torch.where(observed_labels == 0, pseudo_labels, observed_labels)
#print("final_labels: ", final_labels)
#input validation:
assert torch.min(final_labels) >= -1
loss_mtx = torch.zeros_like(preds)
pt = torch.zeros_like(preds)
#####
#observed positive label
#####
pt[observed_labels == 1] = preds[observed_labels == 1]
loss_mtx[observed_labels == 1] = torch.pow(1 - pt[observed_labels == 1], gamma_pos) * neg_log(preds[observed_labels == 1]) #+ neg_log(similarity[observed_labels == 1])
#loss_mtx[final_labels == 1] = neg_log(preds[final_labels == 1]) #+ neg_log(similarity[final_labels == 1])
#####
#Unknown Labels
#####
pt[final_labels == 0] = preds[final_labels == 0]
loss_mtx[final_labels == 0] = - torch.pow(1 - pt[final_labels == 0], gamma_unknown) * (preds[final_labels == 0] * neg_log(preds[final_labels == 0]) + (1 - preds[final_labels == 0]) * neg_log(1 - preds[final_labels == 0])) #-P['alpha'] *(similarity[final_labels == 0] * neg_log(similarity[final_labels == 0]) + (1 - similarity[final_labels == 0]) * neg_log(1 - similarity[final_labels == 0]))
#####
#Pseudo-Label
#####
#positive pseudo-label
mask_pos = (observed_labels == 0) & (pseudo_labels == 1)
#print(mask)
pt[mask_pos] = preds[mask_pos]
loss_mtx[mask_pos] = torch.pow(1 - pt[mask_pos], gamma_pos) * neg_log(preds[mask_pos])
#loss_mtx[pseudo_labels==1] = P['beta_pos'] * neg_log(preds[pseudo_labels==1]) #+ P['beta_pos'] * neg_log(similarity[pseudo_labels==1]))
#negative pseudo-label
#may also need to introduce the similarity_score to make sure train the model has the ability to discover the labels which 100% sure negative.
#negative pseudo-label
mask_neg = (observed_labels == 0) & (pseudo_labels ==-1)
pt[mask_neg] = 1 - preds[mask_neg]
loss_mtx[mask_neg] = torch.pow(1 - pt[mask_neg], gamma_neg) *(neg_log(1 - preds[mask_neg]))
#loss_mtx[mask_neg] = P['beta_neg'] * neg_log(1 - preds[mask_neg])# - P['beta_neg'] * neg_log(similarity[mask_neg]))
else:
#Using EM loss to warmup the whole model
#print("The warmup starting...")
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
return loss_mtx, None
def loss_EM_APL(batch, P, Z):
# unpack:
preds = batch['preds']
#print("preds: ", preds)
observed_labels = batch['label_vec_obs']
#print("observed_labels: ", observed_labels)
# input validation:
assert torch.min(observed_labels) >= -1
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
soft_label = -observed_labels[observed_labels < 0]
loss_mtx[observed_labels < 0] = P['beta'] * (
soft_label * neg_log(preds[observed_labels < 0]) +
(1 - soft_label) * neg_log(1 - preds[observed_labels < 0])
)
return loss_mtx, None
loss_functions = {
'bce': loss_bce,
'bce_ls': loss_bce_ls,
'iun': loss_iun,
'iu': loss_iu,
'pr': loss_pr,
'an': loss_an,
'an_ls': loss_an_ls,
'wan': loss_wan,
'epr': loss_epr,
'role': loss_role,
'EM': loss_EM,
'EM_APL': loss_EM_APL,
'EM_PL': loss_EM_PL,
'EM_PL_ASL': loss_EM_PL_ASL
}
'''
top-level wrapper
'''
def compute_batch_loss(batch, P, Z):
assert batch['preds'].dim() == 2
batch_size = int(batch['preds'].size(0))
num_classes = int(batch['preds'].size(1))
loss_denom_mtx = (num_classes * batch_size) * torch.ones_like(batch['preds'])
# input validation:
assert torch.max(batch['label_vec_obs']) <= 1
assert torch.min(batch['label_vec_obs']) >= -1
assert batch['preds'].size() == batch['label_vec_obs'].size()
assert P['loss'] in loss_functions
# validate predictions:
assert torch.max(batch['preds']) <= 1
assert torch.min(batch['preds']) >= 0
# compute loss for each image and class:
loss_mtx, reg_loss = loss_functions[P['loss']](batch, P, Z)
main_loss = (loss_mtx / loss_denom_mtx).sum()
if reg_loss is not None:
batch['loss_tensor'] = main_loss + reg_loss
batch['reg_loss_np'] = reg_loss.clone().detach().cpu().numpy()
else:
batch['loss_tensor'] = main_loss
batch['reg_loss_np'] = 0.0
batch['loss_np'] = batch['loss_tensor'].clone().detach().cpu().numpy()
return batch
'''
helper functions
'''
LOG_EPSILON = 1e-5
def neg_log(x):
return - torch.log(x + LOG_EPSILON)
def log_loss(preds, targs):
return targs * neg_log(preds)
def expected_positive_regularizer(preds, expected_num_pos, norm='2'):
# Assumes predictions in [0,1].
if norm == '1':
reg = torch.abs(preds.sum(1).mean(0) - expected_num_pos)
elif norm == '2':
reg = (preds.sum(1).mean(0) - expected_num_pos)**2
else:
raise NotImplementedError
return reg