-
Notifications
You must be signed in to change notification settings - Fork 69
/
train.py
224 lines (185 loc) · 8.7 KB
/
train.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
import argparse
import os
from tqdm import tqdm
import time
import datetime
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import tensorboardX
import numpy as np
import lib
from lib.utils import adjust_learning_rate, cross_entropy_with_label_smoothing, \
accuracy, save_model, load_model, resume_model
best_val_acc = 0.0
def parse_args():
parser = argparse.ArgumentParser(
description='Image classification')
parser.add_argument('--dataset', default='imagenet1k',
help='Dataset names.')
parser.add_argument('--num_classes', type=int, default=1000,
help='The number of classes in the dataset.')
parser.add_argument('--train_dirs', default='./data/imagenet/train',
help='path to training data')
parser.add_argument('--val_dirs', default='./data/imagenet/val',
help='path to validation data')
parser.add_argument('--batch_size', type=int, default=256,
help='input batch size for training')
parser.add_argument('--val_batch_size', type=int, default=256,
help='input batch size for val')
parser.add_argument('--num_workers', type=int, default=32,
help='input batch size for training')
parser.add_argument("--color_jitter", action='store_true', default=False,
help="To apply color augmentation or not.")
parser.add_argument('--model', default='axial50s',
help='Model names.')
parser.add_argument('--epochs', type=int, default=130,
help='number of epochs to train')
parser.add_argument('--test_epochs', type=int, default=1,
help='number of internal epochs to test')
parser.add_argument('--save_epochs', type=int, default=1,
help='number of internal epochs to save')
parser.add_argument('--optim', default='sgd',
help='Model names.')
parser.add_argument('--lr', type=float, default=0.1,
help='learning rate')
parser.add_argument('--warmup_epochs', type=float, default=10,
help='number of warmup epochs')
parser.add_argument('--momentum', type=float, default=0.9,
help='SGD momentum')
parser.add_argument('--weight_decay', type=float, default=0.00008,
help='weight decay')
parser.add_argument("--label_smoothing", action='store_true', default=False,
help="To use label smoothing or not.")
parser.add_argument('--nesterov', action='store_true', default=False,
help='To use nesterov or not.')
parser.add_argument('--work_dirs', default='./work_dirs',
help='path to work dirs')
parser.add_argument('--name', default='axial50s',
help='the name of work_dir')
parser.add_argument('--no_cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--lr_scheduler', type=str, default="cosine", choices=["linear", "cosine"],
help='how to schedule learning rate')
parser.add_argument('--test', action='store_true', default=False,
help='Test')
parser.add_argument('--test_model', type=int, default=-1,
help="Test model's epochs")
parser.add_argument('--resume', action='store_true', default=False,
help='Resume training')
parser.add_argument('--gpu_id', default='0', type=str,
help='id(s) for CUDA_VISIBLE_DEVICES')
args = parser.parse_args()
if not os.path.exists(args.work_dirs):
os.system('mkdir -p {}'.format(args.work_dirs))
args.log_dir = os.path.join(args.work_dirs, 'log')
if not os.path.exists(args.log_dir):
os.system('mkdir -p {}'.format(args.log_dir))
args.log_dir = os.path.join(args.log_dir, args.name)
args.work_dirs = os.path.join(args.work_dirs, args.name)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
return args
def val(model, val_loader, criterion, epoch, args, log_writer=False):
global best_val_acc
model.eval()
val_loss = lib.Metric('val_loss')
val_accuracy = lib.Metric('val_accuracy')
if epoch == -1:
epoch = args.epochs - 1
with tqdm(total=len(val_loader),
desc='Validate Epoch #{}'.format(epoch + 1)) as t:
with torch.no_grad():
for data, target in val_loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
output = model(data)
val_loss.update(criterion(output, target))
val_accuracy.update(accuracy(output, target))
t.update(1)
print("\nloss: {}, accuracy: {:.2f}, best acc: {:.2f}\n".format(val_loss.avg.item(), 100. * val_accuracy.avg.item(),
100. * max(best_val_acc, val_accuracy.avg)))
if val_accuracy.avg > best_val_acc and log_writer:
save_model(model, None, -1, args)
if log_writer:
log_writer.add_scalar('val/loss', val_loss.avg, epoch)
log_writer.add_scalar('val/accuracy', val_accuracy.avg, epoch)
best_val_acc = max(best_val_acc, val_accuracy.avg)
log_writer.add_scalar('val/best_acc', best_val_acc, epoch)
def train(model, train_loader, optimizer, criterion, epoch, log_writer, args):
train_loss = lib.Metric('train_loss')
train_accuracy = lib.Metric('train_accuracy')
model.train()
N = len(train_loader)
start_time = time.time()
for batch_idx, (data, target) in enumerate(train_loader):
lr_cur = adjust_learning_rate(args, optimizer, epoch, batch_idx, N, type=args.lr_scheduler)
if args.cuda:
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss.update(loss)
train_accuracy.update(accuracy(output, target))
if (batch_idx + 1) % 20 == 0:
memory = torch.cuda.max_memory_allocated() / 1024.0 / 1024.0
used_time = time.time() - start_time
eta = used_time / (batch_idx + 1) * (N - batch_idx)
eta = str(datetime.timedelta(seconds=int(eta)))
training_state = ' '.join(['Epoch: {}', '[{} / {}]', 'eta: {}', 'lr: {:.9f}', 'max_mem: {:.0f}',
'loss: {:.3f}', 'accuracy: {:.3f}'])
training_state = training_state.format(epoch + 1, batch_idx + 1, N, eta, lr_cur, memory,
train_loss.avg.item(), 100. * train_accuracy.avg.item())
print(training_state)
if log_writer:
log_writer.add_scalar('train/loss', train_loss.avg, epoch)
log_writer.add_scalar('train/accuracy', train_accuracy.avg, epoch)
def test_net(args):
print("Init...")
_, _, val_loader, _ = lib.build_dataloader(args)
model = lib.build_model(args)
load_model(model, args)
args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.cuda:
model.cuda()
if args.label_smoothing:
criterion = cross_entropy_with_label_smoothing
else:
criterion = nn.CrossEntropyLoss()
print("Start testing...")
val(model, val_loader, criterion, args.test_model, args)
def train_net(args):
print("Init...")
log_writer = tensorboardX.SummaryWriter(args.log_dir)
train_loader, _, val_loader, _ = lib.build_dataloader(args)
model = lib.build_model(args)
print('Parameters:', sum([np.prod(p.size()) for p in model.parameters()]))
model = torch.nn.DataParallel(model)
optimizer = lib.build_optimizer(args, model)
epoch = 0
if args.resume:
epoch = resume_model(model, optimizer, args)
args.cuda = not args.no_cuda and torch.cuda.is_available()
cudnn.benchmark = True
if args.label_smoothing:
criterion = cross_entropy_with_label_smoothing
else:
criterion = nn.CrossEntropyLoss()
if args.cuda:
model.cuda()
print("Start training...")
while epoch < args.epochs:
train(model, train_loader, optimizer, criterion, epoch, log_writer, args)
if (epoch + 1) % args.test_epochs == 0:
val(model, val_loader, criterion, epoch, args, log_writer)
if (epoch + 1) % args.save_epochs == 0:
save_model(model, optimizer, epoch, args)
epoch += 1
save_model(model, optimizer, epoch - 1, args)
if __name__ == "__main__":
args = parse_args()
if args.test:
test_net(args)
else:
train_net(args)