forked from hannesk95/DINOV2GNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
308 lines (256 loc) · 14.8 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
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
from dataset_mednist import MedMNIST3D
from dataset_custom import GraphNSCLC3D, GraphRadcure3D, Sarcoma
from param_configurator import ParamConfigurator
import mlflow
import torch
import os
from tqdm import tqdm
from utils import FocalLoss, f1_loss
import numpy as np
from torch.utils.data import DataLoader
from utils import set_seed, save_conda_env
from model import GNN, MLP, LSTM #, Classifier
from sklearn.metrics import roc_auc_score, balanced_accuracy_score, accuracy_score
from datetime import datetime
from torch_geometric.loader import DataLoader as GeometricDataLoader
def train(config) -> None:
save_conda_env(config)
mlflow.log_params(config.__dict__)
match config.dataset:
case "nodule" | "synapse" | "adrenal" | "vessel" | "organ" | "fracture":
train_dataset = MedMNIST3D(config=config, mode='train')
val_dataset = MedMNIST3D(config=config, mode='val')
test_dataset = MedMNIST3D(config=config, mode='test')
case "nsclc":
train_dataset = GraphNSCLC3D(config=config, mode='train')
val_dataset = GraphNSCLC3D(config=config, mode='val')
test_dataset = GraphNSCLC3D(config=config, mode='test')
case "radcure":
train_dataset = GraphRadcure3D(config=config, mode='train')
val_dataset = GraphRadcure3D(config=config, mode='val')
test_dataset = GraphRadcure3D(config=config, mode='test')
case "sts":
train_dataset = Sarcoma(config=config, mode='train')
val_dataset = Sarcoma(config=config, mode='val')
test_dataset = Sarcoma(config=config, mode='test')
case _:
raise NotImplementedError(f"Given dataset '{config.dataset}' not implemented!")
match config.model_name:
case "GNN":
model = GNN(config=config).to(config.device)
train_loader = GeometricDataLoader(dataset=train_dataset, batch_size=config.batch_size, shuffle=True)
val_loader = GeometricDataLoader(dataset=val_dataset, batch_size=config.batch_size, shuffle=False)
test_loader = GeometricDataLoader(dataset=test_dataset, batch_size=config.batch_size, shuffle=False)
case "MLP":
# model = Classifier(config=config).to(config.device)
model = MLP(config=config).to(config.device)
train_loader = DataLoader(dataset=train_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=True, drop_last=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False, drop_last=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False, drop_last=True)
case "LSTM":
model = LSTM(config=config).to(config.device)
train_loader = DataLoader(dataset=train_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=True, drop_last=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False, drop_last=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False, drop_last=True)
# criterion = torch.nn.CrossEntropyLoss().to(config.device)
# criterion = torch.nn.CrossEntropyLoss(weight=train_dataset.class_weights).to(config.device)
# criterion = FocalLoss(gamma=config.ce_gamma, reduction='mean').to(config.device)
criterion = FocalLoss(weight=train_dataset.class_weights, gamma=config.ce_gamma, reduction='mean').to(config.device)
match config.optimizer:
case "Adam":
optimizer = torch.optim.Adam(model.parameters(), lr=config.learning_rate)
case "AdamW":
optimizer = torch.optim.AdamW(model.parameters(), lr=config.learning_rate)
case "SGD":
optimizer = torch.optim.SGD(model.parameters(), lr=config.learning_rate, momentum=config.momentum,
nesterov=config.nesterov, weight_decay=config.weight_decay)
case _:
raise NotImplementedError(f"Optimizer not implemented!")
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=config.scheduler_step, gamma=config.scheduler_gamma)
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
mlflow.log_param("num_parameters", total_params)
#######################
# Training ############
#######################
# best_val_acc = float('-inf')
best_val_acc = 0
# best_val_auc = float('-inf')
best_val_auc = 0
for epoch in range(1, config.epochs+1):
train_loss_list = []
train_true_list = []
train_pred_list = []
train_score_list = []
lr = scheduler.get_last_lr()[0]
model.train()
with tqdm(train_loader, unit="batch") as tepoch:
for data in tepoch:
tepoch.set_description(f"[Training] Epoch {epoch:03d} | {config.epochs}")
inputs, labels = data
match config.model_name:
case "GNN":
inputs = inputs.to(config.device)
labels = labels.view(-1).to(config.device)
case "MLP":
# inputs = inputs.view(-1, inputs.shape[-1])
inputs = inputs.to(torch.float32).to(config.device)
# labels = labels.view(-1)
labels = labels.to(torch.long).to(config.device)
case "LSTM":
inputs = inputs.to(config.device)
labels = labels.view(-1).to(config.device)
case _:
inputs = inputs.view(-1, 3, 224, 224)
inputs = inputs.float().to(config.device)
labels = labels.view(-1)
labels = labels.long().to(config.device)
optimizer.zero_grad()
out = model(inputs)
# if config.model_name == "MLP":
# match config.mlp_aggregation:
# case "mean":
# # out = torch.concat([torch.mean(out[(i*config.num_slices):(i+1)*config.num_slices, :], dim=0).view(1, -1) for i in range(config.num_slices)])
# out = torch.concat([torch.mean(temp, dim=0).view(1, -1) for temp in out], dim=0)
# # labels = labels[::config.num_slices]
# labels = labels.view(-1)
# case "max":
# out = torch.concat([torch.max(out[(i*config.num_slices):(i+1)*config.num_slices, :], dim=0).values.view(1, -1) for i in range(config.num_slices)])
# # labels = labels[::config.num_slices]
# labels = labels.view(-1)
# case _:
# out = out
# labels = labels
labels = labels.view(-1)
if config.n_classes > 2:
score = torch.softmax(out, dim=1)
else:
score = torch.softmax(out, dim=1)[:, -1]
# f1_loss_value = f1_loss(labels, score)
loss = criterion(out, labels) #+ f1_loss_value
loss.backward()
optimizer.step()
pred = torch.max(out, dim=1).indices
train_loss_list.append(loss.detach().cpu().item())
train_true_list.extend(labels.detach().cpu().numpy())
train_pred_list.extend(pred.detach().cpu().numpy())
train_score_list.extend(score.detach().cpu().numpy())
train_loss = np.mean(train_loss_list)
train_bacc = balanced_accuracy_score(train_true_list, train_pred_list)
train_auc = roc_auc_score(train_true_list, train_score_list, multi_class='ovo')
train_acc = accuracy_score(train_true_list, train_pred_list)
mlflow.log_metric("train_acc", train_acc, step=epoch)
mlflow.log_metric("train_bacc", train_bacc, step=epoch)
mlflow.log_metric("train_auc", train_auc, step=epoch)
mlflow.log_metric("train_loss", train_loss, step=epoch)
mlflow.log_metric("learning_rate", lr, step=epoch)
scheduler.step()
#######################
# Validation ##########
#######################
eval_loss, eval_bacc, eval_auc, eval_acc = evaluate(config, model, val_loader, criterion)
mlflow.log_metric("val_bacc", eval_bacc, step=epoch)
mlflow.log_metric("val_acc", eval_acc, step=epoch)
mlflow.log_metric("val_auc", eval_auc, step=epoch)
mlflow.log_metric("val_loss", eval_loss, step=epoch)
if eval_acc >= best_val_acc:
best_val_acc = eval_acc
torch.save(model.state_dict(), os.path.join(config.run_dir, "best_model.pt"))
mlflow.log_artifact(os.path.join(config.run_dir, "best_model.pt"))
#################
# Test ##########
#################
model.load_state_dict(torch.load(os.path.join(config.run_dir, "best_model.pt")))
eval_loss, eval_bacc, eval_auc, eval_acc = evaluate(config, model, test_loader, criterion)
mlflow.log_metric("test_bacc", eval_bacc, step=epoch)
mlflow.log_metric("test_acc", eval_acc, step=epoch)
mlflow.log_metric("test_auc", eval_auc, step=epoch)
mlflow.log_metric("test_loss", eval_loss, step=epoch)
def evaluate(config, model, dataloader, criterion) -> None:
eval_loss_list = []
eval_true_list = []
eval_pred_list = []
eval_score_list = []
model.eval()
with torch.no_grad():
with tqdm(dataloader, unit="batch") as tepoch:
for data in tepoch:
tepoch.set_description(f"[Validation]")
inputs, labels = data
match config.model_name:
case "GNN":
inputs = inputs.to(config.device)
labels = labels.view(-1).to(config.device)
case "MLP":
# inputs = inputs.view(-1, inputs.shape[-1])
inputs = inputs.to(torch.float32).to(config.device)
# labels = labels.view(-1)
labels = labels.to(torch.long).to(config.device)
case "LSTM":
inputs = inputs.to(config.device)
labels = labels.view(-1).to(config.device)
case _:
inputs = inputs.view(-1, 3, 224, 224)
inputs = inputs.float().to(config.device)
labels = labels.view(-1)
labels = labels.long().to(config.device)
out = model(inputs)
# if config.model_name == "MLP":
# match config.mlp_aggregation:
# case "mean":
# # out = torch.concat([torch.mean(out[(i*config.num_slices):(i+1)*config.num_slices, :], dim=0).view(1, -1) for i in range(config.num_slices)])
# out = torch.concat([torch.mean(temp, dim=0).view(1, -1) for temp in out], dim=0)
# # labels = labels[::config.num_slices]
# labels = labels.view(-1)
# case "max":
# out = torch.concat([torch.max(out[(i*config.num_slices):(i+1)*config.num_slices, :], dim=0).values.view(1, -1) for i in range(config.num_slices)])
# # labels = labels[::config.num_slices]
# labels = labels.view(-1)
# case _:
# out = out
# labels = labels
labels = labels.view(-1)
loss = criterion(out, labels)
pred = torch.max(out, dim=1).indices
if config.n_classes > 2:
score = torch.softmax(out, dim=1)
else:
score = torch.softmax(out, dim=1)[:, -1]
eval_loss_list.append(loss.detach().cpu().item())
eval_true_list.extend(labels.detach().cpu().numpy())
eval_pred_list.extend(pred.detach().cpu().numpy())
eval_score_list.extend(score.detach().cpu().numpy())
eval_loss = np.mean(eval_loss_list)
eval_bacc = balanced_accuracy_score(eval_true_list, eval_pred_list)
eval_auc = roc_auc_score(eval_true_list, eval_score_list, multi_class='ovo')
eval_acc = accuracy_score(eval_true_list, eval_pred_list)
return eval_loss, eval_bacc, eval_auc, eval_acc
if __name__ == "__main__":
for dataset in ['nodule', 'fracture', 'adrenal', 'vessel', 'synapse', 'organ']:
for model_name in ['MLP']:
for gnn_type in ['GATConv']:#, 'SAGEConv', 'GCNConv']:
for topology in ["line"]:#, "fully", "custom", "star", "euclidean", "manhattan", "chebyshev", "cosine", "pearson"]:
for k in [3]:#, 5, 7]:
for seed in [0, 28, 42]:
if (topology in ["line", "fully", "custom", "star"]) & (k > 3):
print("skip run!")
continue
config = ParamConfigurator()
config.dataset = dataset
config.model_name = model_name
config.gnn_type = gnn_type
config.topology = topology
config.k = k
config.seed = seed
match config.dataset:
case "fracture":
config.n_classes = 3
case "organ":
config.n_classes = 11
case _:
config.n_classes = 2
set_seed(config.seed)
# mlflow.set_experiment(f'{config.dataset}_{config.model_name}')
mlflow.set_experiment(f'{config.model_name}')
date = str(datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
with mlflow.start_run(run_name=date, log_system_metrics=True):
train(config)