-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_new.py
255 lines (223 loc) · 10.4 KB
/
train_new.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
import argparse
import os
import sys
import tabulate
import time
import torch
import torch.nn.functional as F
import curves_new as curves
import data
import models
import utils
import wandb_utils
from wandb_utils import log
def main(args):
if(args.fix_end_points is not None):
args.fix_end_points = list(map(bool, args.fix_end_points))
if(args.fix_end_points is None and args.system_end_points is not None):
args.fix_end_points = [True] * len(args.system_end_points)
os.makedirs(args.dir, exist_ok=True)
with open(os.path.join(args.dir, 'command.sh'), 'w') as f:
f.write(' '.join(sys.argv))
f.write('\n')
torch.backends.cudnn.benchmark = True
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
loaders, num_classes = data.loaders(
args.dataset,
args.data_path,
args.batch_size,
args.num_workers,
args.transform,
args.dataset_scale,
args.use_test
)
if(type(num_classes) == torch.Tensor):
num_classes = num_classes.item()
architecture = getattr(models, args.model)
if args.curve is None:
model = architecture.base(num_classes=num_classes, **architecture.kwargs)
elif args.curve[-6:] == "System":
curve = getattr(curves, args.curve[:-6])
model = curves.CurveSystemNet(
num_classes,
curve,
architecture.curve,
args.num_bends,
args.fix_end_points,
architecture_kwargs=architecture.kwargs
)
base_model = None
if args.resume is None:
for k, path in enumerate(args.system_end_points):
if path is not None:
if base_model is None:
base_model = architecture.base(num_classes=num_classes, **architecture.kwargs)
checkpoint = torch.load(path)
print('Loading %s as point #%d' % (path, k+1))
base_model.load_state_dict(checkpoint['model_state'])
model.import_base_parameters(base_model, k+1)
if args.init_linear:
print('Linear initialization.')
model.init_linear()
else:
curve = getattr(curves, args.curve)
model = curves.CurveNet(
num_classes,
curve,
architecture.curve,
args.num_bends,
args.fix_start,
args.fix_end,
architecture_kwargs=architecture.kwargs,
)
base_model = None
if args.resume is None:
for path, k in [(args.init_start, 0), (args.init_end, args.num_bends - 1)]:
if path is not None:
if base_model is None:
base_model = architecture.base(num_classes=num_classes, **architecture.kwargs)
checkpoint = torch.load(path)
print('Loading %s as point #%d' % (path, k))
base_model.load_state_dict(checkpoint['model_state'])
model.import_base_parameters(base_model, k)
if args.init_linear:
print('Linear initialization.')
model.init_linear()
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
def learning_rate_schedule(base_lr, epoch, total_epochs):
alpha = epoch / total_epochs
if alpha <= 0.5:
factor = 1.0
elif alpha <= 0.9:
factor = 1.0 - (alpha - 0.5) / 0.4 * 0.99
else:
factor = 0.01
return factor * base_lr
criterion = F.cross_entropy
regularizer = None if args.curve is None else curves.l2_regularizer(args.wd)
optimizer = torch.optim.SGD(
filter(lambda param: param.requires_grad, model.parameters()),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.wd if args.curve is None else 0.0
)
start_epoch = 1
if args.resume is not None:
print('Resume training from %s' % args.resume)
checkpoint = torch.load(args.resume)
start_epoch = checkpoint['epoch'] + 1
model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])
columns = ['ep', 'lr', 'tr_loss', 'tr_acc', 'te_nll', 'te_acc', 'time']
utils.save_checkpoint(
args.dir,
start_epoch - 1,
model_state=model.state_dict(),
optimizer_state=optimizer.state_dict()
)
has_bn = utils.check_bn(model)
test_res = {'loss': None, 'accuracy': None, 'nll': None}
for epoch in range(start_epoch, args.epochs + 1):
time_ep = time.time()
lr = learning_rate_schedule(args.lr, epoch, args.epochs)
utils.adjust_learning_rate(optimizer, lr)
train_res = utils.train(loaders['train'], model, optimizer, criterion, regularizer)
if args.curve is None or not has_bn:
test_res = utils.test(loaders['test'], model, criterion, regularizer)
if epoch % args.save_freq == 0:
utils.save_checkpoint(
args.dir,
epoch,
model_state=model.state_dict(),
optimizer_state=optimizer.state_dict()
)
time_ep = time.time() - time_ep
log(dict(
epoch=epoch,
learning_rate=lr,
train_loss=train_res['loss'],
train_acc=train_res['accuracy'],
test_nll=test_res['nll'],
test_acc=test_res['accuracy'],
epoch_duration=time_ep
))
values = [epoch, lr, train_res['loss'], train_res['accuracy'], test_res['nll'],
test_res['accuracy'], time_ep]
table = tabulate.tabulate([values], columns, tablefmt='simple', floatfmt='9.4f')
if epoch % 40 == 1 or epoch == start_epoch:
table = table.split('\n')
table = '\n'.join([table[1]] + table)
else:
table = table.split('\n')[2]
print(table)
if args.epochs % args.save_freq != 0:
utils.save_checkpoint(
args.dir,
args.epochs,
model_state=model.state_dict(),
optimizer_state=optimizer.state_dict()
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='DNN curve training')
parser.add_argument('--dir', type=str, default='/tmp/curve/', metavar='DIR',
help='training directory (default: /tmp/curve/)')
parser.add_argument('--dataset', type=str, default='CIFAR10', metavar='DATASET',
help='dataset name (default: CIFAR10)')
parser.add_argument('--use_test', action='store_true',
help='switches between validation and test set (default: validation)')
parser.add_argument('--transform', type=str, default='VGG', metavar='TRANSFORM',
help='transform name (default: VGG)')
parser.add_argument('--data_path', type=str, default=None, metavar='PATH',
help='path to datasets location (default: None)')
parser.add_argument('--batch_size', type=int, default=128, metavar='N',
help='input batch size (default: 128)')
parser.add_argument('--num-workers', type=int, default=4, metavar='N',
help='number of workers (default: 4)')
parser.add_argument('--model', type=str, default=None, metavar='MODEL', required=True,
help='model name (default: None)')
parser.add_argument('--curve', type=str, default=None, metavar='CURVE',
help='curve type to use (default: None)')
parser.add_argument('--num_bends', type=int, default=3, metavar='N',
help='number of curve bends (default: 3)')
parser.add_argument('--init_start', type=str, default=None, metavar='CKPT',
help='checkpoint to init start point (default: None)')
parser.add_argument('--fix_start', dest='fix_start', action='store_true',
help='fix start point (default: off)')
parser.add_argument('--init_end', type=str, default=None, metavar='CKPT',
help='checkpoint to init end point (default: None)')
parser.add_argument('--fix_end', dest='fix_end', action='store_true',
help='fix end point (default: off)')
parser.set_defaults(init_linear=True)
parser.add_argument('--init_linear_off', dest='init_linear', action='store_false',
help='turns off linear initialization of intermediate points (default: on)')
parser.add_argument('--resume', type=str, default=None, metavar='CKPT',
help='checkpoint to resume training from (default: None)')
parser.add_argument('--epochs', type=int, default=200, metavar='N',
help='number of epochs to train (default: 200)')
parser.add_argument('--save_freq', type=int, default=20, metavar='N',
help='save frequency (default: 20)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='initial learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--wd', type=float, default=1e-4, metavar='WD',
help='weight decay (default: 1e-4)')
parser.add_argument('--dataset_scale', type=float, default=1,
help='a factor in [0, 1] that allows to scale down the data used')
parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)')
parser.set_defaults(use_wandb=False)
parser.add_argument('--no_wandb', action='store_false', dest='use_wandb',
help='Turns off logging to wandb')
parser.add_argument('--wandb_log', action='store_true',
help='Turns on logging to wandb')
parser.add_argument('--system_end_points', nargs="+", help='path to the endpoints of a curve system (e.g. -- system_end_points endpt1.pt endpt2.pt')
parser.add_argument('--fix_end_points', nargs="+", type=int, help='boolean values indicating which endpoints can be trained with the system and which are fixed (use 1 or 0)')
args = parser.parse_args()
args = wandb_utils.init_wandb(args)
try:
main(args)
except Exception as e:
with open("error.txt", "a") as f:
f.write(str(e))