-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
323 lines (291 loc) · 8.38 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
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
from collections import defaultdict
import click
from click import option as opt
from torch import nn
from torch.autograd import Variable
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from torch.utils.data.sampler import WeightedRandomSampler
import torch
import torch.backends.cudnn as cudnn
from tqdm import tqdm
from augmentation_transforms import make_augmentation_transforms
import config
import dataset
from model import make_nasnet_model
from optimizer import get_optimizer
from utils import (
save_checkpoint,
load_checkpoint,
MetricMonitor,
set_seed,
calculate_accuracy,
TensorboardClient
)
cudnn.benchmark = config.CUDNN_BENCHMARK
def forward_pass(
images,
targets,
model,
loss_fn,
epoch,
stream,
monitor,
mode='train',
):
images = Variable(images).cuda(async=True)
targets = Variable(targets).cuda(async=True)
outputs = model(images)
accuracy = calculate_accuracy(outputs, targets)
monitor.update('accuracy', accuracy, multiply_by_n=False)
loss = loss_fn(outputs, targets)
monitor.update('loss', loss.data[0])
stream.set_description(f'epoch: {epoch} | {mode}: {monitor}')
return loss, outputs
def train(
train_data_loader,
model,
optimizer,
iter_size,
loss_fn,
epoch,
tensorboard_client,
grad_max_norm,
):
model.train()
train_monitor = MetricMonitor(batch_size=train_data_loader.batch_size)
stream = tqdm(train_data_loader)
for i, (images, targets) in enumerate(stream, start=1):
loss, _ = forward_pass(
images,
targets,
model,
loss_fn,
epoch,
stream,
train_monitor,
mode='train',
)
loss.backward()
if grad_max_norm is not None:
torch.nn.utils.clip_grad_norm(model.parameters(), grad_max_norm)
if i % iter_size == 0 or i == len(train_data_loader):
optimizer.step()
optimizer.zero_grad()
tensorboard_client.log_value(
'train',
'lr',
optimizer.param_groups[0]['lr'],
epoch,
)
for metric, value in train_monitor.get_metric_values():
tensorboard_client.log_value('train', metric, value, epoch)
def validate(valid_data_loader, model, loss_fn, epoch, tensorboard_client):
model.eval()
valid_monitor = MetricMonitor(batch_size=valid_data_loader.batch_size)
stream = tqdm(valid_data_loader)
with torch.no_grad():
for images, targets in stream:
_, outputs = forward_pass(
images,
targets,
model,
loss_fn,
epoch,
stream,
valid_monitor,
mode='valid',
)
for metric, value in valid_monitor.get_metric_values():
tensorboard_client.log_value('valid', metric, value, epoch)
return valid_monitor
def train_and_validate(
train_data_loader,
valid_data_loader,
model,
optimizer,
iter_size,
scheduler,
loss_fn,
epochs,
experiment_name,
tensorboard_client,
start_epoch,
best_val_loss,
max_epochs_without_improvement,
grad_max_norm,
):
if best_val_loss is None:
best_val_loss = float('+inf')
epochs_without_improvement = 0
best_checkpoint = None
for epoch in range(start_epoch, epochs + 1):
train(
train_data_loader,
model,
optimizer,
iter_size,
loss_fn,
epoch,
tensorboard_client,
grad_max_norm,
)
val_monitor = validate(
valid_data_loader,
model,
loss_fn,
epoch,
tensorboard_client,
)
val_loss = val_monitor.get_avg('loss')
if val_loss < best_val_loss:
print('Best model so far!')
best_val_loss = val_loss
epochs_without_improvement = 0
best_checkpoint = {
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'val_loss': val_loss,
}
save_checkpoint(
best_checkpoint,
f'{experiment_name}_best.pth',
verbose=True,
)
else:
epochs_without_improvement += 1
if epochs_without_improvement > max_epochs_without_improvement:
print(
f'{epochs_without_improvement} epochs without improvement. '
f'Training is finished.'
)
break
scheduler.step(val_loss)
return best_checkpoint
@click.command()
@opt('--batch-size', default=32)
@opt('--optimizer-name', type=click.Choice(['adam', 'sgd']), default='sgd')
@opt('--lr', default=0.001)
@opt('--epochs', default=300)
@opt('--iter-size', default=1)
@opt('--experiment-name', type=str)
@opt('--folds', default=5)
@opt('--fold-num', default=0)
@opt('--load-best-model', is_flag=True)
@opt('--load-best-model-optimizer', is_flag=True)
@opt('--start-epoch', default=1)
@opt('--seed', default=config.SEED)
@opt('--dropout-p', default=0.5)
@opt('--num-workers', default=config.NUM_WORKERS)
@opt('--max-epochs-without-improvement', default=9)
@opt('--grad-max-norm', type=float)
@opt('--augmentation', type=str, required=True)
def main(
batch_size,
optimizer_name,
lr,
epochs,
iter_size,
experiment_name,
folds,
fold_num,
load_best_model,
load_best_model_optimizer,
start_epoch,
seed,
dropout_p,
num_workers,
max_epochs_without_improvement,
grad_max_norm,
augmentation,
):
set_seed(seed)
transform_train = make_augmentation_transforms(augmentation, mode='train')
transform_valid = make_augmentation_transforms(augmentation, mode='valid')
if experiment_name is None:
experiment_name = f'{config.PROJECT_NAME}'
full_experiment_name = f'{experiment_name}_{fold_num}_{folds}'
print(full_experiment_name)
model = make_nasnet_model(
num_classes=config.NUM_CLASSES,
dropout_p=dropout_p,
)
best_val_loss = None
model = model.cuda()
optimizer = get_optimizer(optimizer_name, lr, model)
if load_best_model:
checkpoint_filename = f'{full_experiment_name}_temp.pth'
print(f'Loading checkpoint {checkpoint_filename}')
checkpoint = load_checkpoint(checkpoint_filename)
model.load_state_dict(checkpoint['state_dict'])
best_val_loss = checkpoint.get('val_loss')
if load_best_model_optimizer:
optimizer.load_state_dict(checkpoint['optimizer'])
optimizer.state = defaultdict(dict, optimizer.state)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
tensorboard_client = TensorboardClient(full_experiment_name)
loss_fn = nn.CrossEntropyLoss().cuda()
scheduler = ReduceLROnPlateau(
optimizer,
patience=3,
verbose=True,
threshold=1e-5,
min_lr=0,
mode='min',
)
dataset_args = {
'folds': folds,
'fold_num': fold_num,
}
train_dataset = dataset.TrainValidDataset(
mode='train',
transform=transform_train,
**dataset_args,
)
valid_dataset = dataset.TrainValidDataset(
mode='valid',
transform=transform_valid,
**dataset_args,
)
data_loader_args = {
'pin_memory': True,
'num_workers': num_workers,
}
train_data_weights = (
torch.from_numpy(train_dataset.get_item_weights()).double()
)
train_sampler = WeightedRandomSampler(
train_data_weights,
len(train_dataset),
)
train_data_loader = DataLoader(
**data_loader_args,
dataset=train_dataset,
sampler=train_sampler,
batch_size=batch_size,
)
valid_data_loader = DataLoader(
**data_loader_args,
dataset=valid_dataset,
batch_size=batch_size,
)
train_and_validate(
train_data_loader,
valid_data_loader,
model,
optimizer,
iter_size,
scheduler,
loss_fn,
epochs,
full_experiment_name,
tensorboard_client,
start_epoch,
best_val_loss,
max_epochs_without_improvement,
grad_max_norm,
)
if __name__ == '__main__':
main()