-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
282 lines (232 loc) · 13 KB
/
main.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
#import torch.cuda
from dataset import DatasetCL
from torch.utils.data import DataLoader, RandomSampler, WeightedRandomSampler, SequentialSampler
from losses import WSPContrastiveLoss
from torch.nn import CrossEntropyLoss
import itertools
import models.network as model_
from sampler import CustomSampler
import argparse
from pytorch_lightning.loggers.tensorboard import TensorBoardLogger
from config import Config
from wspContrastiveLearning import wspContrastiveModel
from sklearn.model_selection import KFold, StratifiedKFold, LeaveOneOut, train_test_split
import pytorch_lightning as pl
import pandas as pd
from pytorch_lightning.callbacks import ModelCheckpoint, Callback
from os.path import join
from sklearn.metrics import confusion_matrix
import numpy as np
import torch
from pytorch_lightning.callbacks import LearningRateMonitor
import warnings
if __name__ == "__main__":
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=str, choices=["pretraining", "finetuning", "autoencoder"], required=True,
help="Set the training mode. Do not forget to configure config.py accordingly!")
parser.add_argument("--lr", type=float)
parser.add_argument("--weight_decay", type=float)
parser.add_argument("--cross_val", dest="cross_val", action="store_true")
parser.add_argument("--no-cross_val", dest="cross_val", action="store_false")
parser.add_argument("--n_fold", type=int)
parser.add_argument("--encoder", type=str)
parser.add_argument("--n_layer", type=int)
parser.add_argument("--pretrained_path", default=None, type=str)
parser.add_argument("--sigma", default=0.5, type=float)
parser.add_argument("--temperature", type=float)
parser.add_argument("--label_name", default="label", type=str)
parser.add_argument("--num_classes", default=2, type=int)
parser.add_argument("--kernel", default='rbf', type=str)
parser.add_argument("--max_epochs", default=40, type=int)
parser.add_argument("--batch_size", default=64, type=int)
parser.add_argument("--rep_dim", default=256, type=int)
parser.add_argument("--hidden_dim", default=128, type=int)
parser.add_argument("--output_dim", default=64, type=int)
parser.add_argument("--pretrained", dest="pretrained", action="store_true")
args = parser.parse_args()
mode = args.mode
lr = args.lr
weight_decay = args.weight_decay
cross_val = args.cross_val
n_fold = args.n_fold
encoder = args.encoder
pretrained_path = args.pretrained_path
sigma = args.sigma
temperature = args.temperature
label_name = args.label_name
num_classes = args.num_classes
kernel = args.kernel
max_epochs = args.max_epochs
batch_size = args.batch_size
pretrained = args.pretrained
rep_dim = args.rep_dim
hidden_dim = args.hidden_dim
output_dim = args.output_dim
n_layer = args.n_layer
config = Config(mode=mode,
lr=lr,
weight_decay=weight_decay,
cross_val=cross_val,
n_fold=n_fold,
encoder=encoder,
n_layer=n_layer,
dir=dir,
pretrained_path=pretrained_path,
sigma=sigma,
temperature=temperature,
label_name=label_name,
num_classes=num_classes,
kernel=kernel,
max_epochs=max_epochs,
batch_size=batch_size,
rep_dim=rep_dim,
hidden_dim=hidden_dim,
output_dim=output_dim,
pretrained=pretrained)
print('label name',config.label_name)
if config.cross_val:
df = pd.read_csv(join(config.path_to_data, '1_subjects_label.csv'), delimiter=",").drop(['Unnamed: 0'], axis=1)
skf = StratifiedKFold(n_splits=config.n_fold)
for j, (train_index, val_index) in enumerate(skf.split(df['subject'],df['class'])):#enumerate(skf.split(df['subject'],df['label'])):
train_index_ = df.loc[train_index, 'subject']
train_labs = df.loc[train_index, config.label_name]
val_index_ = df.loc[val_index, 'subject']
val_labs = df.loc[val_index, config.label_name]
train = [1 if x in list(train_index_) else 0 for x in df['subject']]
val = [1 if x in list(val_index_) else 0 for x in df['subject']]
df['train_set'] = train
df['val_set'] = val
## at each fold, we download the dataframe again.
df.to_csv(join(config.lght_dir, '1_subjects_label_cv.csv'))
df.to_csv(join(config.lght_dir, '1_subjects_label_cv_'+str(j)+'.csv'))
dataset_train = DatasetCL(config, training=True)
dataset_val = DatasetCL(config, validation=True)
#sampler = WeightedRandomSampler(dataset_train.samples_weight.type('torch.DoubleTensor'),
# len(dataset_train))
indices = {} # indices of all the slices for each volume (patient) in the dataset
for z in dataset_train.volumes:
indices[z] = [i for i, x in enumerate(dataset_train.volumes) if x == z]
k = len(dataset_train)
sampler = CustomSampler(dataset_train, config.batch_size, indices,
weights=dataset_train.samples_weight.numpy(),
k=k)
print('Weights assigned to each class', dataset_train.weight)
loader_train = DataLoader(dataset_train, batch_size=config.batch_size,
sampler=sampler,
collate_fn=dataset_train.collate_fn,
pin_memory=config.pin_mem,
num_workers=config.nb_cpu,
drop_last=False)
loader_val = DataLoader(dataset_val, batch_size=config.batch_size,
sampler=SequentialSampler(dataset_val),
collate_fn=dataset_val.collate_fn,
pin_memory=config.pin_mem,
num_workers=config.nb_cpu,
drop_last=False)
print('Ready to download the model!')
print('Pretrained on ImageNet?', config.pretrained)
net = model_.network(mode="classifier",
net=config.encoder,
pretrained=config.pretrained,
n_layer=config.n_layer,
num_classes=config.num_classes,
rep_dim=config.rep_dim,
hidden_dim=config.hidden_dim)
print('Network downloaded!')
loss = CrossEntropyLoss()
lr_logger = LearningRateMonitor(logging_interval='epoch')
# Folder hack
tb_logger = TensorBoardLogger(save_dir=config.lght_dir,
version=f'fold_{j + 1}')
checkpoint_callback = ModelCheckpoint(monitor="val_auc",
save_top_k=1,
every_n_epochs=1,
save_last=True,
mode="max",
dirpath=tb_logger.log_dir,
filename='best')
trainer = pl.Trainer(gpus=([0]), default_root_dir=config.lght_dir, max_epochs=config.max_epochs,
callbacks=[checkpoint_callback, lr_logger],
val_check_interval=config.val_rate,
amp_backend="native", precision=16,
reload_dataloaders_every_epoch=False,
num_sanity_val_steps=-1,
logger=tb_logger,
)
model = wspContrastiveModel(net, loss, config, dataset_train, dataset_val, config.mode)
# we check the number of trainable parameters
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
model_params = sum([np.prod(p.size()) for p in model_parameters])
print('Number of trainable parameters in the whole model: ' + str(model_params))
config.cv_fold = j
trainer.fit(model, loader_train, loader_val)
else:
dataset_train = DatasetCL(config, training=True)
dataset_val = DatasetCL(config, validation=True)
print('ok')
indices = {} # indices of each volume (patient) in the dataset
for z in dataset_train.volumes:
indices[z] = [i for i, x in enumerate(dataset_train.volumes) if x == z]
sampler = CustomSampler(dataset_train, config.batch_size, indices,
weights=dataset_train.samples_weight.numpy(),
k=10000)
print('Weights assigned to each class', dataset_train.weight)
loader_train = DataLoader(dataset_train, batch_size=config.batch_size,
sampler=sampler,
collate_fn=dataset_train.collate_fn,
pin_memory=config.pin_mem,
num_workers=config.nb_cpu,
drop_last=False)
loader_val = DataLoader(dataset_val, batch_size=config.batch_size,
sampler=SequentialSampler(dataset_val),
collate_fn=dataset_val.collate_fn,
pin_memory=config.pin_mem,
num_workers=config.nb_cpu,
drop_last=False)
if config.mode == "pretraining":
net = model_.network(mode="encoder",
net=config.encoder,
pretrained=config.pretrained,
n_layer=config.n_layer,
rep_dim=config.rep_dim,
hidden_dim=config.hidden_dim)
loss = WSPContrastiveLoss(config=config,
temperature=config.temperature,
kernel=config.kernel,
sigma=config.sigma,
return_logits=True)
elif config.mode == "finetuning":
net = model_.network(mode="classifier",
net=config.encoder,
pretrained=config.pretrained,
n_layer=config.n_layer,
num_classes=config.num_classes,
rep_dim=config.rep_dim,
hidden_dim=config.hidden_dim,
output_dim=config.output_dim)
loss = CrossEntropyLoss()
lr_logger = LearningRateMonitor(logging_interval='epoch')
tb_logger = TensorBoardLogger(save_dir=config.lght_dir)
checkpoint_callback = ModelCheckpoint(monitor="val_auc",
save_top_k=1,
every_n_epochs=1,
save_last=True,
mode="max",
dirpath=tb_logger.log_dir,
filename='best')
trainer = pl.Trainer(gpus=([0]), default_root_dir=config.lght_dir, max_epochs=config.max_epochs,
callbacks=[checkpoint_callback, lr_logger],
val_check_interval=config.val_rate,
reload_dataloaders_every_epoch=False,
num_sanity_val_steps=-1,
logger=tb_logger,
)
# The monitor argument name corresponds to the scalar value that you log
# when using the self.log method within the LightningModule hooks.
model = wspContrastiveModel(net, loss, config, dataset_train, dataset_val, config.mode)
# we check the number of trainable parameters
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
model_params = sum([np.prod(p.size()) for p in model_parameters])
print('Number of trainable parameters in the whole model: ' + str(model_params))
trainer.fit(model, loader_train, loader_val)