forked from Zhongying-Deng/NorMatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_normatch.py
693 lines (607 loc) · 29.4 KB
/
train_normatch.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
import argparse
import logging
import math
import os
import random
import shutil
import time
from collections import OrderedDict
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from torch.utils.data.distributed import DistributedSampler
from torch.utils.tensorboard import SummaryWriter
from torch.nn.utils import clip_grad_norm_
from tqdm import tqdm
from dataset.cifar import DATASET_GETTERS
from utils import AverageMeter, accuracy
from models.flow_model import FlowGMM
logger = logging.getLogger(__name__)
best_acc = 0
def save_checkpoint(state, is_best, checkpoint, filename='checkpoint.pth.tar'):
filepath = os.path.join(checkpoint, filename)
torch.save(state, filepath)
if is_best:
shutil.copyfile(filepath, os.path.join(checkpoint,
'model_best.pth.tar'))
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
def get_cosine_schedule_with_warmup(optimizer,
num_warmup_steps,
num_training_steps,
num_cycles=7./16.,
last_epoch=-1):
def _lr_lambda(current_step):
if current_step < num_warmup_steps:
return float(current_step) / float(max(1, num_warmup_steps))
no_progress = float(current_step - num_warmup_steps) / \
float(max(1, num_training_steps - num_warmup_steps))
return max(0., math.cos(math.pi * num_cycles * no_progress))
return LambdaLR(optimizer, _lr_lambda, last_epoch)
def interleave(x, size):
s = list(x.shape)
return x.reshape([-1, size] + s[1:]).transpose(0, 1).reshape([-1] + s[1:])
def de_interleave(x, size):
s = list(x.shape)
return x.reshape([size, -1] + s[1:]).transpose(0, 1).reshape([-1] + s[1:])
def main():
parser = argparse.ArgumentParser(description='PyTorch FixMatch Training')
parser.add_argument('--gpu-id', default='0', type=int,
help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--num-workers', type=int, default=4,
help='number of workers')
parser.add_argument('--dataset', default='cifar10', type=str,
choices=['cifar10', 'cifar100', 'svhn', 'stl10',
'imagenet', 'mini_imagenet'],
help='dataset name')
parser.add_argument('--num-labeled', type=int, default=4000,
help='number of labeled data')
parser.add_argument("--expand-labels", action="store_true",
help="expand labels to fit eval steps")
parser.add_argument('--arch', default='wideresnet', type=str,
choices=['wideresnet', 'resnext', 'resnet'],
help='dataset name')
parser.add_argument('--total-steps', default=2**20, type=int,
help='number of total steps to run')
parser.add_argument('--eval-step', default=1024, type=int,
help='number of eval steps to run')
parser.add_argument('--start-epoch', default=0, type=int,
help='manual epoch number (useful on restarts)')
parser.add_argument('--batch-size', default=64, type=int,
help='train batchsize')
parser.add_argument('--lr', '--learning-rate', default=0.03, type=float,
help='initial learning rate')
parser.add_argument('--warmup', default=0, type=float,
help='warmup epochs (unlabeled data based)')
parser.add_argument('--wdecay', default=5e-4, type=float,
help='weight decay')
parser.add_argument('--nesterov', action='store_true', default=True,
help='use nesterov momentum')
parser.add_argument('--use-ema', action='store_true', default=True,
help='use EMA model')
parser.add_argument('--ema-decay', default=0.999, type=float,
help='EMA decay rate')
parser.add_argument('--mu', default=7, type=int,
help='coefficient of unlabeled batch size')
parser.add_argument('--lambda-u', default=1, type=float,
help='coefficient of unlabeled loss')
parser.add_argument('--lambda-flow', default=1, type=float,
help='coefficient of flow GMM loss')
parser.add_argument('--lambda-flow-unsup', default=0., type=float,
help='coefficient of unsupervised log likelyhood loss for Flow GMM')
parser.add_argument('--T', default=1, type=float,
help='pseudo label temperature')
parser.add_argument('--threshold', default=0.95, type=float,
help='pseudo label threshold')
parser.add_argument('--out', default='result',
help='directory to output the result')
parser.add_argument('--resume', default='', type=str,
help='path to latest checkpoint (default: none)')
parser.add_argument('--seed', default=None, type=int,
help="random seed")
parser.add_argument("--amp", action="store_true",
help="use 16-bit (mixed) precision through NVIDIA apex AMP")
parser.add_argument("--opt_level", type=str, default="O1",
help="apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
"See details at https://nvidia.github.io/apex/amp.html")
parser.add_argument("--local_rank", type=int, default=-1,
help="For distributed training: local_rank")
parser.add_argument('--no-progress', action='store_true',
help="don't use progress bar")
parser.add_argument('--mixing', action='store_true',
help="use channel shuffle and mixing or not")
parser.add_argument('--flow-dist-trainable', action='store_true',
help="train parameters (mean/std/weights) of GMM distribution in NFlow")
parser.add_argument('--dist_align', action='store_true',
help="use distribution alignment or not")
parser.add_argument('--warmup_flow', default=5, type=float,
help='warmup epochs for FlowGMM')
parser.add_argument('--no_grad_clip', action='store_true',
help="whether use grad clip for FlowGMM or not")
parser.add_argument('--no_onehot', action='store_true',
help="whether use one-hot pseudo label or not")
parser.add_argument('--da_cache', default=32, type=int,
help="cache len for distribution alignment")
args = parser.parse_args()
global best_acc
def create_model(args):
if args.arch == 'wideresnet':
import models.wideresnet as models
model = models.build_wideresnet(depth=args.model_depth,
widen_factor=args.model_width,
dropout=0,
num_classes=args.num_classes)
elif args.arch == 'resnext':
import models.resnext as models
model = models.build_resnext(cardinality=args.model_cardinality,
depth=args.model_depth,
width=args.model_width,
num_classes=args.num_classes)
else:
import models.resnet as models
# resnet18 for STL10
if args.dataset == 'stl10' or args.dataset == 'mini_imagenet':
model = models.resnet18(num_classes=args.num_classes, pretrained=False)
else:
model = models.resnet50(num_classes=args.num_classes, pretrained=False)
logger.info("Total params: {:.2f}M".format(
sum(p.numel() for p in model.parameters())/1e6))
return model
if args.local_rank == -1:
device = torch.device('cuda', args.gpu_id)
args.world_size = 1
args.n_gpu = torch.cuda.device_count()
else:
torch.cuda.set_device(args.local_rank)
device = torch.device('cuda', args.local_rank)
torch.distributed.init_process_group(backend='nccl')
args.world_size = torch.distributed.get_world_size()
args.n_gpu = 1
args.device = device if torch.cuda.is_available() else torch.device('cpu')
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN)
logger.warning(
f"Process rank: {args.local_rank}, "
f"device: {args.device}, "
f"n_gpu: {args.n_gpu}, "
f"distributed training: {bool(args.local_rank != -1)}, "
f"16-bits training: {args.amp}",)
logger.info(dict(args._get_kwargs()))
if args.seed is not None:
set_seed(args)
if args.local_rank in [-1, 0]:
os.makedirs(args.out, exist_ok=True)
args.writer = SummaryWriter(args.out)
if args.dataset == 'cifar10' or args.dataset == 'svhn':
args.num_classes = 10
if args.arch == 'wideresnet':
args.model_depth = 28
args.model_width = 2
elif args.arch == 'resnext':
args.model_cardinality = 4
args.model_depth = 28
args.model_width = 4
elif args.dataset == 'cifar100':
args.num_classes = 100
if args.arch == 'wideresnet':
args.model_depth = 28
args.model_width = 8
elif args.arch == 'resnext':
args.model_cardinality = 8
args.model_depth = 29
args.model_width = 64
elif args.dataset == 'stl10':
args.num_classes = 10
elif args.dataset == 'mini_imagenet':
args.num_classes = 100
else:
args.num_classes = 1000
if args.local_rank not in [-1, 0]:
torch.distributed.barrier()
if args.dataset == 'stl10':
from dataset.stl10 import STL10_GETTERS
labeled_dataset, unlabeled_dataset, test_dataset = STL10_GETTERS[args.dataset](
args, './data/stl10')
elif args.dataset == 'imagenet':
from dataset.imagenet import ImageNet_GETTERS
labeled_dataset, unlabeled_dataset, test_dataset = ImageNet_GETTERS[args.dataset](
args, './data/imagenet')
elif args.dataset == 'mini_imagenet':
from dataset.mini_imagenet import MiniImageNet_GETTERS
labeled_dataset, unlabeled_dataset, test_dataset = MiniImageNet_GETTERS[args.dataset](
args, './data/imagenet/mini_imagenet')
else:
labeled_dataset, unlabeled_dataset, test_dataset = DATASET_GETTERS[args.dataset](
args, './data')
if args.local_rank == 0:
torch.distributed.barrier()
train_sampler = RandomSampler if args.local_rank == -1 else DistributedSampler
labeled_trainloader = DataLoader(
labeled_dataset,
sampler=train_sampler(labeled_dataset),
batch_size=args.batch_size,
num_workers=args.num_workers,
drop_last=True)
unlabeled_trainloader = DataLoader(
unlabeled_dataset,
sampler=train_sampler(unlabeled_dataset),
batch_size=args.batch_size*args.mu,
num_workers=args.num_workers,
drop_last=True)
test_loader = DataLoader(
test_dataset,
sampler=SequentialSampler(test_dataset),
batch_size=args.batch_size,
num_workers=args.num_workers)
if args.local_rank not in [-1, 0]:
torch.distributed.barrier()
model = create_model(args)
if args.arch == 'wideresnet':
dim = model.channels
elif args.arch == 'resnext':
dim = model.stages[3]
else:
dim = model.fdim
mean, inv_cov_stds, weights = None, None, None
if args.resume:
logger.info("==> Resuming from checkpoint..")
assert os.path.isfile(
args.resume), "Error: no checkpoint directory found!"
args.out = os.path.dirname(args.resume)
checkpoint = torch.load(args.resume)
if args.flow_dist_trainable:
mean = checkpoint['flow_mean']
inv_cov_stds = checkpoint['flow_std']
weights = checkpoint['flow_weights']
flow_model = FlowGMM(dim, args.num_classes, args,
mean, inv_cov_stds, weights)
logger.info("Total params of FlowGMM: {:.2f}M".format(
sum(p.numel() for p in flow_model.parameters())/1e6))
if args.local_rank == 0:
torch.distributed.barrier()
model.to(args.device)
flow_model.to(args.device)
flow_model.prior.means.requires_grad = args.flow_dist_trainable
flow_model.prior.weights.requires_grad = args.flow_dist_trainable
flow_model.prior.inv_cov_stds.requires_grad = args.flow_dist_trainable
no_decay = ['bias', 'bn']
grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(
nd in n for nd in no_decay)], 'weight_decay': args.wdecay},
{'params': [p for n, p in model.named_parameters() if any(
nd in n for nd in no_decay)], 'weight_decay': 0.0},
#{'params': [p for n, p in flow_model.named_parameters()],
# 'lr': 0.1 * args.lr, 'weight_decay': args.wdecay}
]
optimizer = optim.SGD(grouped_parameters, lr=args.lr,
momentum=0.9, nesterov=args.nesterov)
param_flow = [{'params': [p for n, p in flow_model.named_parameters()],
'weight_decay': args.wdecay},
{'params': [flow_model.prior.inv_cov_stds, flow_model.prior.means, flow_model.prior.weights],
'weight_decay': args.wdecay}
]
optimizer_flow = optim.AdamW(param_flow, lr=0.001) # lr=0.0005
args.epochs = math.ceil(args.total_steps / args.eval_step)
scheduler = get_cosine_schedule_with_warmup(
optimizer, args.warmup, args.total_steps)
scheduler_flow = get_cosine_schedule_with_warmup(
optimizer_flow, args.warmup, args.total_steps)
if args.use_ema:
from models.ema import ModelEMA
ema_model = ModelEMA(args, model, args.ema_decay)
args.start_epoch = 0
if args.resume:
best_acc = checkpoint['best_acc']
args.start_epoch = checkpoint['epoch']
print('Resume epoch: {}'.format(args.start_epoch))
model.load_state_dict(checkpoint['state_dict'])
flow_model.load_state_dict(checkpoint['flow_state_dict'])
if args.use_ema:
ema_model.ema.load_state_dict(checkpoint['ema_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
optimizer_flow.load_state_dict(checkpoint['flow_optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
scheduler_flow.load_state_dict(checkpoint['flow_scheduler'])
if args.amp:
from apex import amp
model, optimizer = amp.initialize(
model, optimizer, opt_level=args.opt_level)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[args.local_rank],
output_device=args.local_rank, find_unused_parameters=True)
flow_model = torch.nn.parallel.DistributedDataParallel(
flow_model, device_ids=[args.local_rank],
output_device=args.local_rank, find_unused_parameters=True)
logger.info("***** Running training *****")
logger.info(f" Task = {args.dataset}@{args.num_labeled}")
logger.info(f" Num Epochs = {args.epochs}")
logger.info(f" Batch size per GPU = {args.batch_size}")
logger.info(
f" Total train batch size = {args.batch_size*args.world_size}")
logger.info(f" Total optimization steps = {args.total_steps}")
model.zero_grad()
train(args, labeled_trainloader, unlabeled_trainloader, test_loader,
model, flow_model, optimizer, optimizer_flow, ema_model, scheduler, scheduler_flow)
def train(args, labeled_trainloader, unlabeled_trainloader, test_loader,
model, flow_model, optimizer, optimizer_flow, ema_model, scheduler, scheduler_flow):
if args.amp:
from apex import amp
global best_acc
test_accs = []
end = time.time()
if args.world_size > 1:
labeled_epoch = 0
unlabeled_epoch = 0
labeled_trainloader.sampler.set_epoch(labeled_epoch)
unlabeled_trainloader.sampler.set_epoch(unlabeled_epoch)
labeled_iter = iter(labeled_trainloader)
unlabeled_iter = iter(unlabeled_trainloader)
model.train()
flow_model.train()
if args.mixing:
mixing = True
else:
mixing = None
prob_list = []
for epoch in range(args.start_epoch, args.epochs):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
losses_x = AverageMeter()
losses_u = AverageMeter()
losses_flow = AverageMeter()
losses_unsup = AverageMeter()
mask_probs = AverageMeter()
uncertainty_disc = AverageMeter()
uncertainty_nflow = AverageMeter()
weight_low_conf = AverageMeter()
lambda_flow = min(epoch / 5., 1.) * args.lambda_flow
lambda_flow_unsup = min(epoch / 5., 1.) * args.lambda_flow_unsup
pl_quality = 0.
acc_high_conf = 0.
acc_consensus = 0.
acc_disagree = 0.
mask_high_consensus = 0.
mask_no_consensus = 0.
num_high_conf = 0.
num_total = 0.
if not args.no_progress:
p_bar = tqdm(range(args.eval_step),
disable=args.local_rank not in [-1, 0])
for batch_idx in range(args.eval_step):
try:
inputs_x, targets_x = labeled_iter.next()
except:
if args.world_size > 1:
labeled_epoch += 1
labeled_trainloader.sampler.set_epoch(labeled_epoch)
labeled_iter = iter(labeled_trainloader)
inputs_x, targets_x = labeled_iter.next()
try:
# targets_u_gt only used to show the noise level of pseudo-labels
(inputs_u_w, inputs_u_s), targets_u_gt = unlabeled_iter.next()
except:
if args.world_size > 1:
unlabeled_epoch += 1
unlabeled_trainloader.sampler.set_epoch(unlabeled_epoch)
unlabeled_iter = iter(unlabeled_trainloader)
(inputs_u_w, inputs_u_s), targets_u_gt = unlabeled_iter.next()
data_time.update(time.time() - end)
batch_size = inputs_x.shape[0]
inputs = interleave(
torch.cat((inputs_x, inputs_u_w, inputs_u_s)), 2*args.mu+1).to(args.device)
targets_x = targets_x.to(args.device)
targets_u_gt = targets_u_gt.to(args.device)
feats, logits = model(inputs, mixing=mixing, return_feat=True)
logits = de_interleave(logits, 2*args.mu+1)
logits_x = logits[:batch_size]
logits_u_w, logits_u_s = logits[batch_size:].chunk(2)
del logits
feats = de_interleave(feats, 2*args.mu+1)
feats_x = feats[:batch_size]
feats_u_w, feats_u_s = feats[batch_size:].chunk(2)
loss_flow, _ = flow_model(feats_x.detach(), targets_x, return_unsup_loss=True)
Lx = F.cross_entropy(logits_x, targets_x, reduction='mean')
pseudo_label = torch.softmax(logits_u_w.detach()/args.T, dim=-1)
if args.dist_align:
prob_list.append(pseudo_label.mean(0))
if len(prob_list)>args.da_cache:
prob_list.pop(0)
prob_avg = torch.stack(prob_list,dim=0).mean(0)
pseudo_label = pseudo_label / prob_avg
pseudo_label = pseudo_label / pseudo_label.sum(dim=1, keepdim=True)
max_probs, targets_u = torch.max(pseudo_label, dim=-1)
uncertainty_disc.update((1-max_probs).mean().item())
# caculate ratio of correct pseudo-labels
correct_pl = (targets_u == targets_u_gt).float()
pl_quality += correct_pl.sum()
mask_thresh = max_probs.ge(args.threshold).float()
correct_high_conf = mask_thresh * correct_pl
acc_high_conf += correct_high_conf.sum() # acc of high confidence pseudo-labels
num_high_conf += mask_thresh.sum()
num_total += max_probs.size(0)
loss_unsup, logits_u_flow = flow_model(feats_u_w.detach())
logits_u_flow = F.softmax(logits_u_flow, 1)
probs_flow, targets_u_flow = torch.max(logits_u_flow, dim=-1)
uncertainty_nflow.update((1-probs_flow).mean().item())
mask = (targets_u_flow == targets_u).float()
correct_consensus = mask * correct_pl
acc_consensus += correct_consensus.sum() # acc of consensus pseudo-labels (NCUE)
mask_high_consensus += mask.sum()
correct_disagree = (1-mask) * correct_pl # acc of disagreed pseudo-labels
acc_disagree += correct_disagree.sum()
mask_no_consensus += (1-mask).sum()
tmp = (1-mask) * torch.min(max_probs, probs_flow)
weight_disagree = tmp.sum()/((1-mask).sum()+1e-7)
weight_low_conf.update(weight_disagree.item())
mask = torch.max(mask, tmp)
if args.no_onehot:
Lu = (torch.sum(-F.log_softmax(logits_u_s,dim=1) * pseudo_label.detach(), dim=1) * mask).mean()
else:
Lu = (F.cross_entropy(logits_u_s, targets_u,
reduction='none') * mask).mean()
loss = Lx + args.lambda_u * Lu + lambda_flow * loss_flow + lambda_flow_unsup * loss_unsup
if args.amp:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
losses.update(loss.item())
losses_x.update(Lx.item())
losses_u.update(Lu.item())
losses_flow.update(loss_flow.item())
losses_unsup.update(loss_unsup.item())
optimizer.step()
scheduler.step()
if not args.no_grad_clip:
clip_grad_norm_(flow_model.parameters(), max_norm=50, norm_type=2)
optimizer_flow.step()
scheduler_flow.step()
if args.use_ema:
ema_model.update(model)
model.zero_grad()
flow_model.zero_grad()
batch_time.update(time.time() - end)
end = time.time()
mask_probs.update(mask.mean().item())
if not args.no_progress:
p_bar.set_description("Train Epoch: {epoch}/{epochs:4}. Iter: {batch:4}/{iter:4}. LR: {lr:.4f}. Data: {data:.3f}s. Batch: {bt:.3f}s. Loss: {loss:.4f}. Loss_x: {loss_x:.4f}. Loss_u: {loss_u:.4f}. Loss_f: {loss_f:.4f} Mask: {mask:.2f}.".format(
epoch=epoch + 1,
epochs=args.epochs,
batch=batch_idx + 1,
iter=args.eval_step,
lr=scheduler.get_last_lr()[0],
data=data_time.avg,
bt=batch_time.avg,
loss=losses.avg,
loss_x=losses_x.avg,
loss_u=losses_u.avg,
loss_f=losses_flow.avg,
mask=mask_probs.avg))
p_bar.update()
else:
if batch_idx % 100 == 0:
print("Train Epoch: {epoch}/{epochs:4}. Iter: {batch:4}/{iter:4}. LR: {lr:.4f}. Data: {data:.3f}s. Batch: {bt:.3f}s. Loss: {loss:.4f}. Loss_x: {loss_x:.4f}. Loss_u: {loss_u:.4f}. Loss_f: {loss_f:.4f}, Loss_unsup: {loss_unsup:.4f}, Mask: {mask:.2f}.".format(
epoch=epoch + 1,
epochs=args.epochs,
batch=batch_idx + 1,
iter=args.eval_step,
lr=scheduler.get_last_lr()[0],
data=data_time.avg,
bt=batch_time.avg,
loss=losses.avg,
loss_x=losses_x.avg,
loss_u=losses_u.avg,
loss_f=losses_flow.avg,
loss_unsup=losses_unsup.avg,
mask=mask_probs.avg)
)
print('High Confidence Ratio: {ratio:.2f}, Correct Pseudo-label Ratio: {ratio_pl:0.2f}, Consensus Ratio: {ratio_co:0.2f}'.format(
ratio=100*(num_high_conf/num_total), ratio_pl=100*(pl_quality/num_total),
ratio_co=100*(mask_high_consensus/num_total)
))
print('Acc_high_conf: {:.2f}, Acc_high_consensus: {:.2f}, Acc_no_consensus: {:.2f}'.format(
100*(acc_high_conf/(num_high_conf+1e-7)), 100*(acc_consensus/(mask_high_consensus+1e-7)), 100*(acc_disagree/(mask_no_consensus+1e-7))
))
print('Uncertainty of Discriminative classifier: {:.2f}, NFlow classifier: {:.2f}, weight_low_confidence: {:.2f}'.format(
uncertainty_disc.avg, uncertainty_nflow.avg, weight_low_conf.avg
))
if not args.no_progress:
p_bar.close()
if args.use_ema:
test_model = ema_model.ema
else:
test_model = model
if args.local_rank in [-1, 0]:
test_loss, test_acc = test(args, test_loader, test_model, flow_model, epoch)
args.writer.add_scalar('train/1.train_loss', losses.avg, epoch)
args.writer.add_scalar('train/2.train_loss_x', losses_x.avg, epoch)
args.writer.add_scalar('train/3.train_loss_u', losses_u.avg, epoch)
args.writer.add_scalar('train/4.mask', mask_probs.avg, epoch)
args.writer.add_scalar('test/1.test_acc', test_acc, epoch)
args.writer.add_scalar('test/2.test_loss', test_loss, epoch)
is_best = test_acc > best_acc
best_acc = max(test_acc, best_acc)
model_to_save = model.module if hasattr(model, "module") else model
flow_model_to_save = flow_model.module if hasattr(flow_model, "module") else flow_model
if args.use_ema:
ema_to_save = ema_model.ema.module if hasattr(
ema_model.ema, "module") else ema_model.ema
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model_to_save.state_dict(),
'ema_state_dict': ema_to_save.state_dict() if args.use_ema else None,
'acc': test_acc,
'best_acc': best_acc,
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'flow_state_dict': flow_model_to_save.state_dict(),
'flow_mean': flow_model_to_save.prior.means,
'flow_std': flow_model_to_save.prior.inv_cov_stds,
'flow_weights': flow_model_to_save.prior.weights,
'flow_optimizer': optimizer_flow.state_dict(),
'flow_scheduler': scheduler_flow.state_dict(),
}, is_best, args.out)
test_accs.append(test_acc)
logger.info('Best top-1 acc: {:.2f}'.format(best_acc))
logger.info('Mean top-1 acc: {:.2f}\n'.format(
np.mean(test_accs[-20:])))
if args.local_rank in [-1, 0]:
args.writer.close()
def test(args, test_loader, model, flow_model, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
top1_flow = AverageMeter()
end = time.time()
if not args.no_progress:
test_loader = tqdm(test_loader,
disable=args.local_rank not in [-1, 0])
with torch.no_grad():
flow_model.eval()
model.eval()
for batch_idx, (inputs, targets) in enumerate(test_loader):
data_time.update(time.time() - end)
inputs = inputs.to(args.device)
targets = targets.to(args.device)
feats, outputs = model(inputs, return_feat=True)
outputs_flow = flow_model.predict(feats)
loss = F.cross_entropy(outputs, targets)
prec1, prec5 = accuracy(outputs, targets, topk=(1, 5))
losses.update(loss.item(), inputs.shape[0])
top1.update(prec1.item(), inputs.shape[0])
top5.update(prec5.item(), inputs.shape[0])
prec1, prec5 = accuracy(outputs_flow, targets, topk=(1, 5))
top1_flow.update(prec1.item(), inputs.shape[0])
batch_time.update(time.time() - end)
end = time.time()
if not args.no_progress:
test_loader.set_description("Test Iter: {batch:4}/{iter:4}. Data: {data:.3f}s. Batch: {bt:.3f}s. Loss: {loss:.4f}. top1: {top1:.2f}. top5: {top5:.2f}. ".format(
batch=batch_idx + 1,
iter=len(test_loader),
data=data_time.avg,
bt=batch_time.avg,
loss=losses.avg,
top1=top1.avg,
top5=top5.avg,
))
if not args.no_progress:
test_loader.close()
logger.info("top-1 acc: {:.2f}".format(top1.avg))
logger.info("top-5 acc: {:.2f}".format(top5.avg))
logger.info("top-1 flow model acc: {:.2f}".format(top1_flow.avg))
return losses.avg, top1.avg
if __name__ == '__main__':
main()