-
Notifications
You must be signed in to change notification settings - Fork 11
/
train_2s2t.py
825 lines (692 loc) · 34.5 KB
/
train_2s2t.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
from mean_teacher import losses, ramps
from utils.util import FocalLoss, PULoss
from models import MultiLayerPerceptron as Model
from models import CNN
from datasets import MNIST_Dataset_FixSample, get_mnist, binarize_mnist_class
from cifar_datasets import CIFAR_Dataset, get_cifar, binarize_cifar_class
from functions import *
from torchvision import transforms
import os
import time
import random
import argparse
import numpy as np
import shutil
from tqdm import tqdm
def boolean_string(s):
if s not in {'False', 'True'}:
raise ValueError('Not a valid boolean string')
return s == 'True'
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=None)
parser.add_argument('--batch-size', '-b', type=int, default=256, help='batch-size')
parser.add_argument('--lr', type=float, default=5e-4, help='Learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=5e-3, type=float,
metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--modeldir', type=str, default="model/", help="Model path")
parser.add_argument('--epochs', type=int, default=200)
parser.add_argument('--loss', type=str, default='nnPU')
parser.add_argument('--gpu', default=None, type=int, help='GPU id to use.')
parser.add_argument('-j', '--workers', default=4, type=int, help='workers')
parser.add_argument('--weight', type=float, default=1.0)
parser.add_argument('--self-paced', type=boolean_string, default=True)
parser.add_argument('--self-paced-start', type=int, default=10)
parser.add_argument('--self-paced-stop', type=int, default=50)
parser.add_argument('--self-paced-frequency', type=int, default=10)
parser.add_argument('--self-paced-type', type=str, default = "A")
parser.add_argument('--increasing', type=boolean_string, default=True)
parser.add_argument('--replacement', type=boolean_string, default=True)
parser.add_argument('--mean-teacher', type=boolean_string, default=True)
parser.add_argument('--ema-start', type=int, default=50)
parser.add_argument('--ema-decay', type=float, default=0.999)
parser.add_argument('--consistency', type=float, default=0.3)
parser.add_argument('--consistency-rampup', type=int, default=400)
parser.add_argument('--evaluation', action="store_true")
parser.add_argument('--top1', type=float, default=0.4)
parser.add_argument('--top2', type=float, default=0.6)
parser.add_argument('--soft-label', action="store_true")
parser.add_argument('--dataset', type=str, default="mnist")
parser.add_argument('--datapath', type=str, default="")
parser.add_argument('--type', type=str, default="mu")
parser.add_argument('--alpha', type=float, default=0.1)
step = 0
results = np.zeros(61000)
switched = False
results1 = None
results2 = None
args = None
def main():
global args, switched
args = parser.parse_args()
print(args)
criterion = get_criterion()
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
if args.dataset == "mnist":
(trainX, trainY), (testX, testY) = get_mnist()
_trainY, _testY = binarize_mnist_class(trainY, testY)
dataset_train1_clean = MNIST_Dataset_FixSample(1000, 60000,
trainX, _trainY, testX, _testY, split='train', ids=[],
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top1, type="clean",
seed = args.seed)
# clean dataset初始化为空
dataset_train1_noisy = MNIST_Dataset_FixSample(1000, 60000,
trainX, _trainY, testX, _testY, split='train',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top1, type="noisy",
seed = args.seed)
dataset_train1_noisy.copy(dataset_train1_clean) # 和clean dataset使用相同的随机顺序
dataset_train1_noisy.reset_ids() # 让初始化的noisy dataset使用全部数据
dataset_test = MNIST_Dataset_FixSample(1000, 60000,
trainX, _trainY, testX, _testY, split='test',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, type="clean")
dataset_train2_noisy = MNIST_Dataset_FixSample(1000, 60000,
trainX, _trainY, testX, _testY, split='train',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top2, type="noisy",
seed = args.seed)
dataset_train2_clean = MNIST_Dataset_FixSample(1000, 60000,
trainX, _trainY, testX, _testY, split='train', ids=[],
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top2, type="clean",
seed = args.seed)
dataset_train2_noisy.copy(dataset_train1_noisy)
dataset_train2_noisy.reset_ids()
dataset_train2_clean.copy(dataset_train1_clean)
#dataset_train2_clean.set_ids([])
assert np.all(dataset_train1_clean.X == dataset_train1_noisy.X)
assert np.all(dataset_train2_clean.X == dataset_train1_noisy.X)
assert np.all(dataset_train2_noisy.X == dataset_train1_noisy.X)
elif args.dataset == 'cifar':
data_transforms = {
'train': transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]),
'val': transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
}
(trainX, trainY), (testX, testY) = get_cifar()
_trainY, _testY = binarize_cifar_class(trainY, testY)
dataset_train1_clean = CIFAR_Dataset(1000, 50000,
trainX, _trainY, testX, _testY, split='train', ids=[],
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top1, transform = data_transforms['train'], type="clean",
seed = args.seed)
# clean dataset初始化为空
dataset_train1_noisy = CIFAR_Dataset(1000, 50000,
trainX, _trainY, testX, _testY, split='train',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, top = args.top1, transform = data_transforms['train'], type="noisy",
seed = args.seed)
dataset_train1_noisy.copy(dataset_train1_clean) # 和clean dataset使用相同的随机顺序
dataset_train1_noisy.reset_ids() # 让初始化的noisy dataset使用全部数据
dataset_test = CIFAR_Dataset(1000, 50000,
trainX, _trainY, testX, _testY, split='test',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type, transform = data_transforms['val'], type="clean",
seed = args.seed)
dataset_train2_noisy = CIFAR_Dataset(1000, 50000,
trainX, _trainY, testX, _testY, split='train',
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type,
transform = data_transforms['train'], top = args.top2, type="noisy",
seed = args.seed)
dataset_train2_clean = CIFAR_Dataset(1000, 50000,
trainX, _trainY, testX, _testY, split='train', ids=[],
increasing=args.increasing, replacement=args.replacement, mode=args.self_paced_type,
transform = data_transforms['train'], top = args.top2, type="clean",)
dataset_train2_noisy.copy(dataset_train1_noisy)
dataset_train2_noisy.reset_ids()
dataset_train2_clean.copy(dataset_train1_clean)
#dataset_train2_clean.set_ids([])
assert np.all(dataset_train1_clean.X == dataset_train1_noisy.X)
assert np.all(dataset_train2_clean.X == dataset_train1_noisy.X)
assert np.all(dataset_train2_noisy.X == dataset_train1_noisy.X)
assert np.all(dataset_train1_clean.Y == dataset_train1_noisy.Y)
assert np.all(dataset_train2_clean.Y == dataset_train1_noisy.Y)
assert np.all(dataset_train2_noisy.Y == dataset_train1_noisy.Y)
assert np.all(dataset_train1_clean.T == dataset_train1_noisy.T)
assert np.all(dataset_train2_clean.T == dataset_train1_noisy.T)
assert np.all(dataset_train2_noisy.T == dataset_train1_noisy.T)
criterion.update_p(0.4)
dataloader_train1_clean = None
dataloader_train1_noisy = DataLoader(dataset_train1_noisy, batch_size=args.batch_size, num_workers=args.workers, shuffle=False, pin_memory=True)
dataloader_train2_clean = None
dataloader_train2_noisy = DataLoader(dataset_train2_noisy, batch_size=args.batch_size, num_workers=args.workers, shuffle=False, pin_memory=True)
dataloader_test = DataLoader(dataset_test, batch_size=args.batch_size, num_workers=args.workers, shuffle=False, pin_memory=True)
consistency_criterion = losses.softmax_mse_loss
if args.dataset == 'mnist':
model1 = create_model()
model2 = create_model()
ema_model1 = create_model(ema = True)
ema_model2 = create_model(ema = True)
elif args.dataset == 'cifar':
model1 = create_cifar_model()
model2 = create_cifar_model()
ema_model1 = create_cifar_model(ema = True)
ema_model2 = create_cifar_model(ema = True)
if args.gpu is not None:
model1 = model1.cuda(args.gpu)
model2 = model2.cuda(args.gpu)
ema_model1 = ema_model1.cuda(args.gpu)
ema_model2 = ema_model2.cuda(args.gpu)
else:
model1 = model1.cuda(args.gpu)
model2 = model2.cuda(args.gpu)
ema_model1 = ema_model1.cuda(args.gpu)
ema_model2 = ema_model2.cuda(args.gpu)
optimizer1 = torch.optim.Adam(model1.parameters(), lr=args.lr,
weight_decay=args.weight_decay
)
optimizer2 = torch.optim.Adam(model2.parameters(), lr=args.lr,
weight_decay=args.weight_decay
)
stats_ = stats(args.modeldir, 0)
scheduler1 = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer1, args.epochs)
scheduler2 = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer2, args.epochs)
if args.evaluation:
print("Evaluation mode!")
best_acc1 = 0
best_acc2 = 0
best_acc3 = 0
best_acc4 = 0
best_acc = 0
for epoch in range(args.epochs):
print("Self paced status: {}".format(check_self_paced(epoch)))
print("Mean Teacher status: {}".format(check_mean_teacher(epoch)))
if check_mean_teacher(epoch) and not check_mean_teacher(epoch - 1) and not switched:
ema_model1.load_state_dict(model1.state_dict())
ema_model2.load_state_dict(model2.state_dict())
switched = True
print("SWITCHED!")
trainPacc, trainNacc, trainPNacc = train(dataloader_train1_clean, dataloader_train1_noisy, dataloader_train2_clean, dataloader_train2_noisy, model1, model2, ema_model1, ema_model2, criterion, consistency_criterion, optimizer1, scheduler1, optimizer2, scheduler2, epoch)
valPacc, valNacc, valPNacc1, valPNacc2, valPNacc3, valPNacc4 = validate(dataloader_test, model1, model2, ema_model1, ema_model2, epoch)
#print(valPacc, valNacc, valPNacc1, valPNacc2, valPNacc3)
stats_._update(trainPacc, trainNacc, trainPNacc, valPacc, valNacc, valPNacc1)
best_acc1 = max(valPNacc1, best_acc1)
best_acc2 = max(valPNacc2, best_acc2)
best_acc3 = max(valPNacc3, best_acc3)
best_acc4 = max(valPNacc4, best_acc4)
all_accuracy = [valPNacc1, valPNacc2, valPNacc3, valPNacc4]
models = [model1, model2, ema_model1, ema_model2]
if (check_self_paced(epoch)) and (epoch - args.self_paced_start) % args.self_paced_frequency == 0:
dataloader_train1_clean, dataloader_train1_noisy, dataloader_train2_clean, dataloader_train2_noisy = update_dataset(model1, model2, ema_model1, ema_model2, dataset_train1_clean, dataset_train1_noisy, dataset_train2_clean, dataset_train2_noisy, epoch)
plot_curve(stats_, args.modeldir, 'model', True)
if (max(all_accuracy) > best_acc):
torch.save({
'epoch': epoch + 1,
'state_dict': models[all_accuracy.index(max(all_accuracy))].state_dict(),
'best_prec1': best_acc1,
}, 'model_best.pth.tar')
best_acc = max(all_accuracy)
dataset_train1_noisy.shuffle()
dataset_train2_noisy.shuffle()
dataloader_train1_noisy = DataLoader(dataset_train1_noisy, batch_size=args.batch_size, num_workers=args.workers, shuffle=False, pin_memory=True)
dataloader_train2_noisy = DataLoader(dataset_train2_noisy, batch_size=args.batch_size, num_workers=args.workers, shuffle=False, pin_memory=True)
print(best_acc1)
print(best_acc2)
print(best_acc3)
print(best_acc4)
def train(clean1_loader, noisy1_loader, clean2_loader, noisy2_loader, model1, model2, ema_model1, ema_model2, criterion, consistency_criterion, optimizer1, scheduler1, optimizer2, scheduler2, epoch):
global step, switched
batch_time = AverageMeter()
data_time = AverageMeter()
#losses = AverageMeter()
pacc1 = AverageMeter()
nacc1 = AverageMeter()
pnacc1 = AverageMeter()
pacc2 = AverageMeter()
nacc2 = AverageMeter()
pnacc2 = AverageMeter()
pacc3 = AverageMeter()
nacc3 = AverageMeter()
pnacc3 = AverageMeter()
pacc4 = AverageMeter()
nacc4 = AverageMeter()
pnacc4 = AverageMeter()
count_clean = AverageMeter()
count_noisy = AverageMeter()
model1.train()
model2.train()
ema_model1.train()
ema_model2.train()
end = time.time()
entropy_clean = AverageMeter()
entropy_noisy = AverageMeter()
count2 = AverageMeter()
count1 = AverageMeter()
consistency_weight = get_current_consistency_weight(epoch - 30)
scheduler1.step()
scheduler2.step()
resultt = np.zeros(61000)
if clean1_loader:
for i, (X, _, Y, T, ids, _) in enumerate(clean1_loader):
# measure data loading time
data_time.update(time.time() - end)
X = X.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.view(X.shape[0], 1, -1)
Y = Y.cuda(args.gpu).float()
T = T.cuda(args.gpu).long()
# compute output
output1 = model1(X)
output2 = model2(X)
with torch.no_grad():
ema_output1 = ema_model1(X)
consistency_loss = consistency_weight * \
consistency_criterion(output1, ema_output1) / X.shape[0]
predictiont1 = torch.sign(ema_output1).long()
predictions1 = torch.sign(output1).long() # 否则使用自己的结果
smx1 = torch.sigmoid(output1) # 计算sigmoid概率
smx1 = torch.cat([1 - smx1, smx1], dim=1) # 组合成预测变量
smxY = ((Y + 1) // 2).long() # 分类结果,0-1分类
smx2 = torch.sigmoid(output2) # 计算sigmoid概率
smx2 = torch.cat([1 - smx2, smx2], dim=1) # 组合成预测变量
if args.soft_label:
aux1 = -torch.sum(smx1 * torch.log(smx1 + 1e-10)) / smx1.shape[0]
aux2 = -torch.sum(smx2 * torch.log(smx2 + 1e-10)) / smx2.shape[0]
else:
smxY = smxY.float()
smxY = smxY.view(-1, 1)
smxY = torch.cat([1 - smxY, smxY], dim = 1)
aux1 = - torch.sum(smxY * torch.log(smx1 + 1e-10)) / smxY.shape[0]
aux2 = - torch.sum(smxY * torch.log(smx2 + 1e-10)) / smxY.shape[0]
loss = aux1
entropy_clean.update(aux1, 1)
if check_mean_teacher(epoch):
loss += consistency_loss
optimizer1.zero_grad()
loss.backward()
optimizer1.step()
pacc_1, nacc_1, pnacc_1, psize = accuracy(predictions1, T) # 使用T来计算预测准确率
pacc_3, nacc_3, pnacc_3, psize = accuracy(predictiont1, T)
pacc1.update(pacc_1, psize)
nacc1.update(nacc_1, X.size(0) - psize)
pnacc1.update(pnacc_1, X.size(0))
pacc3.update(pacc_3, psize)
nacc3.update(nacc_3, X.size(0) - psize)
pnacc3.update(pnacc_3, X.size(0))
if clean2_loader:
for i, (X, _, Y, T, ids, _) in enumerate(clean2_loader):
# measure data loading time
data_time.update(time.time() - end)
X = X.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.view(X.shape[0], 1, -1)
Y = Y.cuda(args.gpu).float()
T = T.cuda(args.gpu).long()
# compute output
output1 = model1(X)
output2 = model2(X)
with torch.no_grad():
ema_output2 = ema_model2(X)
consistency_loss = consistency_weight * \
consistency_criterion(output2, ema_output2) / X.shape[0]
predictiont2 = torch.sign(ema_output2).long()
predictions2 = torch.sign(output2).long()
smx1 = torch.sigmoid(output1) # 计算sigmoid概率
smx1 = torch.cat([1 - smx1, smx1], dim=1) # 组合成预测变量
smxY = ((Y + 1) // 2).long() # 分类结果,0-1分类
smx2 = torch.sigmoid(output2) # 计算sigmoid概率
smx2 = torch.cat([1 - smx2, smx2], dim=1) # 组合成预测变量
if args.soft_label:
aux1 = - torch.sum(smx1 * torch.log(smx1 + 1e-10)) / smx1.shape[0]
aux2 = - torch.sum(smx2 * torch.log(smx2 + 1e-10)) / smx2.shape[0]
else:
smxY = smxY.float()
smxY = smxY.view(-1, 1)
smxY = torch.cat([1 - smxY, smxY], dim = 1)
aux1 = - torch.sum(smxY * torch.log(smx1 + 1e-10)) / smxY.shape[0] # 计算Xent loss
aux2 = - torch.sum(smxY * torch.log(smx2 + 1e-10)) / smxY.shape[0] # 计算Xent loss
loss = aux2
entropy_clean.update(aux2, 1)
if check_mean_teacher(epoch):
loss += consistency_loss
optimizer2.zero_grad()
loss.backward()
optimizer2.step()
pacc_2, nacc_2, pnacc_2, psize = accuracy(predictions2, T)
pacc_4, nacc_4, pnacc_4, psize = accuracy(predictiont2, T)
pacc2.update(pacc_2, psize)
nacc2.update(nacc_2, X.size(0) - psize)
pnacc2.update(pnacc_2, X.size(0))
pacc4.update(pacc_4, psize)
nacc4.update(nacc_4, X.size(0) - psize)
pnacc4.update(pnacc_4, X.size(0))
if check_mean_teacher(epoch):
update_ema_variables(model1, ema_model1, args.ema_decay, step) # 更新ema参数
update_ema_variables(model2, ema_model2, args.ema_decay, step)
step += 1
print('Epoch Clean : [{0}]\t'
'PACC1 {pacc1.val:.3f} ({pacc1.avg:.3f})\t'
'NACC1 {nacc1.val:.3f} ({nacc1.avg:.3f})\t'
'PNACC1 {pnacc1.val:.3f} ({pnacc1.avg:.3f})\t'
'PACC2 {pacc2.val:.3f} ({pacc2.avg:.3f})\t'
'NACC2 {nacc2.val:.3f} ({nacc2.avg:.3f})\t'
'PNACC2 {pnacc2.val:.3f} ({pnacc2.avg:.3f})\t'
'PACC3 {pacc3.val:.3f} ({pacc3.avg:.3f})\t'
'NACC3 {nacc3.val:.3f} ({nacc3.avg:.3f})\t'
'PNACC3 {pnacc3.val:.3f} ({pnacc3.avg:.3f})\t'
'PACC4 {pacc4.val:.3f} ({pacc4.avg:.3f})\t'
'NACC4 {nacc4.val:.3f} ({nacc4.avg:.3f})\t'
'PNACC4 {pnacc4.val:.3f} ({pnacc4.avg:.3f})\t'.format(
epoch, pacc1=pacc1, nacc1=nacc1, pnacc1=pnacc1,
pacc2=pacc2, nacc2=nacc2, pnacc2=pnacc2, pacc3=pacc3, nacc3=nacc3, pnacc3=pnacc3,
pacc4=pacc4, nacc4=nacc4, pnacc4=pnacc4))
for i, (X, Y, _, T, ids, _) in enumerate(noisy1_loader):
#print(torch.max(X))
X = X.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.view(X.shape[0], 1, -1)
Y = Y.cuda(args.gpu).float()
T = T.cuda(args.gpu).long()
# compute output
output1 = model1(X)
output2 = model2(X)
with torch.no_grad():
ema_output1 = ema_model1(X)
#if epoch >= args.self_paced_start: criterion.update_p(0.5)
_, loss = criterion(output1, Y)
consistency_loss = consistency_weight * \
consistency_criterion(output1, ema_output1) / X.shape[0]
#print(loss1)
predictions1 = torch.sign(output1).long()
predictiont1 = torch.sign(ema_output1).long()
smx1 = torch.sigmoid(output1) # 计算sigmoid概率
smx1 = torch.cat([1 - smx1, smx1], dim=1) # 组合成预测变量
smxY = ((Y + 1) // 2).long() # 分类结果,0-1分类
smx2 = torch.sigmoid(output2) # 计算sigmoid概率
smx2 = torch.cat([1 - smx2, smx2], dim=1) # 组合成预测变量
aux1 = - torch.sum(smx1 * torch.log(smx1 + 1e-10)) / smx1.shape[0]
entropy_noisy.update(aux1, 1)
if args.type == 'mu' and check_mean_teacher(epoch):
aux = F.mse_loss(smx1[:, 0], smx2[:, 0].detach())
if aux < loss * args.alpha:
loss += aux
count_noisy.update(1, X.size(0))
else:
count_noisy.update(0, X.size(0))
if check_mean_teacher(epoch):
loss += consistency_loss
optimizer1.zero_grad()
loss.backward()
optimizer1.step()
pacc_3, nacc_3, pnacc_3, psize = accuracy(predictiont1, T)
pacc_1, nacc_1, pnacc_1, psize = accuracy(predictions1, T) # 使用T来计算预测准确率
pacc1.update(pacc_1, psize)
nacc1.update(nacc_1, X.size(0) - psize)
pnacc1.update(pnacc_1, X.size(0))
pacc3.update(pacc_3, psize)
nacc3.update(nacc_3, X.size(0) - psize)
pnacc3.update(pnacc_3, X.size(0))
for i, (X, Y, _, T, ids, _) in enumerate(noisy2_loader):
X = X.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.view(X.shape[0], 1, -1)
Y = Y.cuda(args.gpu).float()
T = T.cuda(args.gpu).long()
# compute output
output1 = model1(X)
output2 = model2(X)
with torch.no_grad():
ema_output2 = ema_model2(X)
_, loss = criterion(output2, Y)
consistency_loss = consistency_weight * \
consistency_criterion(output2, ema_output2) / X.shape[0]
#print(loss2)
predictions2 = torch.sign(output2).long()
predictiont2 = torch.sign(ema_output2).long()
smx1 = torch.sigmoid(output1) # 计算sigmoid概率
smx1 = torch.cat([1 - smx1, smx1], dim=1) # 组合成预测变量
smxY = ((Y + 1) // 2).long() # 分类结果,0-1分类
smx2 = torch.sigmoid(output2) # 计算sigmoid概率
smx2 = torch.cat([1 - smx2, smx2], dim=1) # 组合成预测变量
aux2 = - torch.sum(smx2 * torch.log(smx2 + 1e-10)) / smx2.shape[0]
entropy_noisy.update(aux2, 1)
if args.type == 'mu' and check_mean_teacher(epoch):
aux = F.mse_loss(smx2[:, 0], smx1[:, 0].detach())
if aux < loss * args.alpha:
loss += aux
count_noisy.update(1, X.size(0))
else:
count_noisy.update(0, X.size(0))
if check_mean_teacher(epoch):
loss += consistency_loss
optimizer2.zero_grad()
loss.backward()
optimizer2.step()
pacc_2, nacc_2, pnacc_2, psize = accuracy(predictions2, T)
pacc_4, nacc_4, pnacc_4, psize = accuracy(predictiont2, T)
pacc2.update(pacc_2, psize)
nacc2.update(nacc_2, X.size(0) - psize)
pnacc2.update(pnacc_2, X.size(0))
pacc4.update(pacc_4, psize)
nacc4.update(nacc_4, X.size(0) - psize)
pnacc4.update(pnacc_4, X.size(0))
if check_mean_teacher(epoch):
update_ema_variables(model1, ema_model1, args.ema_decay, step) # 更新ema参数
update_ema_variables(model2, ema_model2, args.ema_decay, step)
step += 1
print('Epoch Noisy : [{0}]\t'
'PACC1 {pacc1.val:.3f} ({pacc1.avg:.3f})\t'
'NACC1 {nacc1.val:.3f} ({nacc1.avg:.3f})\t'
'PNACC1 {pnacc1.val:.3f} ({pnacc1.avg:.3f})\t'
'PACC2 {pacc2.val:.3f} ({pacc2.avg:.3f})\t'
'NACC2 {nacc2.val:.3f} ({nacc2.avg:.3f})\t'
'PNACC2 {pnacc2.val:.3f} ({pnacc2.avg:.3f})\t'
'PACC3 {pacc3.val:.3f} ({pacc3.avg:.3f})\t'
'NACC3 {nacc3.val:.3f} ({nacc3.avg:.3f})\t'
'PNACC3 {pnacc3.val:.3f} ({pnacc3.avg:.3f})\t'
'PACC4 {pacc4.val:.3f} ({pacc4.avg:.3f})\t'
'NACC4 {nacc4.val:.3f} ({nacc4.avg:.3f})\t'
'PNACC4 {pnacc4.val:.3f} ({pnacc4.avg:.3f})\t'.format(
epoch, pacc1=pacc1, nacc1=nacc1, pnacc1=pnacc1,
pacc2=pacc2, nacc2=nacc2, pnacc2=pnacc2, pacc3=pacc3, nacc3=nacc3, pnacc3=pnacc3,
pacc4=pacc4, nacc4=nacc4, pnacc4=pnacc4))
return pacc1.avg, nacc1.avg, pnacc1.avg
def validate(val_loader, model1, model2, ema_model1, ema_model2, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
pacc = AverageMeter()
nacc = AverageMeter()
pnacc1 = AverageMeter()
pnacc2 = AverageMeter()
pnacc3 = AverageMeter()
pnacc4 = AverageMeter()
model1.eval()
model2.eval()
ema_model1.eval()
ema_model2.eval()
end = time.time()
with torch.no_grad():
for i, (X, Y, _, T, ids, _) in enumerate(val_loader):
# measure data loading time
data_time.update(time.time() - end)
X = X.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.view(X.shape[0], 1, -1)
Y = Y.cuda(args.gpu).float()
T = T.cuda(args.gpu).long()
# compute output
output1 = model1(X)
output2 = model2(X)
ema_output1 = ema_model1(X)
ema_output2 = ema_model2(X)
predictions1 = torch.sign(output1).long()
predictions2 = torch.sign(output2).long()
predictiont1 = torch.sign(ema_output1).long()
predictiont2 = torch.sign(ema_output2).long()
pacc_, nacc_, pnacc_, psize = accuracy(predictions1, T)
pacc.update(pacc_, X.size(0))
nacc.update(nacc_, X.size(0))
pnacc1.update(pnacc_, X.size(0))
pacc_, nacc_, pnacc_, psize = accuracy(predictions2, T)
pnacc2.update(pnacc_, X.size(0))
pacc_, nacc_, pnacc_, psize = accuracy(predictiont1, T)
pnacc3.update(pnacc_, X.size(0))
pacc_, nacc_, pnacc_, psize = accuracy(predictiont2, T)
pnacc4.update(pnacc_, X.size(0))
print('Test [{0}]: \t'
'PNACC1 {pnacc1.val:.3f} ({pnacc1.avg:.3f})\t'
'PNACC2 {pnacc2.val:.3f} ({pnacc2.avg:.3f})\t'
'PNACC3 {pnacc3.val:.3f} ({pnacc3.avg:.3f})\t'
'PNACC4 {pnacc4.val:.3f} ({pnacc4.avg:.3f})\t'.format(
epoch, pnacc1=pnacc1, pnacc2=pnacc2, pnacc3=pnacc3, pnacc4 = pnacc4))
print("=====================================")
return pacc.avg, nacc.avg, pnacc1.avg, pnacc2.avg, pnacc3.avg , pnacc4.avg
def create_model(ema=False):
model = Model(28*28)
if ema:
for param in model.parameters():
param.detach_()
return model
def create_cifar_model(ema=False):
model = CNN()
if ema:
for param in model.parameters():
param.detach_()
return model
def update_ema_variables(model, ema_model, alpha, global_step):
alpha = min(1 - 1 / (global_step + 1), alpha)
for ema_param, param in zip(ema_model.parameters(), model.parameters()):
ema_param.data.mul_(alpha).add_(1 - alpha, (param))
def get_current_consistency_weight(epoch):
# Consistency ramp-up from https://arxiv.org/abs/1610.02242
return args.consistency * ramps.sigmoid_rampup(epoch, args.consistency_rampup)
def get_criterion():
weights = [float(args.weight), 1.0]
class_weights = torch.FloatTensor(weights)
class_weights = class_weights.cuda(args.gpu)
if args.loss == 'Xent':
criterion = PULoss(Probability_P=0.49, loss_fn="Xent")
elif args.loss == 'nnPU':
criterion = PULoss(Probability_P=0.49)
elif args.loss == 'Focal':
class_weights = torch.FloatTensor(weights).cuda(args.gpu)
criterion = FocalLoss(gamma=0, weight=class_weights, one_hot=False)
elif args.loss == 'uPU':
criterion = PULoss(Probability_P=0.49, nnPU=False)
elif args.loss == 'Xent_weighted':
criterion = torch.nn.CrossEntropyLoss(weight=class_weights)
return criterion
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
#print(val, n)
if self.count == 0:
self.avg = 0
else:
self.avg = self.sum / self.count
def accuracy(output, target):
with torch.no_grad():
batch_size = float(target.size(0))
output = output.view(-1)
correct = torch.sum(output == target).float()
pcorrect = torch.sum(output[target==1] == target[target == 1]).float()
ncorrect = correct - pcorrect
ptotal = torch.sum(target == 1).float()
if ptotal == 0:
return torch.tensor(0.).cuda(args.gpu), ncorrect / (batch_size - ptotal) * 100, correct / batch_size * 100, ptotal
elif ptotal == batch_size:
return pcorrect / ptotal * 100, torch.tensor(0.).cuda(args.gpu), correct / batch_size * 100, ptotal
else:
return pcorrect / ptotal * 100, ncorrect / (batch_size - ptotal) * 100, correct / batch_size * 100, ptotal
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
torch.save(state, filename[0])
if is_best:
shutil.copyfile(filename[0], filename[1])
def update_dataset(model1, model2, ema_model1, ema_model2, dataset_train1_clean, dataset_train1_noisy, dataset_train2_clean, dataset_train2_noisy, epoch, ratio=0.5):
#global results
global results1, results2
dataset_train1_noisy.reset_ids()
dataset_train1_noisy.set_type("clean")
dataset_train2_noisy.reset_ids()
dataset_train2_noisy.set_type("clean")
dataloader_train1 = DataLoader(dataset_train1_noisy, batch_size=args.batch_size, num_workers=4, shuffle=False, pin_memory=True)
dataloader_train2 = DataLoader(dataset_train2_noisy, batch_size=args.batch_size, num_workers=4, shuffle=False, pin_memory=True)
if args.dataset == 'mnist':
results1 = np.zeros(61000) #
results2 = np.zeros(61000)
elif args.dataset == 'cifar':
results1 = np.zeros(51000)
results2 = np.zeros(51000)
model1.eval()
model2.eval()
############ validation ###########
with torch.no_grad():
for i_batch, (X, Y,_, T, ids, _) in enumerate(tqdm(dataloader_train1)):
X = X.cuda(args.gpu)
Y = Y.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.reshape(-1, 1, 28*28)
Y = Y.float()
# ===================forward====================
if check_mean_teacher(epoch):
output1 = ema_model1(X)
else:
output1 = model1(X)
prob1 = torch.sigmoid(output1).view(-1).cpu().numpy()
results1[ids.view(-1).numpy()] = prob1
for i_batch, (X, Y, _, T, ids, _) in enumerate(tqdm(dataloader_train2)):
#images, lefts, rights, ages, genders, edus, apoes, labels, pu_labels, ids = Variable(sample_batched['mri']).cuda(args.gpu), Variable(sample_batched['left']).cuda(args.gpu), Variable(sample_batched['right']).cuda(args.gpu), Variable(sample_batched['age']).cuda(args.gpu), Variable(sample_batched['gender']).cuda(args.gpu), Variable(sample_batched['edu']).cuda(args.gpu), Variable(sample_batched['apoe']).cuda(args.gpu), Variable(sample_batched['label']).view(-1).type(torch.LongTensor).cuda(args.gpu), Variable(sample_batched['pu_label']).view(-1).type(torch.LongTensor).cuda(args.gpu), sample_batched['id']
X = X.cuda(args.gpu)
Y = Y.cuda(args.gpu)
if args.dataset == 'mnist':
X = X.reshape(-1, 1, 28*28)
Y = Y.float()
# ===================forward====================
if check_mean_teacher(epoch):
output2 = ema_model2(X)
else:
output2 = model2(X)
prob2 = torch.sigmoid(output2).view(-1).cpu().numpy()
results2[ids.view(-1).numpy()] = prob2
# adni_dataset_train.update_labels(results, ratio)
# dataset_origin = dataset_train
ids_noisy1 = dataset_train1_clean.update_ids(results1, epoch, ratio = ratio) # 返回的是noisy ids
ids_noisy2 = dataset_train2_clean.update_ids(results2, epoch, ratio = ratio)
dataset_train1_noisy.set_ids(ids_noisy1) # 将noisy ids更新进去
dataset_train1_noisy.set_type("noisy")
dataset_train2_noisy.set_ids(ids_noisy2) # 将noisy ids更新进去
dataset_train2_noisy.set_type("noisy")
#assert np.all(dataset_train_noisy.ids == ids_noisy) # 确定更新了
#dataloader_origin = DataLoader(dataset_origin, batch_size=args.batch_size, num_workers=4, drop_last=True, shuffle=True, pin_memory=True)
dataloader_train1_clean = DataLoader(dataset_train1_clean, batch_size=args.batch_size, num_workers=4, shuffle=True, pin_memory=True)
dataloader_train1_noisy = DataLoader(dataset_train1_noisy, batch_size=args.batch_size, num_workers=4, shuffle=False, pin_memory=True)
dataloader_train2_clean = DataLoader(dataset_train2_clean, batch_size=args.batch_size, num_workers=4, shuffle=True, pin_memory=True)
dataloader_train2_noisy = DataLoader(dataset_train2_noisy, batch_size=args.batch_size, num_workers=4, shuffle=False, pin_memory=True)
return dataloader_train1_clean, dataloader_train1_noisy, dataloader_train2_clean, dataloader_train2_noisy
def check_mean_teacher(epoch):
if not args.mean_teacher:
return False
elif epoch < args.ema_start:
return False
else:
return True
def check_self_paced(epoch):
if not args.self_paced:
return False
elif args.self_paced and epoch >= args.self_paced_stop:
return False
elif args.self_paced and epoch < args.self_paced_start:
return False
else: return True
if __name__ == '__main__':
main()