-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixmatch.py
698 lines (584 loc) · 28.6 KB
/
fixmatch.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
# import needed library
import logging
import os
import contextlib
import random
import warnings
from collections import Counter
from copy import deepcopy
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
from sklearn.metrics import *
from torch.cuda.amp import autocast, GradScaler
from datasets.dataset_helper import get_dataset_and_loader
from models.fixmatch.fixmatch import FixMatch
from models.fixmatch.fixmatch_utils import consistency_loss, Get_Scalar
from train_utils import TBLog, get_optimizer, get_cosine_schedule_with_warmup, ce_loss, EMA, Bn_Controller
from utils import net_builder, get_logger, count_parameters, over_write_args_from_file, print_args
class FixMatch:
def __init__(self, net_builder, num_classes, ema_m, T, p_cutoff, lambda_u, hard_label=True, t_fn=None, p_fn=None, it=0, num_eval_iter=1000, tb_log=None, logger=None):
"""
class Fixmatch contains setter of data_loader, optimizer, and model update methods.
Args:
net_builder: backbone network class (see net_builder in utils.py)
num_classes: # of label classes
ema_m: momentum of exponential moving average for eval_model
T: Temperature scaling parameter for output sharpening (only when hard_label = False)
p_cutoff: confidence cutoff parameters for loss masking
lambda_u: ratio of unsupervised loss to supervised loss
hard_label: If True, consistency regularization use a hard pseudo label.
it: initial iteration count
num_eval_iter: freqeuncy of iteration (after 500,000 iters)
tb_log: tensorboard writer (see train_utils.py)
logger: logger (see utils.py)
"""
super(FixMatch, self).__init__()
# momentum update param
self.loader = {}
self.num_classes = num_classes
self.ema_m = ema_m
# create the encoders
# network is builded only by num_classes,
# other configs are covered in main.py
self.model = net_builder(num_classes=num_classes)
self.ema_model = None
self.num_eval_iter = num_eval_iter
self.t_fn = Get_Scalar(T) # temperature params function
self.p_fn = Get_Scalar(p_cutoff) # confidence cutoff function
self.lambda_u = lambda_u
self.tb_log = tb_log
self.use_hard_label = hard_label
self.optimizer = None
self.scheduler = None
self.it = 0
self.best_eval_acc = 0.0
self.best_it = 0
self.lst = [[] for i in range(10)]
self.abs_lst = [[] for i in range(10)]
self.clsacc = [[] for i in range(10)]
self.logger = logger
self.print_fn = print if logger is None else logger.info
self.bn_controller = Bn_Controller()
def set_data_loader(self, loader_dict):
self.loader_dict = loader_dict
self.print_fn(f'[!] data loader keys: {self.loader_dict.keys()}')
def set_dset(self, dset):
self.ulb_dset = dset
def set_optimizer(self, optimizer, scheduler=None):
self.optimizer = optimizer
self.scheduler = scheduler
def train(self, args, logger=None):
ngpus_per_node = torch.cuda.device_count() if torch.cuda.is_available() else 1
# EMA Init
debug_print = True
self.model.train()
self.ema = EMA(self.model, self.ema_m)
self.ema.register()
if args.resume == True:
self.ema.load(self.ema_model)
# for gpu profiling
if torch.cuda.is_available():
start_batch = torch.cuda.Event(enable_timing=True)
end_batch = torch.cuda.Event(enable_timing=True)
start_run = torch.cuda.Event(enable_timing=True)
end_run = torch.cuda.Event(enable_timing=True)
start_batch.record()
scaler = GradScaler()
amp_cm = autocast if args.amp else contextlib.nullcontext
# eval for once to verify if the checkpoint is loaded correctly
if args.resume == True:
eval_dict = self.evaluate(args=args)
print(eval_dict)
selected_label = torch.ones((len(self.ulb_dset),), dtype=torch.long, ) * -1
selected_label = selected_label
classwise_acc = torch.zeros((args.num_classes,))
if torch.cuda.is_available():
selected_label = selected_label.cuda(args.gpu)
classwise_acc = classwise_acc.cuda(args.gpu)
for (_, x_lb, y_lb), (x_ulb_idx, x_ulb_w, x_ulb_s) in zip(self.loader_dict['train_lb'],
self.loader_dict['train_ulb']):
# prevent the training iterations exceed args.num_train_iter
if self.it > args.num_train_iter:
break
if torch.cuda.is_available():
end_batch.record()
torch.cuda.synchronize()
start_run.record()
num_lb = x_lb.shape[0]
num_ulb = x_ulb_w.shape[0]
assert num_ulb == x_ulb_s.shape[0]
if torch.cuda.is_available():
x_lb, x_ulb_w, x_ulb_s = x_lb.cuda(args.gpu), x_ulb_w.cuda(args.gpu), x_ulb_s.cuda(args.gpu)
y_lb = y_lb.long().cuda(args.gpu)
pseudo_counter = Counter(selected_label.tolist())
if max(pseudo_counter.values()) < len(self.ulb_dset): # not all(5w) -1
for i in range(args.num_classes):
classwise_acc[i] = pseudo_counter[i] / max(pseudo_counter.values())
inputs = torch.cat((x_lb, x_ulb_w, x_ulb_s))
if debug_print:
debug_print = False
print(f'inputs shape: {inputs.shape}')
print(f'x_lb shape: {x_lb.shape}')
print(f'x_ulb_w shape: {x_ulb_w.shape}')
print(f'x_ulb_s shape: {x_ulb_s.shape}')
print(f'y_lb shape: {y_lb.shape}')
# inference and calculate sup/unsup losses
with amp_cm():
logits = self.model(inputs)
logits_x_lb = logits[:num_lb]
logits_x_ulb_w, logits_x_ulb_s = logits[num_lb:].chunk(2)
sup_loss = ce_loss(logits_x_lb, y_lb, reduction='mean')
# hyper-params for update
T = self.t_fn(self.it)
p_cutoff = self.p_fn(self.it)
unsup_loss, mask, select, pseudo_lb = consistency_loss(logits_x_ulb_s,
logits_x_ulb_w,
'ce', T, p_cutoff,
use_hard_labels=args.hard_label)
total_loss = sup_loss + self.lambda_u * unsup_loss
# parameter updates
if args.amp:
scaler.scale(total_loss).backward()
if (args.clip > 0):
torch.nn.utils.clip_grad_norm_(self.model.parameters(), args.clip)
scaler.step(self.optimizer)
scaler.update()
else:
total_loss.backward()
if (args.clip > 0):
torch.nn.utils.clip_grad_norm_(self.model.parameters(), args.clip)
self.optimizer.step()
self.scheduler.step()
self.ema.update()
self.model.zero_grad()
if torch.cuda.is_available():
end_run.record()
torch.cuda.synchronize()
# tensorboard_dict update
tb_dict = {}
tb_dict['train/sup_loss'] = sup_loss.detach()
tb_dict['train/unsup_loss'] = unsup_loss.detach()
tb_dict['train/total_loss'] = total_loss.detach()
tb_dict['train/mask_ratio'] = 1.0 - mask.detach()
tb_dict['lr'] = self.optimizer.param_groups[0]['lr']
if torch.cuda.is_available():
tb_dict['train/prefecth_time'] = start_batch.elapsed_time(end_batch) / 1000.
tb_dict['train/run_time'] = start_run.elapsed_time(end_run) / 1000.
# Save model for each 10K steps and best model for each 1K steps
if self.it % 10000 == 0:
save_path = os.path.join(args.save_dir, args.save_name)
if not args.multiprocessing_distributed or \
(args.multiprocessing_distributed and args.rank % ngpus_per_node == 0):
self.save_model('latest_model.pth', save_path)
if 'terminal_iter' in args:
if self.it >= args.terminal_iter:
break
if self.it % self.num_eval_iter == 0:
eval_dict = self.evaluate(args=args)
tb_dict.update(eval_dict)
save_path = os.path.join(args.save_dir, args.save_name)
if tb_dict['eval/top-1-acc'] > self.best_eval_acc:
self.best_eval_acc = tb_dict['eval/top-1-acc']
self.best_it = self.it
self.print_fn(
f"{self.it} iteration, USE_EMA: {self.ema_m != 0}, {tb_dict}, BEST_EVAL_ACC: {self.best_eval_acc}, at {self.best_it} iters")
if not args.multiprocessing_distributed or \
(args.multiprocessing_distributed and args.rank % ngpus_per_node == 0):
if self.it == self.best_it:
self.save_model('model_best.pth', save_path)
if not self.tb_log is None:
self.tb_log.update(tb_dict, self.it)
self.it += 1
del tb_dict
if torch.cuda.is_available():
start_batch.record()
if self.it > 0.8 * args.num_train_iter:
self.num_eval_iter = 1000
eval_dict = self.evaluate(args=args)
eval_dict.update({'eval/best_acc': self.best_eval_acc, 'eval/best_it': self.best_it})
try:
os.makedirs(args.save_dir + '/eval_acc', exist_ok=True)
with open(os.path.join(args.save_dir + '/eval_acc', args.save_name[:-2] + '.txt'), 'a') as f:
f.write(args.save_name + ' ' + str(round(self.best_eval_acc * 100, 2)) + '\n')
except:
pass
return eval_dict
@torch.no_grad()
def evaluate(self, eval_loader=None, args=None):
self.model.eval()
self.ema.apply_shadow()
if eval_loader is None:
eval_loader = self.loader_dict['eval']
total_loss = 0.0
total_num = 0.0
y_true = []
y_pred = []
y_logits = []
for _, x, y in eval_loader:
if torch.cuda.is_available():
x, y = x.cuda(args.gpu), y.cuda(args.gpu)
num_batch = x.shape[0]
total_num += num_batch
logits = self.model(x)
loss = F.cross_entropy(logits, y, reduction='mean')
y_true.extend(y.cpu().tolist())
y_pred.extend(torch.max(logits, dim=-1)[1].cpu().tolist())
y_logits.extend(torch.softmax(logits, dim=-1).cpu().tolist())
total_loss += loss.detach() * num_batch
top1 = accuracy_score(y_true, y_pred)
top5 = top_k_accuracy_score(y_true, y_logits, k=5)
precision = precision_score(y_true, y_pred, average='macro')
recall = recall_score(y_true, y_pred, average='macro')
F1 = f1_score(y_true, y_pred, average='macro')
AUC = roc_auc_score(y_true, y_logits, multi_class='ovo')
cf_mat = confusion_matrix(y_true, y_pred) # , normalize='true'
self.print_fn('confusion matrix:\n' + np.array_str(cf_mat))
self.ema.restore()
self.model.train()
return {'eval/loss': total_loss / total_num, 'eval/top-1-acc': top1, 'eval/top-5-acc': top5,
'eval/precision': precision, 'eval/recall': recall, 'eval/F1': F1, 'eval/AUC': AUC}
def save_model(self, save_name, save_path):
save_filename = os.path.join(save_path, save_name)
# copy EMA parameters to ema_model for saving with model as temp
self.model.eval()
self.ema.apply_shadow()
ema_model = self.model.state_dict()
self.ema.restore()
self.model.train()
torch.save({'model': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scheduler': self.scheduler.state_dict(),
'it': self.it,
'best_eval_acc': self.best_eval_acc,
'best_it': self.best_it,
'ema_model': ema_model},
save_filename)
self.print_fn(f"model saved: {save_filename}")
def load_model(self, load_path):
checkpoint = torch.load(load_path)
self.print_fn('Loading save model from: ' + str(load_path))
self.ema_model = deepcopy(self.model)
try:
self.model.load_state_dict(checkpoint['model'])
self.ema_model.load_state_dict(checkpoint['ema_model'])
self.print_fn('model loaded')
except Exception as e:
self.print_fn('SAVED CHECKPOINT IS NOT COMPATIBLE WITH CURRENT MODEL! ATTEMPTING TO LOAD MANUALLY')
# Scenario 1: current single gpu & loading multi-gpu
if type(self.model) != torch.nn.parallel.distributed.DistributedDataParallel:
state_dict = checkpoint['model']
new_state_dict = {}
for k, v in state_dict.items():
k = k.replace("module.", "")
new_state_dict[k] = v
state_dict = new_state_dict
self.model.load_state_dict(state_dict)
state_dict_ema = checkpoint['ema_model']
new_state_dict_ema = {}
for k, v in state_dict_ema.items():
k = k.replace("module.", "")
new_state_dict_ema[k] = v
state_dict_ema = new_state_dict_ema
self.ema_model.load_state_dict(state_dict_ema)
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.scheduler.load_state_dict(checkpoint['scheduler'])
self.it = checkpoint['it']
self.print_fn('model loaded from multi-gpu checkpoint to single gpu')
# Scenario 2: saved multi-gpu loading single-gpu
else:
state_dict = checkpoint['model']
new_state_dict = {}
for k, v in state_dict.items():
k = "module." + k
new_state_dict[k] = v
state_dict = new_state_dict
self.model.load_state_dict(state_dict)
state_dict_ema = checkpoint['ema_model']
new_state_dict_ema = {}
for k, v in state_dict_ema.items():
k = "module." + k
new_state_dict_ema[k] = v
state_dict_ema = new_state_dict_ema
self.ema_model.load_state_dict(state_dict_ema)
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.scheduler.load_state_dict(checkpoint['scheduler'])
self.it = checkpoint['it']
self.print_fn('model loaded from single-gpu checkpoint to multi gpu')
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.scheduler.load_state_dict(checkpoint['scheduler'])
self.it = checkpoint['it']
try:
self.best_eval_acc = checkpoint['best_eval_acc']
self.best_it = checkpoint['best_it']
except:
print('no best eval acc found')
def interleave_offsets(self, batch, nu):
groups = [batch // (nu + 1)] * (nu + 1)
for x in range(batch - sum(groups)):
groups[-x - 1] += 1
offsets = [0]
for g in groups:
offsets.append(offsets[-1] + g)
assert offsets[-1] == batch
return offsets
def interleave(self, xy, batch):
nu = len(xy) - 1
offsets = self.interleave_offsets(batch, nu)
xy = [[v[offsets[p]:offsets[p + 1]] for p in range(nu + 1)] for v in xy]
for i in range(1, nu + 1):
xy[0][i], xy[i][i] = xy[i][i], xy[0][i]
return [torch.cat(v, dim=0) for v in xy]
def main(args):
"""
For (Distributed)DataParallelism,
main(args) spawn each process (main_worker) to each GPU.
"""
save_path = os.path.join(args.save_dir, args.save_name)
if os.path.exists(save_path) and not args.overwrite:
raise Exception('already existing model: {}'.format(save_path))
if args.resume:
if args.load_path is None:
raise Exception('Resume of training requires --load_path in the args')
if os.path.abspath(save_path) == os.path.abspath(args.load_path) and not args.overwrite:
raise Exception('Saving & Loading pathes are same. \
If you want over-write, give --overwrite in the argument.')
if args.seed is not None:
warnings.warn('You have chosen to seed training. '
'This will turn on the CUDNN deterministic setting, '
'which can slow down your training considerably! '
'You may see unexpected behavior when restarting '
'from checkpoints.')
if args.gpu is not None:
warnings.warn('You have chosen a specific GPU. This will completely '
'disable data parallelism.')
if args.dist_url == "env://" and args.world_size == -1:
args.world_size = int(os.environ["WORLD_SIZE"])
# distributed: true if manually selected or if world_size > 1
args.distributed = (args.world_size > 1 or args.multiprocessing_distributed) and torch.cuda.is_available()
ngpus_per_node = torch.cuda.device_count() if torch.cuda.is_available() else 1
if args.multiprocessing_distributed and torch.cuda.is_available():
# now, args.world_size means num of total processes in all nodes
args.world_size = ngpus_per_node * args.world_size
# args=(,) means the arguments of main_worker
mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, args))
else:
main_worker(args.gpu, ngpus_per_node, args)
def main_worker(gpu, ngpus_per_node, args):
"""
main_worker is conducted on each GPU.
"""
global best_acc1
args.gpu = gpu
# random seed has to be set for the syncronization of labeled data sampling in each process.
assert args.seed is not None
random.seed(args.seed)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
cudnn.deterministic = True
# SET UP FOR DISTRIBUTED TRAINING
if args.distributed:
if args.dist_url == "env://" and args.rank == -1:
args.rank = int(os.environ["RANK"])
if args.multiprocessing_distributed:
args.rank = args.rank * ngpus_per_node + gpu # compute global rank
# set distributed group:
dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
# SET save_path and logger
save_path = os.path.join(args.save_dir, args.save_name)
logger_level = "WARNING"
tb_log = None
if args.rank % ngpus_per_node == 0:
tb_log = TBLog(save_path, 'tensorboard', use_tensorboard=args.use_tensorboard)
logger_level = "INFO"
logger = get_logger(args.save_name, save_path, logger_level)
logger.warning(f"USE GPU: {args.gpu} for training")
# SET FixMatch: class FixMatch in models.fixmatch
args.bn_momentum = 1.0 - 0.999
if 'imagenet' in args.dataset.lower():
_net_builder = net_builder(args.net, False, None, is_remix=False)
else:
_net_builder = net_builder(args.net,
args.net_from_name,
{'first_stride': 2 if 'stl' in args.dataset else 1,
'depth': args.depth,
'widen_factor': args.widen_factor,
'leaky_slope': args.leaky_slope,
'bn_momentum': args.bn_momentum,
'dropRate': args.dropout,
'use_embed': False,
'is_remix': False},
)
model = FixMatch(_net_builder,
args.num_classes,
args.ema_m,
args.T,
args.p_cutoff,
args.ulb_loss_ratio,
args.hard_label,
num_eval_iter=args.num_eval_iter,
tb_log=tb_log,
logger=logger)
logger.info(f'Number of Trainable Params: {count_parameters(model.model)}')
# SET Optimizer & LR Scheduler
# construct SGD and cosine lr scheduler
optimizer = get_optimizer(model.model, args.optim, args.lr, args.momentum, args.weight_decay)
scheduler = get_cosine_schedule_with_warmup(optimizer,
args.num_train_iter,
num_warmup_steps=args.num_train_iter * 0)
# set SGD and cosine lr on FixMatch
model.set_optimizer(optimizer, scheduler)
# SET Devices for (Distributed) DataParallel
if not torch.cuda.is_available():
print('ONLY GPU TRAINING IS SUPPORTED')
elif args.distributed:
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
'''
batch_size: batch_size per node -> batch_size per gpu
workers: workers per node -> workers per gpu
'''
args.batch_size = int(args.batch_size / ngpus_per_node)
model.model.cuda(args.gpu)
model.model = nn.SyncBatchNorm.convert_sync_batchnorm(model.model)
model.model = torch.nn.parallel.DistributedDataParallel(model.model,
device_ids=[args.gpu],
broadcast_buffers=False,
find_unused_parameters=True)
else:
# if arg.gpu is None, DDP will divide and allocate batch_size
# to all available GPUs if device_ids are not set.
model.cuda()
model = torch.nn.parallel.DistributedDataParallel(model)
elif args.gpu is not None:
torch.cuda.set_device(args.gpu)
model.model = model.model.cuda(args.gpu)
else:
model.model = torch.nn.DataParallel(model.model).cuda()
import copy
model.ema_model = copy.deepcopy(model.model)
logger.info(f"model_arch: {model}")
logger.info(f"Arguments: {args}")
if args.rank == 0:
print_args(args)
cudnn.benchmark = True
if args.rank != 0 and args.distributed:
torch.distributed.barrier()
dset_dict, loader_dict = get_dataset_and_loader(args)
# set DataLoader on FixMatch
model.set_data_loader(loader_dict)
model.set_dset(dset_dict['train_ulb'])
# If args.resume, load checkpoints from args.load_path
if os.path.exists(os.path.join(save_path, 'latest_model.pth')):
print('Attempting auto-resume!!')
model.load_model(os.path.join(save_path, 'latest_model.pth'))
# START TRAINING of FixMatch
trainer = model.train
for epoch in range(args.epoch):
trainer(args, logger=logger)
if not args.multiprocessing_distributed or \
(args.multiprocessing_distributed and args.rank % ngpus_per_node == 0):
model.save_model('latest_model.pth', save_path)
logging.warning(f"GPU {args.rank} training is FINISHED")
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='')
'''
Saving & loading of the model.
'''
parser.add_argument('--save_dir', type=str, default='./saved_models')
parser.add_argument('-sn', '--save_name', type=str, default='fixmatch')
parser.add_argument('--resume', action='store_true')
parser.add_argument('--load_path', type=str, default=None)
parser.add_argument('-o', '--overwrite', action='store_true')
parser.add_argument('--use_tensorboard', action='store_true', help='Use tensorboard to plot and save curves, otherwise save the curves locally.')
'''
Training Configuration of FixMatch
'''
parser.add_argument('--epoch', type=int, default=1)
parser.add_argument('--num_train_iter', type=int, default=2 ** 20,
help='total number of training iterations')
parser.add_argument('--num_eval_iter', type=int, default=5000,
help='evaluation frequency')
parser.add_argument('-nl', '--num_labels', type=int, default=40)
parser.add_argument('-bsz', '--batch_size', type=int, default=64)
parser.add_argument('--img_size', type=int, default=112)
parser.add_argument('--num_frames', type=int, default=16)
parser.add_argument('--fps', type=float, default=16.0)
parser.add_argument('--uratio', type=int, default=7,
help='the ratio of unlabeled data to labeld data in each mini-batch')
parser.add_argument('--eval_batch_size', type=int, default=1024,
help='batch size of evaluation data loader (it does not affect the accuracy)')
parser.add_argument('--hard_label', type=str2bool, default=True)
parser.add_argument('--T', type=float, default=0.5)
parser.add_argument('--p_cutoff', type=float, default=0.95)
parser.add_argument('--ema_m', type=float, default=0.999, help='ema momentum for eval_model')
parser.add_argument('--ulb_loss_ratio', type=float, default=1.0)
'''
Optimizer configurations
'''
parser.add_argument('--optim', type=str, default='SGD')
parser.add_argument('--lr', type=float, default=3e-2)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--weight_decay', type=float, default=5e-4)
parser.add_argument('--amp', type=str2bool, default=False, help='use mixed precision training or not')
parser.add_argument('--clip', type=float, default=0)
'''
Backbone Net Configurations
'''
parser.add_argument('--net', type=str, default='WideResNet')
parser.add_argument('--net_from_name', type=str2bool, default=False)
parser.add_argument('--depth', type=int, default=28)
parser.add_argument('--widen_factor', type=int, default=2)
parser.add_argument('--leaky_slope', type=float, default=0.1)
parser.add_argument('--dropout', type=float, default=0.0)
'''
Data Configurations
'''
parser.add_argument('--data_dir', type=str, default='./data')
parser.add_argument('-ds', '--dataset', type=str, default='cifar10')
parser.add_argument('--train_sampler', type=str, default='RandomSampler')
parser.add_argument('-nc', '--num_classes', type=int, default=10)
parser.add_argument('--num_workers', type=int, default=1)
'''
multi-GPUs & Distrbitued Training
'''
parser.add_argument('--world-size', default=1, type=int,
help='number of nodes for distributed training')
parser.add_argument('--rank', default=0, type=int,
help='**node rank** for distributed training')
parser.add_argument('-du', '--dist-url', default='tcp://127.0.0.1:11111', type=str,
help='url used to set up distributed training')
parser.add_argument('--dist-backend', default='nccl', type=str,
help='distributed backend')
parser.add_argument('--seed', default=1, type=int,
help='seed for initializing training. ')
parser.add_argument('--gpu', default=None, type=int,
help='GPU id to use.')
parser.add_argument('--multiprocessing-distributed', type=str2bool, default=True,
help='Use multi-processing distributed training to launch '
'N processes per node, which has N GPUs. This is the '
'fastest way to use PyTorch for either single node or '
'multi node data parallel training')
# config file
parser.add_argument('--c', type=str, default='')
args = parser.parse_args()
over_write_args_from_file(args, args.c)
main(args)
print("Finish Training. Canceling job...")
os.system('scancel %s' % os.environ["SLURM_ARRAY_JOB_ID"])