-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcae_ba.py
442 lines (358 loc) · 17.6 KB
/
cae_ba.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#
# mDKL
#
# Copyright (c) Siemens AG, 2021
# Authors:
# Zhiliang Wu <zhiliang.wu@siemens.com>
# License-Identifier: MIT
from pathlib import Path
import gc
from functools import partial
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mlflow
import torch
from torch import nn
from torch.optim.lr_scheduler import StepLR
import ignite
from ignite.contrib.handlers.mlflow_logger import MLflowLogger, \
global_step_from_engine
from ignite.contrib.handlers import FastaiLRFinder, ProgressBar
from ignite.contrib.handlers.param_scheduler import LRScheduler
from ignite.engine import Events, create_supervised_trainer, \
create_supervised_evaluator
from ignite.handlers import DiskSaver, EarlyStopping, Checkpoint
from ignite.metrics import MeanSquaredError
from data_utils import get_tranform, get_augm_data_loaders_in_file, BoneAge, \
prepare_batch_cae
from logging_conf import logger
from model_utils import get_pretrained_models
from cae_utils import BasicBlock, InvResNet, ConvolutionalAutoencoder
def lr_finder(batch_size, alpha, end_lr=10, diverge_th=5, model_name='resnet',
pretrain=True, fold_idx=0, device=torch.device('cpu'),
exp_name='dataset_xxx',
run_name='model_lrf', seed=42):
"""Find a suitable learning rate.
More theory see https://www.jeremyjordan.me/nn-learning-rate/.
Args:
batch_size (int): Batch size.
alpha (float): The value of weight decay (a.k.a. regularization).
end_lr (int or float): The upper bound of the tested learning rate.
diverge_th (int or float): The threshold for the divergence.
model_name (str): The name of the backbone.
pretrain (bool): Whether load the weights in pretrained models.
fold_idx (int): The index of the training/validation set.
device (torch.device or str): The device to load the models.
exp_name (str): The name of the experiments with a format of
dataset+xxx, which defines the experiment name inside MLflow.
run_name (str): The name of the run with a format of [model_name]_lrf,
which defines the run name inside MLflow.
seed (int): The number of the random seed to ensure the reproducibility.
Returns:
None: The plot of the lrfinder will be saved.
"""
np.random.seed(seed)
torch.manual_seed(seed)
logger.info(f'Running learning rate finder with {exp_name} on model '
f'{model_name}.')
df_path = Path('../boneage.csv')
im_path = Path(f'{str(Path.home())}/boneage/')
df_split = pd.read_csv('../boneage_idx_split.csv',
index_col=0)
custom_tranform = get_tranform(mean=(0.183, 0.183, 0.183),
std=(0.166, 0.166, 0.166),
bbox=False)
train_loader, _, valid_loader, test_loader = \
get_augm_data_loaders_in_file(df_path, im_path, df_split,
train_batch_size=batch_size,
valid_batch_size=128,
custom_tranform=custom_tranform,
datasetclass=BoneAge,
fname_col='id',
n_fold=fold_idx, augm=False,
target_col='label')
encoder, _ = get_pretrained_models(model_name='resnet', pretrain=pretrain)
conv_config = {'kernel_size': 4, 'stride': 4, 'padding': 0}
decoder = InvResNet(block=BasicBlock, layers=[2, 2, 2, 2],
n_input_features=512, conv_config=conv_config,
normalize=False)
model = ConvolutionalAutoencoder(encoder, decoder, unpool_scale=8)
model = model.to(device)
# start from small learning rates
optimizer = torch.optim.Adam(model.parameters(), lr=1e-6,
weight_decay=alpha)
prepare_batch_ba = partial(prepare_batch_cae, key='image')
trainer = create_supervised_trainer(model, optimizer,
nn.MSELoss(),
device=device,
prepare_batch=prepare_batch_ba)
pbar = ProgressBar(persist=True)
pbar.attach(trainer, output_transform=lambda x: {'batch loss': x})
mlflow.set_experiment(exp_name)
with mlflow.start_run(run_name=run_name):
mlflow_logger = MLflowLogger()
mlflow_logger.log_params({
'seed': seed,
'batch_size': batch_size,
'model': model_name,
'weight_decay': alpha,
'fold_index': fold_idx,
'pytorch version': torch.__version__,
'ignite version': ignite.__version__,
'cuda version': torch.version.cuda,
'device name': torch.cuda.get_device_name(0)
})
lf = FastaiLRFinder()
to_save = {'model': model, 'optimizer': optimizer}
with lf.attach(trainer,
to_save,
end_lr=end_lr,
diverge_th=diverge_th) as trainer_with_lr_finder:
trainer_with_lr_finder.run(train_loader)
lf_log = pd.DataFrame(lf.get_results())
lf_log.to_csv(f'./temp/lf_log_{exp_name}.csv', index=False)
mlflow_logger.log_artifact(f'./temp/lf_log_{exp_name}.csv')
fig, ax = plt.subplots()
ax.plot(lf_log.lr[:-1], lf_log.loss[:-1])
ax.set_xscale('log')
ax.set_xlabel('Learning rate')
ax.set_ylabel('Loss')
ax.set_title(f'Suggestion from finder: {lf.lr_suggestion()}')
fig.savefig(f'./temp/lr_finder_{exp_name}.png', dpi=600)
plt.close(fig)
mlflow_logger.log_artifact(f'./temp/lr_finder_{exp_name}.png')
def run_cae(batch_size=64, lr=3e-4, alpha=1e-3, cae_loss=nn.MSELoss(),
model_name='resnet', epoch=50, pretrain=True, fold_idx=0,
device=torch.device('cpu'), exp_name='dataset_xxx',
run_name='model_xxx', seed=42):
"""Run the experiment with a given setting.
Args:
batch_size (int): Batch size.
lr (float): The value of the learning rate, possibly from lrfinder.
alpha (float): The value of weight decay (a.k.a. regularization).
cae_loss (nn.Module): The loss used to train the CAE.
model_name (str): The name of the backbone.
epoch (int): The number of training epochs.
pretrain (bool): Whether load the weights in pretrained models.
fold_idx (int): The index of the training/validation set.
device (torch.device or str): The device to load the models.
exp_name (str): The name of the experiments with a format of
dataset+xxx, which defines the experiment name inside MLflow.
run_name (str): The name of the run with a format of
[model_name]_cae, which defines the run name inside
MLflow.
seed (int): The number of the random seed to ensure the reproducibility.
Returns:
(nn.Module, int): a sota cnn backbone pretrained with metric learning
and the number of features of the backbone.
"""
np.random.seed(seed)
torch.manual_seed(seed)
df_path = Path('../boneage.csv')
im_path = Path(f'{str(Path.home())}/boneage/')
df_split = pd.read_csv('../boneage_idx_split.csv',
index_col=0)
custom_tranform = get_tranform(mean=(0.183, 0.183, 0.183),
std=(0.166, 0.166, 0.166),
bbox=False)
train_loader, _, valid_loader, test_loader = \
get_augm_data_loaders_in_file(df_path, im_path, df_split,
train_batch_size=batch_size,
valid_batch_size=128,
custom_tranform=custom_tranform,
datasetclass=BoneAge,
fname_col='id',
n_fold=fold_idx, augm=False,
target_col='label')
encoder, n_feature = get_pretrained_models(model_name='resnet',
pretrain=pretrain)
conv_config = {'kernel_size': 4, 'stride': 4, 'padding': 0}
decoder = InvResNet(block=BasicBlock, layers=[2, 2, 2, 2],
n_input_features=512, conv_config=conv_config,
normalize=False)
model = ConvolutionalAutoencoder(encoder, decoder, unpool_scale=8)
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr,
weight_decay=alpha)
# step_scheduler = StepLR(optimizer, step_size=int(epoch/2), gamma=0.1)
# scheduler = LRScheduler(step_scheduler)
def train_output_transform(x, y, y_pred, loss):
return {'y': y, 'y_pred': y_pred, 'loss': loss.item()}
prepare_batch_ba = partial(prepare_batch_cae, key='image')
trainer = create_supervised_trainer(model, optimizer,
cae_loss,
device=device,
prepare_batch=prepare_batch_ba,
output_transform=train_output_transform
)
# trainer.add_event_handler(Events.EPOCH_COMPLETED, scheduler)
pbar = ProgressBar(persist=True)
pbar.attach(trainer,
output_transform=lambda out: {'batch loss': out['loss']})
mse = MeanSquaredError() / (256 * 256 * 3)
# evaluators
val_metrics = {'mse': mse,
}
for name, metric in val_metrics.items():
metric.attach(trainer, name)
evaluator = create_supervised_evaluator(model,
metrics=val_metrics,
device=device,
prepare_batch=prepare_batch_ba
)
pbar.attach(evaluator)
mlflow.set_experiment(exp_name)
with mlflow.start_run(run_name=run_name):
mlflow_logger = MLflowLogger()
mlflow_logger.log_params({
'seed': seed,
'batch_size': batch_size,
'num_epoch': epoch,
'model': model_name,
'weight_decay': alpha,
'fold_index': fold_idx,
'pytorch version': torch.__version__,
'ignite version': ignite.__version__,
'cuda version': torch.version.cuda,
'device name': torch.cuda.get_device_name(0)
})
# handlers for evaluator
# note, this actually calls the evaluator
@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results(engine):
evaluator.run(valid_loader)
metrics = evaluator.state.metrics
pbar.log_message(f"Validation Results "
f"- Epoch: {engine.state.epoch} "
f"- Mean Square Error: {metrics['mse']:.4f} "
)
log_metrics = {f'validation {k}': v for k, v in metrics.items()}
mlflow_logger.log_metrics(log_metrics, step=engine.state.epoch)
def score_function(engine):
return -engine.state.metrics['mse']
handler = EarlyStopping(patience=15, score_function=score_function,
trainer=trainer)
evaluator.add_event_handler(Events.COMPLETED, handler)
to_save = {'model': model}
save_handler = Checkpoint(to_save=to_save,
save_handler=DiskSaver('./temp/cae_models',
create_dir=True,
require_empty=False),
n_saved=1,
filename_prefix='best',
score_function=score_function,
score_name="val_mse",
global_step_transform=global_step_from_engine(trainer)
)
evaluator.add_event_handler(Events.COMPLETED, save_handler)
def load_best_model(engine):
to_load = {'model': model}
last_checkpoint_fp = f'./temp/cae_models/{save_handler.last_checkpoint}'
print(last_checkpoint_fp)
checkpoint = torch.load(last_checkpoint_fp, map_location=device)
Checkpoint.load_objects(to_load=to_load, checkpoint=checkpoint)
logger.info('The best model on validation is reloaded.')
###
n_images = 6
fig, axes = plt.subplots(nrows=2, ncols=n_images,
sharex=True, sharey=True,
figsize=(15, 2.5))
img_examples = next(iter(test_loader))['image'][:n_images]
img_examples = img_examples.to(device, dtype=torch.float)
orig_images = img_examples
model.eval()
with torch.no_grad():
decoded_images = model(img_examples)
# un-normalize images
orig_images.mul_(0.166).add_(0.183)
decoded_images.mul_(0.166).add_(0.183)
for i in range(n_images):
for ax, img in zip(axes, [orig_images, decoded_images]):
curr_img = img[i].detach().to(torch.device('cpu'))
ax[i].imshow(curr_img.permute(1, 2, 0))
# avoid horizontal/vertical white lines shown in images
ax[i].set_axis_off()
save_fp = f'./temp/cae_models/' \
f'{save_handler.last_checkpoint[:-3]}.pdf'
fig.savefig(save_fp, dpi=600)
plt.close(fig)
mlflow_logger.log_artifact(save_fp)
trainer.add_event_handler(Events.COMPLETED, load_best_model)
# handlers for trainer
@trainer.on(Events.EPOCH_COMPLETED(every=10))
def log_training_results(engine):
metrics = engine.state.metrics
pbar.log_message(f"Training Set "
f"- Epoch: {engine.state.epoch} "
f"- Mean Square Error: {metrics['mse']:.4f} "
)
#######################
n_images = 6
fig, axes = plt.subplots(nrows=2, ncols=n_images,
sharex=True, sharey=True,
figsize=(15, 2.5))
img_examples = next(iter(test_loader))['image'][:n_images]
img_examples = img_examples.to(device, dtype=torch.float)
orig_images = img_examples
model.eval()
with torch.no_grad():
decoded_images = model(img_examples)
# un-normalize images
orig_images.mul_(0.166).add_(0.183)
decoded_images.mul_(0.166).add_(0.183)
for i in range(n_images):
for ax, img in zip(axes, [orig_images, decoded_images]):
curr_img = img[i].detach().to(torch.device('cpu'))
ax[i].imshow(curr_img.permute(1, 2, 0))
# avoid horizontal/vertical white lines shown in images
ax[i].set_axis_off()
save_fp = f'./temp/cae_models/epoch_{engine.state.epoch}.pdf'
fig.savefig(save_fp, dpi=600)
plt.close(fig)
mlflow_logger.log_artifact(save_fp)
########################
# log training loss at each iteration
mlflow_logger.attach_output_handler(trainer,
event_name=Events.ITERATION_COMPLETED,
tag='training',
output_transform=lambda out: {
'batch_loss': out['loss']}
)
# setup `global_step_transform=global_step_from_engine(trainer)` to
# take the epoch of the `trainer` instead of `train_evaluator`.
mlflow_logger.attach_output_handler(trainer,
event_name=Events.EPOCH_COMPLETED,
tag='training',
metric_names=['mse',]
)
# Attach the logger to the trainer to log optimizer's parameters,
# e.g. learning rate at each iteration
mlflow_logger.attach_opt_params_handler(trainer,
event_name=Events.ITERATION_STARTED,
optimizer=optimizer,
param_name='lr'
)
_ = trainer.run(train_loader, max_epochs=epoch)
return model, n_feature
if __name__ == '__main__':
sd = 42
dc = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
fold = 0
bs = 128
n_epoch = 200
pt = True
exp = 'boneage_final'
m_name = 'resnet'
#
# r_name = f'{m_name}_cae_lrf'
# lr_finder(batch_size=bs, alpha=1e-4, end_lr=10, diverge_th=2,
# model_name=m_name, pretrain=pt, fold_idx=fold, device=dc,
# exp_name=exp, run_name=r_name, seed=sd)
lrt = 3e-4 # this is from lr finder
r_name = f'{m_name}_cae'
for a in [1e-5, ]:
run_cae(batch_size=bs, lr=lrt, alpha=a, model_name=m_name,
epoch=n_epoch, pretrain=pt, fold_idx=fold, device=dc,
exp_name=exp, run_name=r_name, seed=sd)
gc.collect()