-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathaugment.py
246 lines (202 loc) · 9.21 KB
/
augment.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
import torch.nn as nn
import torchvision
from tensorboardX import SummaryWriter
from config import AugmentConfig
import utils
from models.augment_cnn import AugmentCNN
from utils import *
from torch.utils.data import RandomSampler
config = AugmentConfig()
device = torch.device("cuda")
# tensorboard
writer = SummaryWriter(log_dir=os.path.join(config.path, "tb"))
writer.add_text('config', config.as_markdown(), 0)
logger = utils.get_logger(os.path.join(config.path, "{}.log".format(config.name)))
config.print_params(logger.info)
def main():
logger.info("Logger is set - training start")
logger.info("Torch version is: {}".format(torch.__version__))
logger.info("Torch_vision version is: {}".format(torchvision.__version__))
# set default gpu device id
torch.cuda.set_device(config.gpus[0])
# set seed
np.random.seed(config.seed)
torch.manual_seed(config.seed)
torch.cuda.manual_seed_all(config.seed)
# Important for stablizing training
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = True
# get data with meta info
input_size, input_channels, n_classes, train_data, valid_data = utils.get_data(
config.dataset, config.data_path, config.image_size, config.cutout_length, validation=True)
# print(input_size, input_channels, n_classes)
criterion = nn.CrossEntropyLoss().to(device)
use_aux = config.aux_weight > 0.
# change size of input image
input_size = config.image_size
model = AugmentCNN(input_size, input_channels, config.init_channels, n_classes, config.layers,
use_aux, eval(config.genotype))
# model size
mb_params = utils.param_size(model)
logger.info("Model size = {:.3f} MB".format(mb_params))
model = nn.DataParallel(model, device_ids=config.gpus).to(device)
if config.fp16:
model = model.half()
logger.info("Create model with Half-float data")
else:
logger.info("Create model with Full-float data")
# weights optimizer
optimizer = torch.optim.SGD(model.parameters(), config.lr, momentum=config.momentum,
weight_decay=config.weight_decay)
# get data loader
if config.data_loader_type == 'Torch':
train_loader = torch.utils.data.DataLoader(train_data,
batch_size=config.batch_size,
shuffle=True,
num_workers=config.workers,
pin_memory=True)
valid_loader = torch.utils.data.DataLoader(valid_data,
batch_size=config.batch_size,
shuffle=False,
num_workers=config.workers,
pin_memory=True)
elif config.data_loader_type == 'DALI':
config.dataset = config.dataset.lower()
if config.dataset == 'cifar10':
from DataLoaders_DALI import cifar10
train_loader = cifar10.get_cifar_iter_dali(type='train',
image_dir=config.data_path,
batch_size=config.batch_size,
num_threads=config.workers)
valid_loader = cifar10.get_cifar_iter_dali(type='val',
image_dir=config.data_path,
batch_size=config.batch_size,
num_threads=config.workers)
else:
raise NotImplementedError
else:
raise NotImplementedError
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, config.epochs)
best_top1 = 0.
if config.data_loader_type == 'DALI':
len_train_loader = get_train_loader_len(config.dataset.lower(), config.batch_size, is_train=True)
else:
len_train_loader = len(train_loader)
# training loop
for epoch in range(config.epochs):
lr_scheduler.step()
drop_prob = config.drop_path_prob * epoch / config.epochs
fp = 16 if config.fp16 else 32
model.module.drop_path_prob(drop_prob, fp)
# training
train(train_loader, model, optimizer, criterion, epoch)
# validation
cur_step = (epoch+1) * len_train_loader
top1 = validate(valid_loader, model, criterion, epoch, cur_step)
# save
if best_top1 < top1:
best_top1 = top1
is_best = True
else:
is_best = False
utils.save_checkpoint(model, config.path, is_best)
print("")
logger.info("Final best Prec@1 = {:.4%}".format(best_top1))
def train(train_loader, model, optimizer, criterion, epoch):
top1 = utils.AverageMeter()
top5 = utils.AverageMeter()
losses = utils.AverageMeter()
def train_iter(X, y):
N = X.size(0)
optimizer.zero_grad()
logits, aux_logits = model(X)
loss = criterion(logits, y)
if config.aux_weight > 0.:
loss += config.aux_weight * criterion(aux_logits, y)
loss.backward()
# gradient clipping
nn.utils.clip_grad_norm_(model.parameters(), config.grad_clip)
optimizer.step()
prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
losses.update(loss.item(), N)
top1.update(prec1.item(), N)
top5.update(prec5.item(), N)
if step % config.print_freq == 0 or step == len_train_loader - 1:
logger.info(
"Train: [{:3d}/{}] Step {:03d}/{:03d} Loss {losses.avg:.3f} "
"Prec@(1,5) ({top1.avg:.1%}, {top5.avg:.1%})".format(
epoch + 1, config.epochs, step, len_train_loader - 1, losses=losses,
top1=top1, top5=top5))
if config.data_loader_type == 'DALI':
len_train_loader = get_train_loader_len(config.dataset.lower(), config.batch_size, is_train=True)
else:
len_train_loader = len(train_loader)
cur_step = epoch * len_train_loader
cur_lr = optimizer.param_groups[0]['lr']
logger.info("Epoch {} LR {}".format(epoch, cur_lr))
writer.add_scalar('train/lr', cur_lr, cur_step)
model.train()
if config.data_loader_type == 'DALI':
for step, data in enumerate(train_loader):
X = data[0]["data"].cuda(async=True)
y = data[0]["label"].squeeze().long().cuda(async=True)
if config.cutout_length > 0:
X = cutout_batch(X, config.cutout_length)
train_iter(X, y)
cur_step += 1
train_loader.reset()
else:
for step, (X, y) in enumerate(train_loader):
# print(X.shape)
X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)
if config.fp16:
X = X.type(torch.float16)
train_iter(X, y)
cur_step += 1
logger.info("train steps: {}".format(step))
logger.info("Train: [{:3d}/{}] Final Prec@1 {:.4%}".format(epoch+1, config.epochs, top1.avg))
def validate(valid_loader, model, criterion, epoch, cur_step):
top1 = utils.AverageMeter()
top5 = utils.AverageMeter()
losses = utils.AverageMeter()
if config.data_loader_type == 'DALI':
len_val_loader = get_train_loader_len(config.dataset.lower(), config.batch_size, is_train=False)
else:
len_val_loader = len(valid_loader)
def val_iter(X, y):
N = X.size(0)
logits, _ = model(X)
loss = criterion(logits, y)
prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
losses.update(loss.item(), N)
top1.update(prec1.item(), N)
top5.update(prec5.item(), N)
if step % config.print_freq == 0 or step == len_val_loader - 1:
logger.info(
"Valid: [{:3d}/{}] Step {:03d}/{:03d} Loss {losses.avg:.3f} "
"Prec@(1,5) ({top1.avg:.1%}, {top5.avg:.1%})".format(
epoch + 1, config.epochs, step, len_val_loader - 1, losses=losses,
top1=top1, top5=top5))
model.eval()
with torch.no_grad():
if config.data_loader_type == 'DALI':
for step, data in enumerate(valid_loader):
X = data[0]["data"].cuda(async=True)
y = data[0]["label"].squeeze().long().cuda(async=True)
val_iter(X, y)
valid_loader.reset()
else:
for step, (X, y) in enumerate(valid_loader):
X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)
if config.fp16:
X = X.type(torch.float16)
val_iter(X, y)
logger.info("valid steps: {}".format(step))
writer.add_scalar('val/loss', losses.avg, cur_step)
writer.add_scalar('val/top1', top1.avg, cur_step)
writer.add_scalar('val/top5', top5.avg, cur_step)
logger.info("Valid: [{:3d}/{}] Final Prec@1 {:.4%}".format(epoch+1, config.epochs, top1.avg))
return top1.avg
if __name__ == "__main__":
main()