-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpml_dl.py
356 lines (290 loc) · 14.5 KB
/
pml_dl.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
#
# mDKL
#
# Copyright (c) Siemens AG, 2021
# Authors:
# Zhiliang Wu <[email protected]>
# License-Identifier: MIT
from pathlib import Path
import faiss
import numpy as np
import mlflow
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import ignite
from ignite.contrib.handlers import FastaiLRFinder, ProgressBar
from ignite.contrib.handlers.mlflow_logger import MLflowLogger
from ignite.metrics import Average
from ignite.engine import Events
from ignite.handlers import EarlyStopping
from pytorch_metric_learning import losses, miners, distances, reducers, testers
from pytorch_metric_learning.utils.accuracy_calculator import AccuracyCalculator
from data_utils import AugmBBoxDatasetAll, get_tranform, \
get_augm_data_loaders_in_file, prepare_batch
from logging_conf import logger
from model_utils import get_pretrained_models, create_metric_trainer
def lr_finder(batch_size=32, alpha=1e-4, end_lr=10, diverge_th=5,
model_name='resnet', pretrain=True, fold_idx=0, device='cpu',
exp_name='dataset_xxx', run_name='xmodel_metric_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)
df_path = Path('../DL_lung_0.5.csv')
im_path = Path(f'{str(Path.home())}/Key_DL/')
df_split = pd.read_csv('../DL_lung_0.5_idx_split.csv',
index_col=0)
custom_tranform = get_tranform(mean=(0.389, 0.389, 0.389),
std=(0.240, 0.240, 0.240),
bbox=True)
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=AugmBBoxDatasetAll,
fname_col='File_name',
n_fold=fold_idx, augm=False,
target_col='label')
model, n_feature = get_pretrained_models(model_name=model_name,
pretrain=pretrain)
# model = LinearModel(feature_extractor=backbone, num_features=n_feature,
# output_dim=dim_gp_in)
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-6,
weight_decay=alpha)
# pytorch-metric-learning stuff
distance = distances.LpDistance()
reducer = reducers.MeanReducer()
loss_func = losses.TripletMarginLoss(margin=0.2, distance=distance,
reducer=reducer)
mining_func = miners.TripletMarginMiner(margin=0.2, distance=distance,
type_of_triplets="semihard")
# pytorch-metric-learning stuff
train_output_transform = lambda x, y, embeddings, num_triplets, loss: loss.item()
trainer = create_metric_trainer(model, optimizer,
loss_func, mining_func,
device=device,
prepare_batch=prepare_batch,
output_transform=train_output_transform
)
pbar = ProgressBar(persist=True)
pbar.attach(trainer,
output_transform=lambda out: {'batch loss': out})
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_metric_learning(batch_size=32, lr=1e-3, alpha=1e-4,
model_name='resnet', epoch=1, pretrain=True,
fold_idx=0, device='cpu',
exp_name='dataset_xxx', run_name='model_metric',
seed=42):
"""Run the metric learning 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).
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]_metric, 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('../DL_lung_0.5.csv')
im_path = Path(f'{str(Path.home())}/Key_DL/')
df_split = pd.read_csv('../DL_lung_0.5_idx_split.csv',
index_col=0)
custom_tranform = get_tranform(mean=(0.389, 0.389, 0.389),
std=(0.240, 0.240, 0.240),
bbox=True)
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=AugmBBoxDatasetAll,
fname_col='File_name',
n_fold=fold_idx, augm=False,
target_col='label')
model, n_features = get_pretrained_models(model_name=model_name,
pretrain=pretrain)
# model = LinearModel(feature_extractor=backbone, num_features=n_feature,
# output_dim=dim_gp_in)
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr,
weight_decay=alpha)
# pytorch-metric-learning stuff
distance = distances.LpDistance()
reducer = reducers.MeanReducer()
loss_func = losses.TripletMarginLoss(margin=0.2, distance=distance,
reducer=reducer)
mining_func = miners.TripletMarginMiner(margin=0.2, distance=distance,
type_of_triplets='semihard')
accuracy_calculator = AccuracyCalculator(include=("mean_average_precision_at_r",), k=10)
# pytorch-metric-learning stuff
def train_output_transform(x, y, embeddings, num_triplets, loss):
return {'n_triplets': num_triplets, 'loss': loss.item()}
# prepare_batch_reshape = partial(prepare_batch, new_shape=(-1, 1))
trainer = create_metric_trainer(model, optimizer,
loss_func, mining_func,
device=device,
prepare_batch=prepare_batch,
output_transform=train_output_transform
)
train_metrics = {'loss': Average(output_transform=lambda x: x['loss']),
'n_triplets': Average(output_transform=lambda x: x['n_triplets']),
}
for name, metric in train_metrics.items():
metric.attach(trainer, name)
pbar = ProgressBar(persist=True)
pbar.attach(trainer,
output_transform=lambda out: {'batch loss': out['loss']})
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,
# 'gp_input_dim': dim_gp_in,
'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 trainer
@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(engine):
metrics = engine.state.metrics
pbar.log_message(f"Training Set "
f"- Epoch: {engine.state.epoch} "
f"- average loss: {metrics['loss']:.4f} "
f"- average n_triplets: {metrics['n_triplets']:.2f}"
)
@trainer.on(Events.EPOCH_COMPLETED)
def evaluate_model(engine):
data_and_label_getter = lambda x: (x['image'], x['target'])
tester = testers.BaseTester(reference_set="compared_to_self",
normalize_embeddings=True,
dataloader_num_workers=4,
data_device=device,
data_and_label_getter=data_and_label_getter)
model.eval()
embeddings, labels = tester.compute_all_embeddings(valid_loader,
model,
nn.Identity())
embeddings = tester.maybe_normalize(embeddings)
accuracies = accuracy_calculator.get_accuracy(embeddings,
embeddings,
np.squeeze(labels),
np.squeeze(labels),
True)
logger.info(f"Valid MAP10 ="
f" {accuracies['mean_average_precision_at_r']}")
engine.state.metrics['MAP10'] = accuracies['mean_average_precision_at_r']
mlflow.log_metric("validation MAP10",
accuracies['mean_average_precision_at_r'],
step=engine.state.epoch
)
def score_function(engine):
val_metric = engine.state.metrics['MAP10']
return val_metric
handler = EarlyStopping(patience=15, score_function=score_function,
trainer=trainer)
trainer.add_event_handler(Events.EPOCH_COMPLETED, handler)
mlflow_logger.attach_output_handler(trainer,
event_name=Events.EPOCH_COMPLETED,
tag='training',
metric_names=['loss',
'n_triplets',
]
)
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_features
if __name__ == '__main__':
sd = 42
dc = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
f = 0
bs = 128
n_epoch = 100
pt = True
exp = 'DL_lung_final'
m_name = 'resnet'
a = 1e-4
# r_name = f'{m_name}_metric_lrf'
# lr_finder(batch_size=bs, alpha=a, end_lr=10, diverge_th=2,
# model_name=m_name, pretrain=pt, fold_idx=f, device=dc,
# exp_name=exp, run_name=r_name, seed=sd)
lrt = 1e-4
r_name = f'{m_name}_metric'
_ = run_metric_learning(batch_size=bs, lr=lrt, alpha=a, model_name=m_name,
epoch=n_epoch, pretrain=pt, fold_idx=f,
exp_name=exp, run_name=r_name, device=dc, seed=sd)