forked from osmr/imgclsmob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_gl_mealv2.py
1010 lines (928 loc) · 33.8 KB
/
train_gl_mealv2.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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Script for training model on MXNet/Gluon.
"""
import argparse
import time
import logging
import os
import random
import numpy as np
import mxnet as mx
from mxnet import gluon
from mxnet import autograd as ag
from common.logger_utils import initialize_logging
from common.train_log_param_saver import TrainLogParamSaver
from gluon.lr_scheduler import LRScheduler
from gluon.utils import prepare_mx_context, prepare_model, validate
from gluon.utils import report_accuracy, get_composite_metric, get_metric_name, get_initializer, get_loss
from gluon.metrics.metrics import LossValue
from gluon.dataset_utils import get_dataset_metainfo
from gluon.dataset_utils import get_train_data_source, get_val_data_source
from gluon.dataset_utils import get_batch_fn
from gluon.gluoncv2.models.common import Concurrent
from gluon.distillation import MealDiscriminator, MealAdvLoss
def add_train_cls_parser_arguments(parser):
"""
Create python script parameters (for training/classification specific subpart).
Parameters:
----------
parser : ArgumentParser
ArgumentParser instance.
"""
parser.add_argument(
"--model",
type=str,
required=True,
help="type of model to use. see model_provider for options")
parser.add_argument(
"--teacher-models",
type=str,
help="teacher model names to use. see model_provider for options")
parser.add_argument(
"--use-pretrained",
action="store_true",
help="enable using pretrained model from github repo")
parser.add_argument(
"--dtype",
type=str,
default="float32",
help="data type for training")
parser.add_argument(
'--not-hybridize',
action='store_true',
help='do not hybridize model')
parser.add_argument(
'--not-discriminator',
action='store_true',
help='do not use discriminator')
parser.add_argument(
"--resume",
type=str,
default="",
help="resume from previously saved parameters if not None")
parser.add_argument(
"--resume-state",
type=str,
default="",
help="resume from previously saved optimizer state if not None")
parser.add_argument(
"--initializer",
type=str,
default="MSRAPrelu",
help="initializer name. options are MSRAPrelu, Xavier and Xavier-gaussian-out-2")
parser.add_argument(
"--num-gpus",
type=int,
default=0,
help="number of gpus to use")
parser.add_argument(
"-j",
"--num-data-workers",
dest="num_workers",
default=4,
type=int,
help="number of preprocessing workers")
parser.add_argument(
"--batch-size",
type=int,
default=512,
help="training batch size per device (CPU/GPU)")
parser.add_argument(
"--batch-size-scale",
type=int,
default=1,
help="manual batch-size increasing factor")
parser.add_argument(
"--num-epochs",
type=int,
default=120,
help="number of training epochs")
parser.add_argument(
"--start-epoch",
type=int,
default=1,
help="starting epoch for resuming, default is 1 for new training")
parser.add_argument(
"--attempt",
type=int,
default=1,
help="current attempt number for training")
parser.add_argument(
"--optimizer-name",
type=str,
default="nag",
help="optimizer name")
parser.add_argument(
"--lr",
type=float,
default=0.1,
help="learning rate")
parser.add_argument(
"--dlr-factor",
type=float,
default=1.0,
help="discriminator learning rate factor")
parser.add_argument(
"--lr-mode",
type=str,
default="cosine",
help="learning rate scheduler mode. options are step, poly and cosine")
parser.add_argument(
"--lr-decay",
type=float,
default=0.1,
help="decay rate of learning rate")
parser.add_argument(
"--lr-decay-period",
type=int,
default=0,
help="interval for periodic learning rate decays. default is 0 to disable")
parser.add_argument(
"--lr-decay-epoch",
type=str,
default="40,60",
help="epoches at which learning rate decays")
parser.add_argument(
"--target-lr",
type=float,
default=1e-8,
help="ending learning rate")
parser.add_argument(
"--poly-power",
type=float,
default=2,
help="power value for poly LR scheduler")
parser.add_argument(
"--warmup-epochs",
type=int,
default=0,
help="number of warmup epochs")
parser.add_argument(
"--warmup-lr",
type=float,
default=1e-8,
help="starting warmup learning rate")
parser.add_argument(
"--warmup-mode",
type=str,
default="linear",
help="learning rate scheduler warmup mode. options are linear, poly and constant")
parser.add_argument(
"--momentum",
type=float,
default=0.9,
help="momentum value for optimizer")
parser.add_argument(
"--wd",
type=float,
default=0.0001,
help="weight decay rate")
parser.add_argument(
"--gamma-wd-mult",
type=float,
default=1.0,
help="weight decay multiplier for batchnorm gamma")
parser.add_argument(
"--beta-wd-mult",
type=float,
default=1.0,
help="weight decay multiplier for batchnorm beta")
parser.add_argument(
"--bias-wd-mult",
type=float,
default=1.0,
help="weight decay multiplier for bias")
parser.add_argument(
"--grad-clip",
type=float,
default=None,
help="max_norm for gradient clipping")
parser.add_argument(
"--label-smoothing",
action="store_true",
help="use label smoothing")
parser.add_argument(
"--mixup",
action="store_true",
help="use mixup strategy")
parser.add_argument(
"--mixup-epoch-tail",
type=int,
default=12,
help="number of epochs without mixup at the end of training")
parser.add_argument(
"--log-interval",
type=int,
default=50,
help="number of batches to wait before logging")
parser.add_argument(
"--save-interval",
type=int,
default=4,
help="saving parameters epoch interval, best model will always be saved")
parser.add_argument(
"--save-dir",
type=str,
default="",
help="directory of saved models and log-files")
parser.add_argument(
"--logging-file-name",
type=str,
default="train.log",
help="filename of training log")
parser.add_argument(
"--seed",
type=int,
default=-1,
help="random seed to be fixed")
parser.add_argument(
"--log-packages",
type=str,
default="mxnet, numpy",
help="list of python packages for logging")
parser.add_argument(
"--log-pip-packages",
type=str,
default="mxnet-cu110, mxnet-cu112",
help="list of pip packages for logging")
parser.add_argument(
"--tune-layers",
type=str,
default="",
help="regexp for selecting layers for fine tuning")
def parse_args():
"""
Parse python script parameters (common part).
Returns:
-------
ArgumentParser
Resulted args.
"""
parser = argparse.ArgumentParser(
description="Train a model for image classification (Gluon)",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--dataset",
type=str,
default="ImageNet1K_rec",
help="dataset name. options are ImageNet1K, ImageNet1K_rec, CUB200_2011, CIFAR10, CIFAR100, SVHN")
parser.add_argument(
"--work-dir",
type=str,
default=os.path.join("..", "imgclsmob_data"),
help="path to working directory only for dataset root path preset")
args, _ = parser.parse_known_args()
dataset_metainfo = get_dataset_metainfo(dataset_name=args.dataset)
dataset_metainfo.add_dataset_parser_arguments(
parser=parser,
work_dir_path=args.work_dir)
add_train_cls_parser_arguments(parser)
args = parser.parse_args()
return args
def init_rand(seed):
"""
Initialize all random generators by seed.
Parameters:
----------
seed : int
Seed value.
Returns:
-------
int
Generated seed value.
"""
if seed <= 0:
seed = np.random.randint(10000)
random.seed(seed)
np.random.seed(seed)
mx.random.seed(seed)
return seed
def prepare_trainer(net,
optimizer_name,
wd,
momentum,
lr_mode,
lr,
lr_decay_period,
lr_decay_epoch,
lr_decay,
target_lr,
poly_power,
warmup_epochs,
warmup_lr,
warmup_mode,
batch_size,
num_epochs,
num_training_samples,
dtype,
gamma_wd_mult=1.0,
beta_wd_mult=1.0,
bias_wd_mult=1.0,
state_file_path=None):
"""
Prepare trainer.
Parameters:
----------
net : HybridBlock
Model.
optimizer_name : str
Name of optimizer.
wd : float
Weight decay rate.
momentum : float
Momentum value.
lr_mode : str
Learning rate scheduler mode.
lr : float
Learning rate.
lr_decay_period : int
Interval for periodic learning rate decays.
lr_decay_epoch : str
Epoches at which learning rate decays.
lr_decay : float
Decay rate of learning rate.
target_lr : float
Final learning rate.
poly_power : float
Power value for poly LR scheduler.
warmup_epochs : int
Number of warmup epochs.
warmup_lr : float
Starting warmup learning rate.
warmup_mode : str
Learning rate scheduler warmup mode.
batch_size : int
Training batch size.
num_epochs : int
Number of training epochs.
num_training_samples : int
Number of training samples in dataset.
dtype : str
Base data type for tensors.
gamma_wd_mult : float
Weight decay multiplier for batchnorm gamma.
beta_wd_mult : float
Weight decay multiplier for batchnorm beta.
bias_wd_mult : float
Weight decay multiplier for bias.
state_file_path : str, default None
Path for file with trainer state.
Returns:
-------
Trainer
Trainer.
LRScheduler
Learning rate scheduler.
"""
if gamma_wd_mult != 1.0:
for k, v in net.collect_params(".*gamma").items():
v.wd_mult = gamma_wd_mult
if beta_wd_mult != 1.0:
for k, v in net.collect_params(".*beta").items():
v.wd_mult = beta_wd_mult
if bias_wd_mult != 1.0:
for k, v in net.collect_params(".*bias").items():
v.wd_mult = bias_wd_mult
if lr_decay_period > 0:
lr_decay_epoch = list(range(lr_decay_period, num_epochs, lr_decay_period))
else:
lr_decay_epoch = [int(i) for i in lr_decay_epoch.split(",")]
num_batches = num_training_samples // batch_size
lr_scheduler = LRScheduler(
mode=lr_mode,
base_lr=lr,
n_iters=num_batches,
n_epochs=num_epochs,
step=lr_decay_epoch,
step_factor=lr_decay,
target_lr=target_lr,
power=poly_power,
warmup_epochs=warmup_epochs,
warmup_lr=warmup_lr,
warmup_mode=warmup_mode)
optimizer_params = {"learning_rate": lr,
"wd": wd,
"momentum": momentum,
"lr_scheduler": lr_scheduler}
if dtype != "float32":
optimizer_params["multi_precision"] = True
trainer = gluon.Trainer(
params=net.collect_params(),
optimizer=optimizer_name,
optimizer_params=optimizer_params)
if (state_file_path is not None) and state_file_path and os.path.exists(state_file_path):
logging.info("Loading trainer states: {}".format(state_file_path))
trainer.load_states(state_file_path)
if trainer._optimizer.wd != wd:
trainer._optimizer.wd = wd
logging.info("Reset the weight decay: {}".format(wd))
# lr_scheduler = trainer._optimizer.lr_scheduler
trainer._optimizer.lr_scheduler = lr_scheduler
return trainer, lr_scheduler
def save_params(file_stem,
net,
trainer):
"""
Save current model/trainer parameters.
Parameters:
----------
file_stem : str
File stem (with path).
net : HybridBlock
Model.
trainer : Trainer
Trainer.
"""
net.save_parameters(file_stem + ".params")
trainer.save_states(file_stem + ".states")
def train_epoch(epoch,
net,
teacher_net,
discrim_net,
train_metric,
loss_metrics,
train_data,
batch_fn,
data_source_needs_reset,
dtype,
ctx,
loss_func,
discrim_loss_func,
trainer,
lr_scheduler,
batch_size,
log_interval,
mixup,
mixup_epoch_tail,
label_smoothing,
num_classes,
num_epochs,
grad_clip_value,
batch_size_scale):
"""
Train model on particular epoch.
Parameters:
----------
epoch : int
Epoch number.
net : HybridBlock
Model.
teacher_net : HybridBlock or None
Teacher model.
discrim_net : HybridBlock or None
MEALv2 discriminator model.
train_metric : EvalMetric
Metric object instance.
loss_metric : list of EvalMetric
Metric object instances (loss values).
train_data : DataLoader or ImageRecordIter
Data loader or ImRec-iterator.
batch_fn : func
Function for splitting data after extraction from data loader.
data_source_needs_reset : bool
Whether to reset data (if test_data is ImageRecordIter).
dtype : str
Base data type for tensors.
ctx : Context
MXNet context.
loss_func : Loss
Loss function.
discrim_loss_func : Loss or None
MEALv2 adversarial loss function.
trainer : Trainer
Trainer.
lr_scheduler : LRScheduler
Learning rate scheduler.
batch_size : int
Training batch size.
log_interval : int
Batch count period for logging.
mixup : bool
Whether to use mixup.
mixup_epoch_tail : int
Number of epochs without mixup at the end of training.
label_smoothing : bool
Whether to use label-smoothing.
num_classes : int
Number of model classes.
num_epochs : int
Number of training epochs.
grad_clip_value : float
Threshold for gradient clipping.
batch_size_scale : int
Manual batch-size increasing factor.
Returns:
-------
float
Loss value.
"""
labels_list_inds = None
batch_size_extend_count = 0
tic = time.time()
if data_source_needs_reset:
train_data.reset()
train_metric.reset()
for m in loss_metrics:
m.reset()
i = 0
btic = time.time()
for i, batch in enumerate(train_data):
data_list, labels_list = batch_fn(batch, ctx)
labels_one_hot = False
if teacher_net is not None:
labels_list = [teacher_net(x.astype(dtype, copy=False)).softmax(axis=-1).mean(axis=1) for x in data_list]
labels_list_inds = [y.argmax(axis=-1) for y in labels_list]
labels_one_hot = True
if label_smoothing and not (teacher_net is not None):
eta = 0.1
on_value = 1 - eta + eta / num_classes
off_value = eta / num_classes
if not labels_one_hot:
labels_list_inds = labels_list
labels_list = [y.one_hot(depth=num_classes, on_value=on_value, off_value=off_value)
for y in labels_list]
labels_one_hot = True
if mixup:
if not labels_one_hot:
labels_list_inds = labels_list
labels_list = [y.one_hot(depth=num_classes) for y in labels_list]
labels_one_hot = True
if epoch < num_epochs - mixup_epoch_tail:
alpha = 1
lam = np.random.beta(alpha, alpha)
data_list = [lam * x + (1 - lam) * x[::-1] for x in data_list]
labels_list = [lam * y + (1 - lam) * y[::-1] for y in labels_list]
with ag.record():
outputs_list = [net(x.astype(dtype, copy=False)) for x in data_list]
loss_list = [loss_func(yhat, y.astype(dtype, copy=False)) for yhat, y in zip(outputs_list, labels_list)]
if discrim_net is not None:
d_pred_list = [discrim_net(yhat.astype(dtype, copy=False).softmax()) for yhat in outputs_list]
d_label_list = [discrim_net(y.astype(dtype, copy=False)) for y in labels_list]
d_loss_list = [discrim_loss_func(yhat, y) for yhat, y in zip(d_pred_list, d_label_list)]
loss_list = [z + dz for z, dz in zip(loss_list, d_loss_list)]
for loss in loss_list:
loss.backward()
lr_scheduler.update(i, epoch)
if grad_clip_value is not None:
grads = [v.grad(ctx[0]) for v in net.collect_params().values() if v._grad is not None]
gluon.utils.clip_global_norm(grads, max_norm=grad_clip_value)
if batch_size_scale == 1:
trainer.step(batch_size)
else:
if (i + 1) % batch_size_scale == 0:
batch_size_extend_count = 0
trainer.step(batch_size * batch_size_scale)
for p in net.collect_params().values():
p.zero_grad()
else:
batch_size_extend_count += 1
train_metric.update(
labels=(labels_list if not labels_one_hot else labels_list_inds),
preds=outputs_list)
loss_metrics[0].update(labels=None, preds=loss_list)
if (discrim_net is not None) and (len(loss_metrics) > 1):
loss_metrics[1].update(labels=None, preds=d_loss_list)
if log_interval and not (i + 1) % log_interval:
speed = batch_size * log_interval / (time.time() - btic)
btic = time.time()
train_accuracy_msg = report_accuracy(metric=train_metric)
loss_accuracy_msg = report_accuracy(metric=loss_metrics[0])
if (discrim_net is not None) and (len(loss_metrics) > 1):
dloss_accuracy_msg = report_accuracy(metric=loss_metrics[1])
logging.info("Epoch[{}] Batch [{}]\tSpeed: {:.2f} samples/sec\t{}\t{}\t{}\tlr={:.5f}".format(
epoch + 1, i, speed, train_accuracy_msg, loss_accuracy_msg, dloss_accuracy_msg,
trainer.learning_rate))
else:
logging.info("Epoch[{}] Batch [{}]\tSpeed: {:.2f} samples/sec\t{}\t{}\tlr={:.5f}".format(
epoch + 1, i, speed, train_accuracy_msg, loss_accuracy_msg, trainer.learning_rate))
if (batch_size_scale != 1) and (batch_size_extend_count > 0):
trainer.step(batch_size * batch_size_extend_count)
for p in net.collect_params().values():
p.zero_grad()
throughput = int(batch_size * (i + 1) / (time.time() - tic))
logging.info("[Epoch {}] speed: {:.2f} samples/sec\ttime cost: {:.2f} sec".format(
epoch + 1, throughput, time.time() - tic))
train_accuracy_msg = report_accuracy(metric=train_metric)
loss_accuracy_msg = report_accuracy(metric=loss_metrics[0])
if (discrim_net is not None) and (len(loss_metrics) > 1):
dloss_accuracy_msg = report_accuracy(metric=loss_metrics[1])
logging.info("[Epoch {}] training: {}\t{}\t{}".format(epoch + 1, train_accuracy_msg, loss_accuracy_msg,
dloss_accuracy_msg))
else:
logging.info("[Epoch {}] training: {}\t{}".format(epoch + 1, train_accuracy_msg, loss_accuracy_msg))
def train_net(batch_size,
num_epochs,
start_epoch1,
train_data,
val_data,
batch_fn,
data_source_needs_reset,
dtype,
net,
teacher_net,
discrim_net,
trainer,
lr_scheduler,
lp_saver,
log_interval,
mixup,
mixup_epoch_tail,
label_smoothing,
num_classes,
grad_clip_value,
batch_size_scale,
val_metric,
train_metric,
loss_metrics,
loss_func,
discrim_loss_func,
ctx):
"""
Main procedure for training model.
Parameters:
----------
batch_size : int
Training batch size.
num_epochs : int
Number of training epochs.
start_epoch1 : int
Number of starting epoch (1-based).
train_data : DataLoader or ImageRecordIter
Data loader or ImRec-iterator (training subset).
val_data : DataLoader or ImageRecordIter
Data loader or ImRec-iterator (validation subset).
batch_fn : func
Function for splitting data after extraction from data loader.
data_source_needs_reset : bool
Whether to reset data (if test_data is ImageRecordIter).
dtype : str
Base data type for tensors.
net : HybridBlock
Model.
teacher_net : HybridBlock or None
Teacher model.
discrim_net : HybridBlock or None
MEALv2 discriminator model.
trainer : Trainer
Trainer.
lr_scheduler : LRScheduler
Learning rate scheduler.
lp_saver : TrainLogParamSaver
Model/trainer state saver.
log_interval : int
Batch count period for logging.
mixup : bool
Whether to use mixup.
mixup_epoch_tail : int
Number of epochs without mixup at the end of training.
label_smoothing : bool
Whether to use label-smoothing.
num_classes : int
Number of model classes.
grad_clip_value : float
Threshold for gradient clipping.
batch_size_scale : int
Manual batch-size increasing factor.
val_metric : EvalMetric
Metric object instance (validation subset).
train_metric : EvalMetric
Metric object instance (training subset).
loss_metrics : list of EvalMetric
Metric object instances (loss values).
loss_func : Loss
Loss object instance.
discrim_loss_func : Loss or None
MEALv2 adversarial loss function.
ctx : Context
MXNet context.
"""
if batch_size_scale != 1:
for p in net.collect_params().values():
p.grad_req = "add"
if isinstance(ctx, mx.Context):
ctx = [ctx]
# loss_func = gluon.loss.SoftmaxCrossEntropyLoss(sparse_label=(not (mixup or label_smoothing)))
assert (type(start_epoch1) == int)
assert (start_epoch1 >= 1)
if start_epoch1 > 1:
logging.info("Start training from [Epoch {}]".format(start_epoch1))
validate(
metric=val_metric,
net=net,
val_data=val_data,
batch_fn=batch_fn,
data_source_needs_reset=data_source_needs_reset,
dtype=dtype,
ctx=ctx)
val_accuracy_msg = report_accuracy(metric=val_metric)
logging.info("[Epoch {}] validation: {}".format(start_epoch1 - 1, val_accuracy_msg))
gtic = time.time()
for epoch in range(start_epoch1 - 1, num_epochs):
train_epoch(
epoch=epoch,
net=net,
teacher_net=teacher_net,
discrim_net=discrim_net,
train_metric=train_metric,
loss_metrics=loss_metrics,
train_data=train_data,
batch_fn=batch_fn,
data_source_needs_reset=data_source_needs_reset,
dtype=dtype,
ctx=ctx,
loss_func=loss_func,
discrim_loss_func=discrim_loss_func,
trainer=trainer,
lr_scheduler=lr_scheduler,
batch_size=batch_size,
log_interval=log_interval,
mixup=mixup,
mixup_epoch_tail=mixup_epoch_tail,
label_smoothing=label_smoothing,
num_classes=num_classes,
num_epochs=num_epochs,
grad_clip_value=grad_clip_value,
batch_size_scale=batch_size_scale)
validate(
metric=val_metric,
net=net,
val_data=val_data,
batch_fn=batch_fn,
data_source_needs_reset=data_source_needs_reset,
dtype=dtype,
ctx=ctx)
val_accuracy_msg = report_accuracy(metric=val_metric)
logging.info("[Epoch {}] validation: {}".format(epoch + 1, val_accuracy_msg))
if lp_saver is not None:
lp_saver_kwargs = {"net": net, "trainer": trainer}
val_acc_values = val_metric.get()[1]
train_acc_values = train_metric.get()[1]
val_acc_values = val_acc_values if type(val_acc_values) == list else [val_acc_values]
train_acc_values = train_acc_values if type(train_acc_values) == list else [train_acc_values]
lp_saver.epoch_test_end_callback(
epoch1=(epoch + 1),
params=(val_acc_values + train_acc_values + [loss_metrics[0].get()[1], trainer.learning_rate]),
**lp_saver_kwargs)
logging.info("Total time cost: {:.2f} sec".format(time.time() - gtic))
if lp_saver is not None:
opt_metric_name = get_metric_name(val_metric, lp_saver.acc_ind)
logging.info("Best {}: {:.4f} at {} epoch".format(
opt_metric_name, lp_saver.best_eval_metric_value, lp_saver.best_eval_metric_epoch))
def main():
"""
Main body of script.
"""
args = parse_args()
args.seed = init_rand(seed=args.seed)
_, log_file_exist = initialize_logging(
logging_dir_path=args.save_dir,
logging_file_name=args.logging_file_name,
script_args=args,
log_packages=args.log_packages,
log_pip_packages=args.log_pip_packages)
ctx, batch_size = prepare_mx_context(
num_gpus=args.num_gpus,
batch_size=args.batch_size)
ds_metainfo = get_dataset_metainfo(dataset_name=args.dataset)
ds_metainfo.update(args=args)
use_teacher = (args.teacher_models is not None) and (args.teacher_models.strip() != "")
net = prepare_model(
model_name=args.model,
use_pretrained=args.use_pretrained,
pretrained_model_file_path=args.resume.strip(),
dtype=args.dtype,
net_extra_kwargs=ds_metainfo.train_net_extra_kwargs,
tune_layers=args.tune_layers,
classes=args.num_classes,
in_channels=args.in_channels,
do_hybridize=(not args.not_hybridize),
initializer=get_initializer(initializer_name=args.initializer),
ctx=ctx)
assert (hasattr(net, "classes"))
num_classes = net.classes
teacher_net = None
discrim_net = None
discrim_loss_func = None
if use_teacher:
teacher_nets = []
for teacher_model in args.teacher_models.split(","):
teacher_net = prepare_model(
model_name=teacher_model.strip(),
use_pretrained=True,
pretrained_model_file_path="",
dtype=args.dtype,
net_extra_kwargs=ds_metainfo.train_net_extra_kwargs,
do_hybridize=(not args.not_hybridize),
ctx=ctx)
assert (teacher_net.classes == net.classes)
assert (teacher_net.in_size == net.in_size)
teacher_nets.append(teacher_net)
if len(teacher_nets) > 0:
teacher_net = Concurrent(stack=True, prefix="", branches=teacher_nets)
for k, v in teacher_net.collect_params().items():
v.grad_req = "null"
if not args.not_discriminator:
discrim_net = MealDiscriminator()
discrim_net.cast(args.dtype)
if not args.not_hybridize:
discrim_net.hybridize(
static_alloc=True,
static_shape=True)
discrim_net.initialize(mx.init.MSRAPrelu(), ctx=ctx)
for k, v in discrim_net.collect_params().items():
v.lr_mult = args.dlr_factor
discrim_loss_func = MealAdvLoss()
train_data = get_train_data_source(
ds_metainfo=ds_metainfo,
batch_size=batch_size,
num_workers=args.num_workers)
val_data = get_val_data_source(
ds_metainfo=ds_metainfo,
batch_size=batch_size,
num_workers=args.num_workers)
batch_fn = get_batch_fn(ds_metainfo=ds_metainfo)
num_training_samples = len(train_data._dataset) if not ds_metainfo.use_imgrec else ds_metainfo.num_training_samples
trainer, lr_scheduler = prepare_trainer(
net=net,
optimizer_name=args.optimizer_name,
wd=args.wd,
momentum=args.momentum,
lr_mode=args.lr_mode,
lr=args.lr,
lr_decay_period=args.lr_decay_period,
lr_decay_epoch=args.lr_decay_epoch,
lr_decay=args.lr_decay,
target_lr=args.target_lr,
poly_power=args.poly_power,
warmup_epochs=args.warmup_epochs,
warmup_lr=args.warmup_lr,
warmup_mode=args.warmup_mode,
batch_size=batch_size,
num_epochs=args.num_epochs,
num_training_samples=num_training_samples,
dtype=args.dtype,
gamma_wd_mult=args.gamma_wd_mult,
beta_wd_mult=args.beta_wd_mult,
bias_wd_mult=args.bias_wd_mult,
state_file_path=args.resume_state)
if args.save_dir and args.save_interval:
param_names = ds_metainfo.val_metric_capts + ds_metainfo.train_metric_capts + ["Train.Loss", "LR"]
lp_saver = TrainLogParamSaver(
checkpoint_file_name_prefix="{}_{}".format(ds_metainfo.short_label, args.model),
last_checkpoint_file_name_suffix="last",
best_checkpoint_file_name_suffix=None,
last_checkpoint_dir_path=args.save_dir,
best_checkpoint_dir_path=None,
last_checkpoint_file_count=2,
best_checkpoint_file_count=2,
checkpoint_file_save_callback=save_params,
checkpoint_file_exts=(".params", ".states"),
save_interval=args.save_interval,
num_epochs=args.num_epochs,
param_names=param_names,
acc_ind=ds_metainfo.saver_acc_ind,
# bigger=[True],
# mask=None,
score_log_file_path=os.path.join(args.save_dir, "score.log"),
score_log_attempt_value=args.attempt,
best_map_log_file_path=os.path.join(args.save_dir, "best_map.log"))
else:
lp_saver = None
val_metric = get_composite_metric(ds_metainfo.val_metric_names, ds_metainfo.val_metric_extra_kwargs)
train_metric = get_composite_metric(ds_metainfo.train_metric_names, ds_metainfo.train_metric_extra_kwargs)
loss_metrics = [LossValue(name="loss"), LossValue(name="dloss")]
loss_kwargs = {"sparse_label": (not (args.mixup or args.label_smoothing) and
not (use_teacher and (teacher_net is not None)))}
if ds_metainfo.loss_extra_kwargs is not None:
loss_kwargs.update(ds_metainfo.loss_extra_kwargs)
loss_func = get_loss(ds_metainfo.loss_name, loss_kwargs)
train_net(
batch_size=batch_size,
num_epochs=args.num_epochs,
start_epoch1=args.start_epoch,
train_data=train_data,
val_data=val_data,
batch_fn=batch_fn,
data_source_needs_reset=ds_metainfo.use_imgrec,
dtype=args.dtype,
net=net,
teacher_net=teacher_net,
discrim_net=discrim_net,
trainer=trainer,
lr_scheduler=lr_scheduler,
lp_saver=lp_saver,
log_interval=args.log_interval,
mixup=args.mixup,
mixup_epoch_tail=args.mixup_epoch_tail,
label_smoothing=args.label_smoothing,
num_classes=num_classes,
grad_clip_value=args.grad_clip,
batch_size_scale=args.batch_size_scale,