-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtrain_supernet.py
158 lines (143 loc) · 7.36 KB
/
train_supernet.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
import argparse
import logging
import os
import sys
import time
import torch
import torch.nn as nn
import torchvision
from torchvision import datasets
import utils
from models.model import SinglePath_OneShot
parser = argparse.ArgumentParser("Single_Path_One_Shot")
parser.add_argument('--exp_name', type=str, default='spos_c10_train_supernet', help='experiment name')
# Supernet Settings
parser.add_argument('--layers', type=int, default=20, help='batch size')
parser.add_argument('--num_choices', type=int, default=4, help='number choices per layer')
# Training Settings
parser.add_argument('--batch_size', type=int, default=96, help='batch size')
parser.add_argument('--epochs', type=int, default=600, help='batch size')
parser.add_argument('--learning_rate', type=float, default=0.025, help='initial learning rate')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum')
parser.add_argument('--weight-decay', type=float, default=3e-4, help='weight decay')
parser.add_argument('--print_freq', type=int, default=100, help='print frequency of training')
parser.add_argument('--val_interval', type=int, default=5, help='validate and save frequency')
parser.add_argument('--ckpt_dir', type=str, default='./checkpoints/', help='checkpoints direction')
parser.add_argument('--seed', type=int, default=0, help='training seed')
# Dataset Settings
parser.add_argument('--data_root', type=str, default='./dataset/', help='dataset dir')
parser.add_argument('--classes', type=int, default=10, help='dataset classes')
parser.add_argument('--dataset', type=str, default='cifar10', help='path to the dataset')
parser.add_argument('--cutout', action='store_true', help='use cutout')
parser.add_argument('--cutout_length', type=int, default=16, help='cutout length')
parser.add_argument('--auto_aug', action='store_true', default=False, help='use auto augmentation')
parser.add_argument('--resize', action='store_true', default=False, help='use resize')
args = parser.parse_args()
args.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
format=log_format, datefmt='%m/%d %I:%M:%S %p')
logging.info(args)
utils.set_seed(args.seed)
def train(args, epoch, train_loader, model, criterion, optimizer):
model.train()
lr = optimizer.param_groups[0]["lr"]
train_acc = utils.AverageMeter()
train_loss = utils.AverageMeter()
steps_per_epoch = len(train_loader)
for step, (inputs, targets) in enumerate(train_loader):
inputs, targets = inputs.to(args.device), targets.to(args.device)
optimizer.zero_grad()
choice = utils.random_choice(args.num_choices, args.layers)
outputs = model(inputs, choice)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
prec1, prec5 = utils.accuracy(outputs, targets, topk=(1, 5))
n = inputs.size(0)
train_loss.update(loss.item(), n)
train_acc.update(prec1.item(), n)
if step % args.print_freq == 0 or step == len(train_loader) - 1:
logging.info(
'[Supernet Training] lr: %.5f epoch: %03d/%03d, step: %03d/%03d, '
'train_loss: %.3f(%.3f), train_acc: %.3f(%.3f)'
% (lr, epoch+1, args.epochs, step+1, steps_per_epoch,
loss.item(), train_loss.avg, prec1, train_acc.avg)
)
return train_loss.avg, train_acc.avg
def validate(args, val_loader, model, criterion):
model.eval()
val_loss = utils.AverageMeter()
val_acc = utils.AverageMeter()
with torch.no_grad():
for step, (inputs, targets) in enumerate(val_loader):
inputs, targets = inputs.to(args.device), targets.to(args.device)
choice = utils.random_choice(args.num_choices, args.layers)
outputs = model(inputs, choice)
loss = criterion(outputs, targets)
prec1, prec5 = utils.accuracy(outputs, targets, topk=(1, 5))
n = inputs.size(0)
val_loss.update(loss.item(), n)
val_acc.update(prec1.item(), n)
return val_loss.avg, val_acc.avg
def main():
# Check Checkpoints Direction
if not os.path.exists(args.ckpt_dir):
os.mkdir(args.ckpt_dir)
# Define Data
assert args.dataset in ['cifar10', 'imagenet']
train_transform, valid_transform = utils.data_transforms(args)
if args.dataset == 'cifar10':
trainset = torchvision.datasets.CIFAR10(root=os.path.join(args.data_root, args.dataset), train=True,
download=True, transform=train_transform)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size,
shuffle=True, pin_memory=True, num_workers=8)
valset = torchvision.datasets.CIFAR10(root=os.path.join(args.data_root, args.dataset), train=False,
download=True, transform=valid_transform)
val_loader = torch.utils.data.DataLoader(valset, batch_size=args.batch_size,
shuffle=False, pin_memory=True, num_workers=8)
elif args.dataset == 'imagenet':
train_data_set = datasets.ImageNet(os.path.join(args.data_root, args.dataset, 'train'), train_transform)
val_data_set = datasets.ImageNet(os.path.join(args.data_root, args.dataset, 'valid'), valid_transform)
train_loader = torch.utils.data.DataLoader(train_data_set, batch_size=args.batch_size, shuffle=True,
num_workers=8, pin_memory=True, sampler=None)
val_loader = torch.utils.data.DataLoader(val_data_set, batch_size=args.batch_size, shuffle=False,
num_workers=8, pin_memory=True)
else:
raise ValueError('Undefined dataset !!!')
# Define Supernet
model = SinglePath_OneShot(args.dataset, args.resize, args.classes, args.layers)
logging.info(model)
model = model.to(args.device)
criterion = nn.CrossEntropyLoss().to(args.device)
optimizer = torch.optim.SGD(model.parameters(), args.learning_rate, args.momentum, args.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epochs)
print('\n')
# Running
start = time.time()
best_val_acc = 0.0
for epoch in range(args.epochs):
# Supernet Training
train_loss, train_acc = train(args, epoch, train_loader, model, criterion, optimizer)
scheduler.step()
logging.info(
'[Supernet Training] epoch: %03d, train_loss: %.3f, train_acc: %.3f' %
(epoch + 1, train_loss, train_acc)
)
# Supernet Validation
val_loss, val_acc = validate(args, val_loader, model, criterion)
# Save Best Supernet Weights
if best_val_acc < val_acc:
best_val_acc = val_acc
best_ckpt = os.path.join(args.ckpt_dir, '%s_%s' % (args.exp_name, 'best.pth'))
torch.save(model.state_dict(), best_ckpt)
logging.info('Save best checkpoints to %s' % best_ckpt)
logging.info(
'[Supernet Validation] epoch: %03d, val_loss: %.3f, val_acc: %.3f, best_acc: %.3f'
% (epoch + 1, val_loss, val_acc, best_val_acc)
)
print('\n')
# Record Time
utils.time_record(start)
if __name__ == '__main__':
main()