forked from cleanlab/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar10_train_crossval.py
735 lines (644 loc) · 29.4 KB
/
cifar10_train_crossval.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
# coding: utf-8
# This code extends the functionality of
# https://github.com/pytorch/examples/tree/master/imagenet
# to support cross-validation training, allowing you compute the out of sample
# predicted probabilities for the entire CIFAR training set:
# a necessary step for confident learning and the cleanlab package.
#
# Example showing how to obtain 4-fold cross-validated predicted probabilities:
# $ python3 cifar10_train_crossval.py \
# -a resnet50 -b 256 --lr 0.1 --gpu 0 --cvn 4 --cv 0 \
# --train-labels LABELS_PATH.json CIFAR10_PATH
# $ python3 cifar10_train_crossval.py \
# -a resnet50 -b 256 --lr 0.1 --gpu 1 --cvn 4 --cv 1 \
# # --train-labels LABELS_PATH.json CIFAR10_PATH
# $ python3 cifar10_train_crossval.py \
# -a resnet50 -b 256 --lr 0.1 --gpu 2 --cvn 4 --cv 2 \
# # --train-labels LABELS_PATH.json CIFAR10_PATH
# $ python3 cifar10_train_crossval.py \
# -a resnet50 -b 256 --lr 0.1 --gpu 3 --cvn 4 --cv 3 \
# # --train-labels LABELS_PATH.json CIFAR10_PATH
#
# Combine the cross-validation folds into a single predicted prob matrix
# $ python3 cifar10_train_crossval.py \
# -a resnet50 --cvn 4 --combine-folds CIFAR10_PATH
#
# This script can also be used to train on CLEANED datasets, like this:
# python3 cifar10_train_crossval.py \
# -a resnet50 -b 256 --lr 0.1 --gpu 0 --train-labels LABELS_PATH.json \
# --dir-train-mask PATH_TO_CLEAN_DATA_BOOL_MASK.npy CIFAR10_PATH
# These imports enhance Python2/3 compatibility.
from __future__ import (
print_function, absolute_import, division, unicode_literals, with_statement,
)
import argparse
import copy
import json
import os
import random
import shutil
import time
import warnings
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.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.datasets as datasets
import torchvision.models as models
import torchvision.transforms as transforms
from sklearn.model_selection import StratifiedKFold
num_classes = 10
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('data', metavar='DIR',
help='path to dataset')
parser.add_argument('-a', '--arch', metavar='ARCH', default='resnet50',
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=350, type=int, metavar='N',
help='number of epochs to run. Use 250 for Co-Teaching')
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=64, type=int,
metavar='N',
help='mini-batch size (default: 64), this is the total '
'batch size of all GPUs on the current node when '
'using Data Parallel or Distributed Data Parallel.'
'Use 128 for Co-Teaching.')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--wd', '--weight-decay', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)',
dest='weight_decay')
parser.add_argument('-p', '--print-freq', default=50, 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('--world-size', default=-1, type=int,
help='number of nodes for distributed training')
parser.add_argument('--rank', default=-1, type=int,
help='node rank for distributed training')
parser.add_argument('--dist-url', default='tcp://224.66.41.62:23456', 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=None, type=int,
help='seed for initializing training. ')
parser.add_argument('--cv-seed', default=0, type=int,
help='seed for determining the cv folds. ')
parser.add_argument('--gpu', default=None, type=int,
help='GPU id to use.')
parser.add_argument('--multiprocessing-distributed', action='store_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')
parser.add_argument('--cv', '--cv-fold', type=int, default=None,
metavar='N', help='The fold to holdout')
parser.add_argument('--cvn', '--cv-n-folds', default=0, type=int,
metavar='N', help='The number of folds')
parser.add_argument('-m', '--dir-train-mask', default=None, type=str,
help='Boolean mask with True for indices to '
'train with and false for indices to skip.')
parser.add_argument('--combine-folds', action='store_true', default=False,
help='Pass this flag and -a arch to combine probs from all'
'folds. You must pass -a and -cvn flags as well!')
parser.add_argument('--train-labels', type=str, default=None,
help='DIR of training labels format: json filename2integer')
parser.add_argument('--coteaching', action='store_true',
help='Use Co-Teaching algorithm to train (Han et al 2018).')
parser.add_argument('--num-iter-per-epoch', type=int, default=400,
help='In each epoch, only train for this many iterations.'
'Total number of examples trained per epoch is'
'args.num_iter_per_epoch * args.batch_size')
parser.add_argument('--forget-rate', type=float, default=0.2,
help='Co-Teaching forget rate. Set to % noise if you can.')
parser.add_argument('--num_gradual', type=int, default=10,
help='Co-Teaching num epochs for linear drop rate,'
'can be 5, 10, 15. This parameter is Tk for R(T) in'
'Co-teaching paper.')
parser.add_argument('--exponent', type=float, default=1,
help='Co-Teaching exponent of the forget rate, can be'
'0.5, 1, 2. This parameter is equal to c in Tc for'
'R(T) in Co-teaching paper.')
parser.add_argument('--epoch_decay_start', type=int, default=80,
help='Co-Teaching number of epochs to train before'
'starting to decay the learning rate.')
parser.add_argument('--turn-off-save-checkpoint', action='store_true',
help='Prevents saving model at every epoch of training.')
best_acc1 = 0
def main(args):
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.deterministic = True
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"])
args.distributed = args.world_size > 1 or args.multiprocessing_distributed
ngpus_per_node = torch.cuda.device_count()
if args.multiprocessing_distributed:
# Since we have ngpus_per_node processes per node, the total world_size
# needs to be adjusted accordingly
args.world_size = ngpus_per_node * args.world_size
# Use torch.multiprocessing.spawn to launch distributed processes: the
# main_worker process function
mp.spawn(main_worker, nprocs=ngpus_per_node,
args=(ngpus_per_node, args))
else:
# Simply call main_worker function
main_worker(args.gpu, ngpus_per_node, args)
def main_worker(gpu, ngpus_per_node, args):
global best_acc1
args.gpu = gpu
use_crossval = args.cvn > 0
use_mask = args.dir_train_mask is not None
cv_fold = args.cv
cv_n_folds = args.cvn
class_weights = None
if use_crossval and use_mask:
raise ValueError(
'Either args.cvn > 0 or dir-train-mask not None, but not both.')
if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
if args.distributed:
if args.dist_url == "env://" and args.rank == -1:
args.rank = int(os.environ["RANK"])
if args.multiprocessing_distributed:
# For multiprocessing distributed training, rank needs to be the
# global rank among all the processes
args.rank = args.rank * ngpus_per_node + gpu
dist.init_process_group(
backend=args.dist_backend,
init_method=args.dist_url,
world_size=args.world_size,
rank=args.rank,
)
# create model
if args.coteaching:
if args.gpu is None:
raise AssertionError('if --co-teaching used, --gpu must be used.')
torch.cuda.set_device(args.gpu)
from cleanlab.models.cifar_cnn import CNN
from cleanlab import coteaching
model = CNN(input_channel=3, n_outputs=num_classes)
model.cuda(args.gpu)
model2 = CNN(input_channel=3, n_outputs=num_classes)
model2.cuda(args.gpu)
elif args.pretrained:
print("=> using pre-trained model '{}'".format(args.arch))
model = models.__dict__[args.arch](
pretrained=True, num_classes=num_classes)
else:
print("=> creating model '{}'".format(args.arch))
model = models.__dict__[args.arch](num_classes=num_classes)
if args.coteaching:
pass # Do nothing
elif args.distributed:
# For multiprocessing, DistributedDataParallel constructor
# should always set the single device scope, otherwise,
# DistributedDataParallel will use all available devices.
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model.cuda(args.gpu)
# When using a single GPU per process and per
# DistributedDataParallel, we need to divide the batch size
# ourselves based on the total number of GPUs we have
args.batch_size = int(args.batch_size / ngpus_per_node)
args.workers = int(args.workers / ngpus_per_node)
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[args.gpu])
else:
model.cuda()
# DistributedDataParallel will divide and allocate batch_size
# to all available GPUs if device_ids are not set
model = torch.nn.parallel.DistributedDataParallel(model)
elif args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
else:
# DataParallel will divide and allocate batch_size to all GPUs
if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
model.features = torch.nn.DataParallel(model.features)
model.cuda()
else:
model = torch.nn.DataParallel(model).cuda()
if args.coteaching:
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
optimizer2 = torch.optim.Adam(model2.parameters(), lr=args.lr)
# Set-up learning rate scheduler alpha and betas for Adam Optimizer
alpha_plan, beta1_plan = coteaching.initialize_lr_scheduler(
lr=args.lr,
epochs=args.epochs,
epoch_decay_start=args.epoch_decay_start,
)
# Schedule fraction of examples to forget at each Co-Teaching epoch.
forget_rate_schedule = coteaching.forget_rate_scheduler(
args.epochs, args.forget_rate, args.num_gradual, args.exponent)
else:
optimizer = torch.optim.SGD(
model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
if args.gpu is None:
checkpoint = torch.load(args.resume)
else:
# Map model to be loaded to specified single gpu.
loc = 'cuda:{}'.format(args.gpu)
checkpoint = torch.load(args.resume, map_location=loc)
args.start_epoch = checkpoint['epoch']
best_acc1 = checkpoint['best_acc1']
if args.gpu is not None:
# In case you load checkpoint from different GPU
best_acc1 = best_acc1.to(args.gpu)
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))
cudnn.benchmark = True
# Data loading code
traindir = os.path.join(args.data, 'train')
valdir = os.path.join(args.data, 'test')
train_dataset = datasets.ImageFolder(
traindir,
transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
]),
)
# if training labels are provided use those instead of dataset labels
if args.train_labels is not None:
with open(args.train_labels, 'r') as rf:
train_labels_dict = json.load(rf)
train_dataset.imgs = [(fn, train_labels_dict[fn]) for fn, _ in
train_dataset.imgs]
train_dataset.samples = train_dataset.imgs
# If training only on cross-validated portion & make val_set = train_holdout
if use_crossval:
checkpoint_fn = "model_{}__fold_{}__checkpoint.pth.tar".format(
args.arch, cv_fold)
print('Computing fold indices. This takes 15 seconds.')
# Prepare labels
labels = [label for img, label in datasets.ImageFolder(traindir).imgs]
# Split train into train and holdout for particular cv_fold.
kf = StratifiedKFold(n_splits=cv_n_folds, shuffle=True,
random_state=args.cv_seed)
cv_train_idx, cv_holdout_idx = (
list(kf.split(range(len(labels)), labels))[cv_fold])
# Separate datasets
np.random.seed(args.cv_seed)
holdout_dataset = copy.deepcopy(train_dataset)
holdout_dataset.imgs = [train_dataset.imgs[i] for i in cv_holdout_idx]
holdout_dataset.samples = holdout_dataset.imgs
train_dataset.imgs = [train_dataset.imgs[i] for i in cv_train_idx]
train_dataset.samples = train_dataset.imgs
print('Train size:', len(cv_train_idx), len(train_dataset.imgs))
print('Holdout size:', len(cv_holdout_idx), len(holdout_dataset.imgs))
else:
checkpoint_fn = "model_{}__checkpoint.pth.tar".format(args.arch)
if use_mask:
checkpoint_fn = "model_{}__masked__checkpoint.pth.tar".format(
args.arch)
orig_class_counts = np.bincount(
[lab for img, lab in datasets.ImageFolder(traindir).imgs],
minlength=num_classes,
)
train_bool_mask = np.load(args.dir_train_mask)
# Mask labels
train_dataset.imgs = [img for i, img in
enumerate(train_dataset.imgs) if
train_bool_mask[i]]
train_dataset.samples = train_dataset.imgs
clean_class_counts = np.bincount(
[lab for img, lab in train_dataset.imgs],
minlength=num_classes,
)
# We divide by this, so make sure no count is zero
clean_class_counts = np.asarray(
[1 if z == 0 else z for z in clean_class_counts])
print('Train size:', len(train_dataset.imgs))
# Compute class weights to re-weight loss during training
# Should use the confident joint to estimate the noise matrix then
# class_weights = 1 / p(s=k, y=k) for each class k.
# Here we approximate this with a simpler approach
# class_weights = count(y=k) / count(s=k, y=k)
class_weights = torch.Tensor(orig_class_counts / clean_class_counts)
class_weights = class_weights.cuda(args.gpu)
val_dataset = datasets.ImageFolder(
valdir,
transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
]),
)
if args.distributed:
train_sampler = torch.utils.data.distributed.DistributedSampler(
train_dataset)
else:
train_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=not args.distributed,
num_workers=args.workers,
pin_memory=True,
sampler=train_sampler,
drop_last=True, # Don't train on last batch: could be 1 noisy example.
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True,
)
# define loss function (criterion)
criterion = nn.CrossEntropyLoss(weight=class_weights).cuda(args.gpu)
# define separate loss function for val set that does not use class_weights
val_criterion = nn.CrossEntropyLoss(weight=None).cuda(args.gpu)
if args.evaluate:
validate(val_loader, model, criterion, args)
return
for epoch in range(args.start_epoch, args.epochs):
if args.distributed:
train_sampler.set_epoch(epoch)
# Train for one epoch
if args.coteaching:
coteaching.adjust_learning_rate(
optimizer, epoch, alpha_plan, beta1_plan)
coteaching.adjust_learning_rate(
optimizer2, epoch, alpha_plan, beta1_plan)
coteaching.train(train_loader, epoch, model, optimizer, model2,
optimizer2, args, forget_rate_schedule,
class_weights, accuracy)
# Evaluate
val_acc1, val_acc2 = coteaching.evaluate(val_loader, model, model2)
print('Epoch [%d/%d] Test Accuracy on the %s test images: Model1 '
'%.4f %% Model2 %.4f %%' % (
epoch + 1, args.epochs, len(val_dataset), val_acc1,
val_acc2))
else:
adjust_learning_rate(optimizer, epoch, args)
train(train_loader, model, criterion, optimizer, epoch, args)
# Evaluate on validation set
acc1 = validate(val_loader, model, criterion, args)
if args.coteaching:
print('Model 2:')
validate(val_loader, model2, val_criterion, args)
# Remember best acc@1, model, and save checkpoint.
is_best = acc1 > best_acc1
best_acc1 = max(best_acc1, acc1)
if (
not args.turn_off_save_checkpoint
and not args.multiprocessing_distributed
) or (
args.multiprocessing_distributed
and args.rank % ngpus_per_node == 0
):
save_checkpoint(
{
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_acc1': best_acc1,
'optimizer': optimizer.state_dict(),
},
is_best=is_best,
filename=checkpoint_fn,
cv_fold=cv_fold,
use_mask=use_mask,
)
if use_crossval:
holdout_loader = torch.utils.data.DataLoader(
holdout_dataset,
batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True,
)
if not args.turn_off_save_checkpoint: # Load best of saved checkpoints
print("=> loading best model_{}__fold_{}_best.pth.tar".format(
args.arch, cv_fold))
checkpoint = torch.load(
"model_{}__fold_{}_best.pth.tar".format(args.arch, cv_fold))
model.load_state_dict(checkpoint['state_dict'])
print("Running forward pass on holdout set of size:",
len(holdout_dataset.imgs))
probs = get_probs(holdout_loader, model, args)
np.save('model_{}__fold_{}__probs.npy'.format(args.arch, cv_fold),
probs)
def train(train_loader, model, criterion, optimizer, epoch, args):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, (input, target) in enumerate(train_loader):
# if batch is size 1, skip because batch-norm will fail
if len(input) <= 1:
continue
# measure data loading time
data_time.update(time.time() - end)
if args.gpu is not None:
input = input.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1[0], input.size(0))
top5.update(acc5[0], input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Acc@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, top1=top1, top5=top5))
def get_probs(loader, model, args):
# Switch to evaluate mode.
model.eval()
n_total = len(loader.dataset.imgs) / float(loader.batch_size)
outputs = []
with torch.no_grad():
end = time.time()
for i, (input, target) in enumerate(loader):
print("\rComplete: {:.1%}".format(i / n_total), end="")
if args.gpu is not None:
input = input.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)
# compute output
outputs.append(model(input))
# Prepare outputs as a single matrix
probs = np.concatenate([
torch.nn.functional.softmax(z, dim=1) if args.gpu is None else
torch.nn.functional.softmax(z, dim=1).cpu().numpy()
for z in outputs
])
return probs
def validate(val_loader, model, criterion, args):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
end = time.time()
for i, (input, target) in enumerate(val_loader):
if args.gpu is not None:
input = input.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1[0], input.size(0))
top5.update(acc5[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Acc@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
i, len(val_loader), batch_time=batch_time, loss=losses,
top1=top1, top5=top5))
print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
return top1.avg
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar', cv_fold=None,
use_mask=False):
torch.save(state, filename)
if is_best:
sm = "__masked" if use_mask else ""
sf = "__fold_{}".format(cv_fold) if cv_fold is not None else ""
wfn = 'model_{}{}{}_best.pth.tar'.format(state['arch'], sm, sf)
shutil.copyfile(filename, wfn)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
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, args):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs
0.1 for epoch [0,150)
0.01 for epoch [150,250)
0.001 for epoch [250,350)"""
if epoch < int(150. / 350 * args.epochs):
lr = args.lr
elif epoch < int(250. / 350 * args.epochs):
lr = args.lr * 0.1
else:
lr = args.lr * 0.01
for param_group in optimizer.param_groups:
param_group['lr'] = lr
print('epoch:', epoch + 1, '| lr:', lr)
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the
specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.reshape(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 combine_folds(args):
wfn = 'cifar10__train__model_{}__pyx.npy'.format(args.arch)
print('Make sure you specified the model architecture with flag -a.')
print('This method will overwrite file: {}'.format(wfn))
print('Computing fold indices. This takes 15 seconds.')
# Prepare labels
labels = [label for img, label in
datasets.ImageFolder(os.path.join(args.data, "train/")).imgs]
# Initialize pyx array (output of trained network)
pyx = np.empty((len(labels), num_classes))
# Split train into train and holdout for each cv_fold.
kf = StratifiedKFold(n_splits=args.cvn, shuffle=True,
random_state=args.cv_seed)
for k, (cv_train_idx, cv_holdout_idx) in enumerate(
kf.split(range(len(labels)), labels)):
probs = np.load('model_{}__fold_{}__probs.npy'.format(args.arch, k))
pyx[cv_holdout_idx] = probs[:, :num_classes]
print('Writing final predicted probabilities.')
np.save(wfn, pyx)
# Compute overall accuracy
print('Computing Accuracy.', flush=True)
acc = sum(np.array(labels) == np.argmax(pyx, axis=1)) / float(len(labels))
print('Accuracy: {:.25}'.format(acc))
if __name__ == '__main__':
arg_parser = parser.parse_args()
if arg_parser.combine_folds:
combine_folds(arg_parser)
else:
main(arg_parser)