-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_func.py
353 lines (220 loc) · 10.6 KB
/
train_func.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
"""
Created on Sat Oct 22 19:10:11 2022
@author: patrick
"""
import torch
import numpy as np
import torch.nn.functional as F
import parsing
from utils import *
parser = parsing.create_parser()
args = parser.parse_args()
state = {k: v for k, v in args._get_kwargs()}
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
'''
***The code below is based on "On the Power of Deep but Naive Partial Label Learning (DNPL)", ICASSP 21***
'''
class T_DNPL():
def __init__(self):
super(T_DNPL, self).__init__()
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
optimizer.zero_grad()
output = model(input)
s = part_y
s_hat = F.softmax(output, dim=1)
ss_hat = s * s_hat
ss_hat_dp = ss_hat.sum(1)
ss_hat_dp = torch.clamp(ss_hat_dp, 0., 1.)
loss = -torch.mean(torch.log(ss_hat_dp + 1e-10))
loss.backward()
optimizer.step()
return loss.item(), confidence
'''
***The code below is based on "Progressive Identification of True Labels for Partial-Label Learning (PRODEN)", ICML 20***
'''
class T_PRODEN():
def __init__(self):
super(T_PRODEN, self).__init__()
def confidence_update(self, index, confidence, outputs, part_y):
revisedY = confidence[index, :].clone()
revisedY[revisedY > 0] = 1
revisedY = revisedY * outputs
revisedY = revisedY / revisedY.sum(dim=1).repeat(revisedY.size(1),1).transpose(0,1)
confidence[index, :] = revisedY.detach()
return confidence
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
optimizer.zero_grad()
outputs = F.softmax(model(input), dim=1)
l = confidence[index, :] * torch.log(outputs)
loss = (-torch.sum(l)) / l.size(0)
loss.backward()
optimizer.step()
if args.use_confidence==True:
confidence = self.confidence_update(index, confidence, outputs, part_y)
return loss.item(), confidence
'''
***The code below is based on "Exploiting Class Activation Value for Partial-Label Learning (CAVL)", ICLR 22***
'''
class T_CAVL():
def __init__(self):
super(T_CAVL, self).__init__()
def confidence_update(self, index, confidence, outputs, part_y):
with torch.no_grad():
cav = (outputs*torch.abs(1-outputs))*part_y
cav_pred = torch.max(cav,dim=1)[1]
gt_label = F.one_hot(cav_pred, part_y.shape[1]) # label_smoothing() could be used to further improve the performance for some datasets
confidence[index, :] = gt_label.float()
return confidence
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
optimizer.zero_grad()
outputs = model(input)
logsm_outputs = F.log_softmax(outputs, dim=1)
final_outputs = logsm_outputs * confidence[index, :]
loss = - ((final_outputs).sum(dim=1)).mean()
loss.backward()
optimizer.step()
if args.use_confidence==True:
confidence = self.confidence_update(index, confidence, outputs, part_y)
return loss.item(), confidence
'''
***The code below is based on "Leveraged Weighted Loss for Partial Label Learning (LW)", ICML 21***
'''
class T_LW():
def __init__(self):
super(T_LW, self).__init__()
def confidence_update(self, index, confidence, outputs, part_y):
with torch.no_grad():
sm_outputs = F.softmax(outputs, dim=1)
onezero = torch.zeros(sm_outputs.shape[0], sm_outputs.shape[1])
onezero[part_y > 0] = 1
counter_onezero = 1 - onezero
onezero = onezero.to(device)
counter_onezero = counter_onezero.to(device)
new_weight1 = sm_outputs * onezero
new_weight1 = new_weight1 / (new_weight1 + 1e-8).sum(dim=1).repeat(
confidence.shape[1], 1).transpose(0, 1)
new_weight2 = sm_outputs * counter_onezero
new_weight2 = new_weight2 / (new_weight2 + 1e-8).sum(dim=1).repeat(
confidence.shape[1], 1).transpose(0, 1)
new_weight = new_weight1 + new_weight2
confidence[index, :] = new_weight
return confidence
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
optimizer.zero_grad()
outputs = model(input)
device = outputs.device
onezero = torch.zeros(outputs.shape[0], outputs.shape[1])
onezero[part_y > 0] = 1
counter_onezero = 1 - onezero
onezero = onezero.to(device)
counter_onezero = counter_onezero.to(device)
# loss 1 is applied on candidate labels and loss 2 is applied on non-candidate labels.
if args.loss == 'sigmoid':
sig_loss1 = 0.5 * torch.ones(outputs.shape[0], outputs.shape[1])
sig_loss1 = sig_loss1.to(device)
sig_loss1[outputs < 0] = 1 / (1 + torch.exp(outputs[outputs < 0]))
sig_loss1[outputs > 0] = torch.exp(-outputs[outputs > 0]) / (
1 + torch.exp(-outputs[outputs > 0]))
if args.use_confidence==True:
l1 = confidence[index, :] * onezero * sig_loss1
else:
l1 = onezero * sig_loss1
average_loss1 = torch.sum(l1) / l1.size(0)
sig_loss2 = 0.5 * torch.ones(outputs.shape[0], outputs.shape[1])
sig_loss2 = sig_loss2.to(device)
sig_loss2[outputs > 0] = 1 / (1 + torch.exp(-outputs[outputs > 0]))
sig_loss2[outputs < 0] = torch.exp(
outputs[outputs < 0]) / (1 + torch.exp(outputs[outputs < 0]))
if args.use_confidence==True:
l2 = confidence[index, :] * counter_onezero * sig_loss2
else:
l2 = counter_onezero * sig_loss2
average_loss2 = torch.sum(l2) / l2.size(0)
loss = average_loss1 + args.beta * average_loss2
elif args.loss == 'cross_entropy':
sm_outputs = F.softmax(outputs, dim=1)
sig_loss1 = - torch.log(sm_outputs + 1e-8)
if args.use_confidence==True:
l1 = confidence[index, :] * onezero * sig_loss1
else:
l1 = onezero * sig_loss1
average_loss1 = torch.sum(l1) / l1.size(0)
sig_loss2 = - torch.log(1 - sm_outputs + 1e-8)
if args.use_confidence==True:
l2 = confidence[index, :] * counter_onezero * sig_loss2
else:
l2 = counter_onezero * sig_loss2
average_loss2 = torch.sum(l2) / l2.size(0)
loss = average_loss1 + args.beta * average_loss2
else:
raise Exception('Need to choose the loss')
loss.backward()
optimizer.step()
if args.use_confidence==True:
confidence = self.confidence_update(index, confidence, outputs, part_y)
return loss.item(), confidence
'''
***The code below is based on "Revisiting Consistency Regularization for Deep Partial Label Learning (CR)", ICML 22***
'''
class T_CR():
def __init__(self):
super(T_CR, self).__init__()
def confidence_update(self, index, confidence, y_pred_aug0_probas, y_pred_aug1_probas, y_pred_aug2_probas, part_y):
part_y[part_y>0]=1
y_pred_aug0_probas, y_pred_aug1_probas, y_pred_aug2_probas = map(lambda x: x.detach(), (y_pred_aug0_probas, y_pred_aug1_probas, y_pred_aug2_probas))
revisedY0 = part_y.clone()
revisedY0 = revisedY0 * torch.pow(y_pred_aug0_probas, 1 / (2 + 1)) \
* torch.pow(y_pred_aug1_probas, 1 / (2 + 1)) \
* torch.pow(y_pred_aug2_probas, 1 / (2 + 1))
revisedY0 = revisedY0 / revisedY0.sum(dim=1).repeat(args.num_class, 1).transpose(0, 1)
confidence[index, :] = revisedY0.detach()
return confidence
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
part_y[part_y>0]=1
consistency_criterion = nn.KLDivLoss(reduction='batchmean').to(device)
optimizer.zero_grad()
output, weak_output, strong_output = map(lambda x: model(x), (input, input_w, input_s))
consistency_loss, consistency_loss_weak, consistency_loss_strong =\
map(lambda x: consistency_criterion(torch.log_softmax(x, dim=-1), confidence[index,:]), (output, weak_output, strong_output))
super_loss = -torch.mean(torch.sum(torch.log(1.0000001 - F.softmax(output, dim=1)) * (1 - part_y), dim=1))
if args.use_confidence==True:
lam = min((epoch / args.epochs) * args.lam, args.lam)
else:
lam = 0
loss = super_loss + lam*(args.c_weight * consistency_loss + args.c_weight_w * consistency_loss_weak + args.c_weight_s * consistency_loss_strong)
loss.backward()
optimizer.step()
if args.use_confidence==True:
confidence = self.confidence_update(index, confidence, torch.softmax(output, dim=-1), torch.softmax(weak_output, dim=-1),
torch.softmax(strong_output, dim=-1), part_y)
return loss.item(), confidence
'''
***The code below is based on "PiCO: Contrastive Label Disambiguation for Partial Label Learning (PiCO)", ICLR 22***
'''
class T_PiCO():
def __init__(self):
super(T_PiCO, self).__init__()
def train_step(self, index, confidence, input, input_w, input_s, part_y, model, optimizer, epoch):
loss_fn = partial_loss(confidence)
loss_cont_fn = SupConLoss()
cls_out, features_cont, pseudo_score_cont, partial_target_cont, score_prot = model(input, input, part_y, args, eval_only=False)
pseudo_target_max, pseudo_target_cont = torch.max(pseudo_score_cont, dim=1)
pseudo_target_cont = pseudo_target_cont.contiguous().view(-1, 1)
if args.use_confidence==True:
loss_fn.confidence_update(temp_un_conf=score_prot, batch_index=index, batchY=part_y)
# warm up ended
mask = torch.eq(pseudo_target_cont[:args.batch_size], pseudo_target_cont.T).float().cuda()
# get positive set by contrasting predicted labels
else:
mask = None
# contrastive loss
loss_cont = loss_cont_fn(features=features_cont, mask=mask, batch_size=args.batch_size)
# classification loss
loss_cls = loss_fn(cls_out, index)
loss = loss_cls + args.gamma * loss_cont
optimizer.zero_grad()
loss.backward()
optimizer.step()
#
return loss.item(), confidence
#