forked from fversaci/cassandra-dali-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdistrib_train_from_cassandra.py
More file actions
854 lines (739 loc) · 25.9 KB
/
distrib_train_from_cassandra.py
File metadata and controls
854 lines (739 loc) · 25.9 KB
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
# Adapted to use cassandra-dali-plugin, from
# https://github.com/NVIDIA/DALI/blob/main/docs/examples/use_cases/pytorch/resnet50/main.py
# (Apache License, Version 2.0)
# cassandra reader
from cassandra_reader import get_cassandra_reader
from crs4.cassandra_utils import get_shard
import argparse
import os
import shutil
import time
import math
import pickle
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.models as models
import numpy as np
try:
from nvidia.dali.plugin.pytorch import DALIClassificationIterator, LastBatchPolicy
from nvidia.dali.pipeline import pipeline_def
import nvidia.dali.types as types
import nvidia.dali.fn as fn
except ImportError:
raise ImportError(
"Please install DALI from https://www.github.com/NVIDIA/DALI to run this example."
)
# supporting torchrun
global_rank = int(os.getenv("RANK", default=0))
local_rank = int(os.getenv("LOCAL_RANK", default=0))
world_size = int(os.getenv("WORLD_SIZE", default=1))
def parse():
model_names = sorted(
name
for name in models.__dict__
if name.islower()
and not name.startswith("__")
and callable(models.__dict__[name])
)
parser = argparse.ArgumentParser(description="PyTorch ImageNet Training")
parser.add_argument(
"--split-fn",
metavar="FILENAME",
required=True,
help="split file filename",
)
parser.add_argument(
"--train-index",
metavar="TINDEX",
default=0,
type=int,
help="Index of the split array in the splitfile to be used for training",
)
parser.add_argument(
"--val-index",
metavar="VINDEX",
default=1,
type=int,
help="Index of the split array in the splitfile to be used for validation",
)
parser.add_argument(
"--crossval-index",
metavar="CVINDEX",
default=None,
type=int,
help="Index of the split array in the splitfile to be used for validation.\
The remaining splits, except the one specified with the --exclude-split option,\
are merged and used as training data.\
The --train-index and --val-index options will be overridden",
)
parser.add_argument(
"--exclude-index",
metavar="EINDEX",
default=None,
type=int,
help="Index of the split to be excluded during the crossvalidation process.",
)
parser.add_argument(
"--arch",
"-a",
metavar="ARCH",
default="resnet18",
choices=model_names,
help="model architecture: " + " | ".join(model_names) + " (default: resnet18)",
)
parser.add_argument(
"-j",
"--workers",
default=4,
type=int,
metavar="N",
help="number of data loading workers (default: 4)",
)
parser.add_argument(
"--epochs",
default=90,
type=int,
metavar="N",
help="number of total epochs to run",
)
parser.add_argument(
"--start-epoch",
default=0,
type=int,
metavar="N",
help="manual epoch number (useful on restarts)",
)
parser.add_argument(
"-b",
"--batch-size",
default=256,
type=int,
metavar="N",
help="mini-batch size per process (default: 256)",
)
parser.add_argument(
"--lr",
"--learning-rate",
default=0.1,
type=float,
metavar="LR",
help="Initial learning rate. Will be scaled by <global batch size>/256: args.lr = args.lr*float(args.batch_size*world_size)/256. A warmup schedule will also be applied over the first 5 epochs.",
)
parser.add_argument(
"--momentum", default=0.9, type=float, metavar="M", help="momentum"
)
parser.add_argument(
"--weight-decay",
"--wd",
default=1e-4,
type=float,
metavar="W",
help="weight decay (default: 1e-4)",
)
parser.add_argument(
"--print-freq",
"-p",
default=10,
type=int,
metavar="N",
help="print frequency (default: 10)",
)
parser.add_argument(
"--resume",
default="",
type=str,
metavar="PATH",
help="path to latest checkpoint (default: none)",
)
parser.add_argument(
"-e",
"--evaluate",
dest="evaluate",
action="store_true",
help="evaluate model on validation set",
)
parser.add_argument(
"--pretrained",
dest="pretrained",
action="store_true",
help="use pre-trained model",
)
parser.add_argument(
"--dali_cpu",
action="store_true",
help="Runs CPU based version of DALI pipeline.",
)
parser.add_argument(
"--prof", default=-1, type=int, help="Only run 10 iterations for profiling."
)
parser.add_argument("--deterministic", action="store_true")
parser.add_argument("--sync_bn", action="store_true", help="enabling apex sync BN.")
parser.add_argument("--opt-level", type=str, default=None)
parser.add_argument("--keep-batchnorm-fp32", type=str, default=None)
parser.add_argument("--loss-scale", type=str, default=None)
parser.add_argument("--channels-last", type=bool, default=False)
parser.add_argument(
"-t",
"--test",
action="store_true",
help="Launch test mode with preset arguments",
)
args = parser.parse_args()
return args
# item() is a recent addition, so this helps with backward compatibility.
def to_python_float(t):
if hasattr(t, "item"):
return t.item()
else:
return t[0]
@pipeline_def
def create_dali_pipeline(
data_table,
id_col,
label_type,
label_col,
data_col,
crop,
size,
source_uuids,
dali_cpu=False,
is_training=True,
prefetch_buffers=8,
shard_id=0,
num_shards=1,
io_threads=1,
comm_threads=2,
copy_threads=2,
wait_threads=2,
):
cass_reader = get_cassandra_reader(
data_table=data_table,
id_col=id_col,
label_type=label_type,
label_col=label_col,
data_col=data_col,
prefetch_buffers=prefetch_buffers,
shard_id=shard_id,
num_shards=num_shards,
source_uuids=source_uuids,
io_threads=io_threads,
comm_threads=comm_threads,
copy_threads=copy_threads,
wait_threads=wait_threads,
)
images, labels = cass_reader
dali_device = "cpu" if dali_cpu else "gpu"
decoder_device = "cpu" if dali_cpu else "mixed"
# ask HW NVJPEG to allocate memory ahead for the biggest image in the data set to avoid reallocations in runtime
preallocate_width_hint = 5980 if decoder_device == "mixed" else 0
preallocate_height_hint = 6430 if decoder_device == "mixed" else 0
if is_training:
images = fn.decoders.image_random_crop(
images,
device=decoder_device,
output_type=types.RGB,
preallocate_width_hint=preallocate_width_hint,
preallocate_height_hint=preallocate_height_hint,
random_aspect_ratio=[0.8, 1.25],
random_area=[0.1, 1.0],
num_attempts=100,
)
images = fn.resize(
images,
device=dali_device,
resize_x=crop,
resize_y=crop,
interp_type=types.INTERP_TRIANGULAR,
)
mirror = fn.random.coin_flip(probability=0.5)
else:
images = fn.decoders.image(images, device=decoder_device, output_type=types.RGB)
images = fn.resize(
images,
device=dali_device,
size=size,
mode="not_smaller",
interp_type=types.INTERP_TRIANGULAR,
)
mirror = False
images = fn.crop_mirror_normalize(
images.gpu(),
dtype=types.FLOAT,
output_layout="CHW",
crop=(crop, crop),
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
mirror=mirror,
)
labels = labels.gpu()
return (images, labels)
def read_split_file(split_fn):
data = pickle.load(open(split_fn, "rb"))
data_table = data["data_table"]
id_col = data["data_id_col"]
label_col = data["data_label_col"] # Name of the table column with the outcome label
data_col = data["data_col"] # Name of the table column with actual data
label_type = data["label_type"]
row_keys = data["row_keys"] # Numpy array of UUIDs
split = data[
"split"
] # List of arrays. Each arrays indexes the row_keys array for each split.
num_classes = data["num_classes"]
return (
data_table,
id_col,
data_col,
label_type,
label_col,
row_keys,
split,
num_classes,
)
def compute_split_index(split, train_index, val_index, crossval_index, exclude_index):
n_split = len(split)
# Merge splits for training samples if crossvalidation is requested.
# Do nothing otherwise
if crossval_index and n_split > 2:
if exclude_index > n_split:
exlcude_index = n_split - 1
if crossval_index > n_split or crossval_index == exclude_index:
crossval_index = n_split - 2
tis = np.array(
[i for i in range(n_split) if i != exclude_index and i != val_index]
)
train_split = np.concatenate([split[i] for i in tis])
val_split = split[val_index]
split = [train_split, val_split]
train_index = 0
val_index = 1
print("\nCrossvalidation:")
print(f"Training samples will be taken from splits {tis}")
print(f"Validation samples will be taken from split {crossval_index}")
if exclude_index:
print(f"Split {exclude_index} will not be used")
print("\n")
return split, train_index, val_index
def main():
global best_prec1, args
best_prec1 = 0
args = parse()
## Read split file to get data for training
(
data_table,
id_col,
data_col,
label_type,
label_col,
row_keys,
split,
num_classes,
) = read_split_file(args.split_fn)
# Get split indexes
split, train_index, val_index = compute_split_index(
split, args.train_index, args.val_index, args.crossval_index, args.exclude_index
)
# test mode, use default args for sanity test
if args.test:
args.opt_level = None
args.epochs = 1
args.start_epoch = 0
args.arch = "resnet50"
args.batch_size = 64
args.sync_bn = False
print("Test mode - no DDP, no apex, RN50, 10 iterations")
args.distributed = world_size > 1
# make apex optional
if args.opt_level is not None or args.distributed or args.sync_bn:
try:
global DDP, amp, optimizers, parallel
from apex.parallel import DistributedDataParallel as DDP
from apex import amp, optimizers, parallel
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex to run this example."
)
print("opt_level = {}".format(args.opt_level))
print(
"keep_batchnorm_fp32 = {}".format(args.keep_batchnorm_fp32),
type(args.keep_batchnorm_fp32),
)
print("loss_scale = {}".format(args.loss_scale), type(args.loss_scale))
print("\nCUDNN VERSION: {}\n".format(torch.backends.cudnn.version()))
cudnn.benchmark = True
best_prec1 = 0
if args.deterministic:
cudnn.benchmark = False
cudnn.deterministic = True
torch.manual_seed(1234)
torch.set_printoptions(precision=10)
args.gpu = 0
if args.distributed:
args.gpu = local_rank
torch.cuda.set_device(args.gpu)
torch.distributed.init_process_group(backend="nccl", init_method="env://")
args.total_batch_size = world_size * args.batch_size
assert torch.backends.cudnn.enabled, "Amp requires cudnn backend to be enabled."
# create model
if args.pretrained:
print("=> using pre-trained model '{}'".format(args.arch))
model = models.__dict__[args.arch](pretrained=True)
else:
print("=> creating model '{}'".format(args.arch))
model = models.__dict__[args.arch]()
if args.sync_bn:
print("using apex synced BN")
model = parallel.convert_syncbn_model(model)
if hasattr(torch, "channels_last") and hasattr(torch, "contiguous_format"):
if args.channels_last:
memory_format = torch.channels_last
else:
memory_format = torch.contiguous_format
model = model.cuda().to(memory_format=memory_format)
else:
model = model.cuda()
# Scale learning rate based on global batch size
args.lr = args.lr * float(args.batch_size * world_size) / 256.0
optimizer = torch.optim.SGD(
model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
# Initialize Amp. Amp accepts either values or strings for the
# optional override arguments, for convenient interoperation with
# argparse.
if args.opt_level is not None:
model, optimizer = amp.initialize(
model,
optimizer,
opt_level=args.opt_level,
keep_batchnorm_fp32=args.keep_batchnorm_fp32,
loss_scale=args.loss_scale,
)
# For distributed training, wrap the model with
# apex.parallel.DistributedDataParallel. This must be done AFTER
# the call to amp.initialize. If model = DDP(model) is called
# before model, ... = amp.initialize(model, ...), the call to
# amp.initialize may alter the types of model's parameters in a
# way that disrupts or destroys DDP's allreduce hooks.
if args.distributed:
# By default, apex.parallel.DistributedDataParallel overlaps
# communication with computation in the backward pass. model
# = DDP(model) delay_allreduce delays all communication to the
# end of the backward pass.
model = DDP(model, delay_allreduce=True)
# define loss function (criterion) and optimizer
criterion = nn.CrossEntropyLoss().cuda()
# Optionally resume from a checkpoint
if args.resume:
# Use a local scope to avoid dangling references
def resume():
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(
args.resume,
map_location=lambda storage, loc: storage.cuda(args.gpu),
)
args.start_epoch = checkpoint["epoch"]
global best_prec1
best_prec1 = checkpoint["best_prec1"]
model.load_state_dict(checkpoint["state_dict"])
optimizer.load_state_dict(checkpoint["optimizer"])
print(
"=> loaded checkpoint '{}' (epoch {})".format(
args.resume, checkpoint["epoch"]
)
)
else:
print("=> no checkpoint found at '{}'".format(args.resume))
resume()
if args.arch == "inception_v3":
raise RuntimeError("Currently, inception_v3 is not supported by this example.")
# crop_size = 299
# val_size = 320 # I chose this value arbitrarily, we can adjust.
else:
crop_size = 224
val_size = 256
# train pipe
train_uuids = row_keys[split[train_index]]
train_uuids = list(train_uuids)
pipe = create_dali_pipeline(
data_table=data_table,
id_col=id_col,
label_type=label_type,
label_col=label_col,
data_col=data_col,
batch_size=args.batch_size,
num_threads=args.workers,
shard_id=global_rank,
num_shards=world_size,
source_uuids=train_uuids,
device_id=local_rank,
seed=1234,
crop=crop_size,
size=val_size,
dali_cpu=args.dali_cpu,
is_training=True,
)
pipe.build()
train_loader = DALIClassificationIterator(
pipe, reader_name="Reader", last_batch_policy=LastBatchPolicy.PARTIAL
)
# val pipe
val_uuids = row_keys[split[val_index]]
val_uuids = list(val_uuids)
pipe = create_dali_pipeline(
data_table=data_table,
id_col=id_col,
label_type=label_type,
label_col=label_col,
data_col=data_col,
batch_size=args.batch_size,
num_threads=args.workers,
shard_id=global_rank,
num_shards=world_size,
source_uuids=val_uuids,
device_id=local_rank,
seed=1234,
crop=crop_size,
size=val_size,
dali_cpu=args.dali_cpu,
is_training=False,
)
pipe.build()
val_loader = DALIClassificationIterator(
pipe, reader_name="Reader", last_batch_policy=LastBatchPolicy.PARTIAL
)
if args.evaluate:
validate(val_loader, model, criterion)
return
total_time = AverageMeter()
for epoch in range(args.start_epoch, args.epochs):
# train for one epoch
avg_train_time = train(train_loader, model, criterion, optimizer, epoch)
total_time.update(avg_train_time)
if args.test:
break
# evaluate on validation set
[prec1, prec5] = validate(val_loader, model, criterion)
# remember best prec@1 and save checkpoint
if local_rank == 0: # global_rank?
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
save_checkpoint(
{
"epoch": epoch + 1,
"arch": args.arch,
"state_dict": model.state_dict(),
"best_prec1": best_prec1,
"optimizer": optimizer.state_dict(),
},
is_best,
)
if epoch == args.epochs - 1:
print(
"##Top-1 {0}\n"
"##Top-5 {1}\n"
"##Perf {2}".format(
prec1, prec5, args.total_batch_size / total_time.avg
)
)
train_loader.reset()
val_loader.reset()
def train(train_loader, model, criterion, optimizer, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, data in enumerate(train_loader):
input = data[0]["data"]
target = data[0]["label"].squeeze(-1).long()
train_loader_len = int(math.ceil(train_loader._size / args.batch_size))
if args.prof >= 0 and i == args.prof:
print("Profiling begun at iteration {}".format(i))
torch.cuda.cudart().cudaProfilerStart()
if args.prof >= 0:
torch.cuda.nvtx.range_push("Body of iteration {}".format(i))
adjust_learning_rate(optimizer, epoch, i, train_loader_len)
if args.test:
if i > 10:
break
# compute output
if args.prof >= 0:
torch.cuda.nvtx.range_push("forward")
output = model(input)
if args.prof >= 0:
torch.cuda.nvtx.range_pop()
loss = criterion(output, target)
# compute gradient and do SGD step
optimizer.zero_grad()
if args.prof >= 0:
torch.cuda.nvtx.range_push("backward")
if args.opt_level is not None:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
if args.prof >= 0:
torch.cuda.nvtx.range_pop()
if args.prof >= 0:
torch.cuda.nvtx.range_push("optimizer.step()")
optimizer.step()
if args.prof >= 0:
torch.cuda.nvtx.range_pop()
if i % args.print_freq == 0:
# Every print_freq iterations, check the loss, accuracy,
# and speed. For best performance, it doesn't make sense
# to print these metrics every iteration, since they incur
# an allreduce and some host<->device syncs.
# Measure accuracy
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
# Average loss and accuracy across processes for logging
if args.distributed:
reduced_loss = reduce_tensor(loss.data)
prec1 = reduce_tensor(prec1)
prec5 = reduce_tensor(prec5)
else:
reduced_loss = loss.data
# to_python_float incurs a host<->device sync
losses.update(to_python_float(reduced_loss), input.size(0))
top1.update(to_python_float(prec1), input.size(0))
top5.update(to_python_float(prec5), input.size(0))
torch.cuda.synchronize()
batch_time.update((time.time() - end) / args.print_freq)
end = time.time()
if local_rank == 0: # global_rank?
print(
"Epoch: [{0}][{1}/{2}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Speed {3:.3f} ({4:.3f})\t"
"Loss {loss.val:.10f} ({loss.avg:.4f})\t"
"Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t"
"Prec@5 {top5.val:.3f} ({top5.avg:.3f})".format(
epoch,
i,
train_loader_len,
world_size * args.batch_size / batch_time.val,
world_size * args.batch_size / batch_time.avg,
batch_time=batch_time,
loss=losses,
top1=top1,
top5=top5,
)
)
# Pop range "Body of iteration {}".format(i)
if args.prof >= 0:
torch.cuda.nvtx.range_pop()
if args.prof >= 0 and i == args.prof + 10:
print("Profiling ended at iteration {}".format(i))
torch.cuda.cudart().cudaProfilerStop()
quit()
return batch_time.avg
def validate(val_loader, model, criterion):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
for i, data in enumerate(val_loader):
input = data[0]["data"]
target = data[0]["label"].squeeze(-1).long()
val_loader_len = int(val_loader._size / args.batch_size)
# compute output
with torch.no_grad():
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
if args.distributed:
reduced_loss = reduce_tensor(loss.data)
prec1 = reduce_tensor(prec1)
prec5 = reduce_tensor(prec5)
else:
reduced_loss = loss.data
losses.update(to_python_float(reduced_loss), input.size(0))
top1.update(to_python_float(prec1), input.size(0))
top5.update(to_python_float(prec5), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# TODO: Change timings to mirror train().
if local_rank == 0 and i % args.print_freq == 0: # global_rank?
print(
"Test: [{0}/{1}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Speed {2:.3f} ({3:.3f})\t"
"Loss {loss.val:.4f} ({loss.avg:.4f})\t"
"Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t"
"Prec@5 {top5.val:.3f} ({top5.avg:.3f})".format(
i,
val_loader_len,
world_size * args.batch_size / batch_time.val,
world_size * args.batch_size / batch_time.avg,
batch_time=batch_time,
loss=losses,
top1=top1,
top5=top5,
)
)
print(" * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}".format(top1=top1, top5=top5))
return [top1.avg, top5.avg]
def save_checkpoint(state, is_best, filename="checkpoint.pth.tar"):
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, "model_best.pth.tar")
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
self.avg = self.sum / self.count
def adjust_learning_rate(optimizer, epoch, step, len_epoch):
"""LR schedule that should yield 76% converged accuracy with batch size 256"""
factor = epoch // 30
if epoch >= 80:
factor = factor + 1
lr = args.lr * (0.1**factor)
"""Warmup"""
if epoch < 5:
lr = lr * float(1 + step + epoch * len_epoch) / (5.0 * len_epoch)
for param_group in optimizer.param_groups:
param_group["lr"] = lr
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def reduce_tensor(tensor):
rt = tensor.clone()
dist.all_reduce(rt, op=dist.reduce_op.SUM)
rt /= world_size
return rt
if __name__ == "__main__":
main()