-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
369 lines (303 loc) · 11.2 KB
/
train.py
File metadata and controls
369 lines (303 loc) · 11.2 KB
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
import os
import subprocess
import time
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from torch.utils.tensorboard import SummaryWriter
from POLARIX import POLARIX, INPUT_FEATURE_SIZE
from utils import *
from earlystop import MonitorBestModelEarlyStopping
def evaluate_model(epoch, model, device, loader, writer):
eval_loss, probs, logits, labels, slide_ids = evaluate_loader(model, device, loader)
auc = roc_auc_score(labels, probs)
print(f"Eval epoch: {epoch}, loss: {eval_loss}, AUC: {auc:.4f}")
writer.add_scalar("Loss/eval", eval_loss, epoch)
writer.add_scalar("auc/eval", auc, epoch)
return eval_loss, auc, probs, logits, labels, slide_ids
def train_one_epoch(epoch, model, device, train_loader, optimizer, writer, loss_fn):
model.train()
epoch_start_time = time.time()
train_loss = 0.0
probs = np.zeros(len(train_loader))
logits = np.zeros(len(train_loader))
labels = np.zeros(len(train_loader))
slide_ids = []
batch_start_time = time.time()
for batch_idx, (data, label, slide_id) in enumerate(train_loader):
data_load_duration = time.time() - batch_start_time
data, label = data.to(device), label.to(device).float()
slide_id = slide_id[0]
logit, Y_prob, _, _ = model(data)
logit = logit.squeeze(dim=1)
Y_prob = Y_prob.squeeze(dim=1)
loss = loss_fn(logit, label)
train_loss += loss.item()
probs[batch_idx] = Y_prob.detach().cpu().item()
logits[batch_idx] = logit.detach().cpu().item()
labels[batch_idx] = label.cpu().item()
slide_ids.append(slide_id)
loss.backward()
optimizer.step()
optimizer.zero_grad()
batch_duration = time.time() - batch_start_time
batch_start_time = time.time()
writer.add_scalar("duration/data_load", data_load_duration, epoch)
writer.add_scalar("duration/batch", batch_duration, epoch)
epoch_duration = time.time() - epoch_start_time
print(f"Finished training on epoch {epoch} in {epoch_duration:.2f}s")
train_loss /= len(train_loader)
auc = roc_auc_score(labels, probs)
print(f"Train epoch: {epoch}, loss: {train_loss}, AUC: {auc:.4f}")
writer.add_scalar("duration/epoch", epoch_duration, epoch)
writer.add_scalar("LR", get_lr(optimizer), epoch)
writer.add_scalar("Loss/train", train_loss, epoch)
writer.add_scalar("auc/train", auc, epoch)
return probs, logits, labels, slide_ids
def run_train_eval_loop(
train_loader,
val_loader,
loss_fn,
hparams,
run_id,
hpset,
device,
output_dir,
):
output_dir = os.path.abspath(output_dir)
tensorboard_dir = os.path.join(output_dir, "tensorboard", run_id)
os.makedirs(tensorboard_dir, exist_ok=True)
writer = SummaryWriter(tensorboard_dir)
print(f"Using fixed input feature size: {INPUT_FEATURE_SIZE}")
model = POLARIX(
precompression_layer=hparams["precompression_layer"],
feature_size_comp=hparams["feature_size_comp"],
feature_size_attn=hparams["feature_size_attn"],
feature_size_comp_post=hparams["feature_size_comp_post"],
dropout=True,
p_dropout_fc=hparams["p_dropout_fc"],
p_dropout_atn=hparams["p_dropout_atn"],
).to(device)
print_model(model)
optimizer = torch.optim.Adam(
filter(lambda p: p.requires_grad, model.parameters()),
lr=hparams["initial_lr"],
weight_decay=hparams["weight_decay"],
)
if hparams["milestones"] == "cos":
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=hparams["max_epochs"], eta_min=0.0, last_epoch=-1
)
else:
milestones = [int(x) for x in hparams["milestones"].split(",")]
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=milestones, gamma=hparams["gamma_lr"]
)
monitor_tracker = MonitorBestModelEarlyStopping(
patience=hparams["earlystop_patience"],
min_epochs=hparams["earlystop_min_epochs"],
saving_checkpoint=True,
hpset=hpset,
output_dir=output_dir,
)
platt_model = LogisticRegression(fit_intercept=True, solver="lbfgs")
for epoch in range(hparams["max_epochs"]):
# Train for one epoch
probs_train, logits_train, labels_train, slide_ids_train = train_one_epoch(
epoch, model, device, train_loader, optimizer, writer, loss_fn
)
# Apply Platt scaling to training predictions
platt_model.fit(logits_train.reshape(-1, 1), labels_train)
calprobs_train = platt_model.predict_proba(logits_train.reshape(-1, 1))[:, 1]
preds_train = pd.DataFrame(probs_train, columns=[f"prob"])
preds_train["prob calibrated"] = calprobs_train
preds_train["logit"] = logits_train
preds_train["label"] = labels_train
preds_train["slide_id"] = slide_ids_train
# Evaluate the model
eval_loss, eval_auc, probs, logits, labels, slide_ids = evaluate_model(
epoch, model, device, val_loader, writer
)
# Apply Platt scaling to validation predictions
calprobs = platt_model.predict_proba(logits.reshape(-1, 1))[:, 1]
preds_df = pd.DataFrame(probs, columns=[f"prob"])
preds_df["prob calibrated"] = calprobs
preds_df["logit"] = logits
preds_df["label"] = labels
preds_df["slide_id"] = slide_ids
# Check for early stopping
monitor_tracker(
epoch, eval_loss, eval_auc, model, platt_model, preds_df, preds_train
)
scheduler.step()
if monitor_tracker.early_stop:
print(
f"Early stop criterion reached. Broke off training loop after epoch {epoch}."
)
break
runs_history = {
"run_id": run_id,
"best_epoch_loss": monitor_tracker.best_epoch_loss,
"best_evalLoss": monitor_tracker.eval_loss_min,
"best_epoch_AUC": monitor_tracker.best_epoch,
"best_AUC_score": monitor_tracker.best_opt_metric_score,
**hparams,
}
with open(f"../runs_final.txt", "a") as filehandle:
for _, value in runs_history.items():
filehandle.write("%s;" % value)
filehandle.write("\n")
writer.close()
def main(args):
set_seed()
if not torch.cuda.is_available():
raise Exception(
"No CUDA device available. Training without CUDA-acceleration is not recommended."
)
df = pd.read_csv(args.manifest)
print(f"Read {args.manifest} dataset containing {len(df)} samples")
try:
training_set = df[df["split"] == "train"]
validation_set = df[df["split"] == "val"]
except KeyError:
raise Exception(f"Required column 'split' does not exist in {args.manifest}")
train_split = FeatureBags(
df=training_set,
data_dir=args.data_dir,
)
val_split = FeatureBags(
df=validation_set,
data_dir=args.data_dir,
)
# Generate a unique run ID
try:
git_sha = (
subprocess.check_output(["git", "describe", "--always"])
.strip()
.decode("utf-8")
)
except (
subprocess.CalledProcessError,
FileNotFoundError,
NotADirectoryError,
OSError,
):
git_sha = "nogit"
train_run_id = f"{git_sha}-{time.strftime('%Y%m%d-%H%M%S')}"
print(f"=> Git SHA {train_run_id}")
print(f"=> Training on {len(train_split)} samples")
print(f"=> Validating on {len(val_split)} samples")
# Base hyperparameters
base_hparams = dict(
data_dir=os.path.dirname(args.data_dir),
sampling_method="random",
max_epochs=100,
earlystop_patience=30,
earlystop_min_epochs=30,
initial_lr=0.00003,
milestones="5, 15, 25",
gamma_lr=0.1,
weight_decay=0.00001,
# Model architecture parameters. See model class for details.
precompression_layer=False,
feature_size_comp=512,
feature_size_attn=256,
feature_size_comp_post=128,
p_dropout_fc=0.25,
p_dropout_atn=0.25,
)
# Hyperparameter sets for experimentation
hparam_sets = [
{**base_hparams},
{
**base_hparams,
"p_dropout_fc": 0.50,
"p_dropout_atn": 0.50,
"weight_decay": 0.0001,
"initial_lr": 0.0001,
"sampling_method": "balanced",
},
{**base_hparams, "class_weighting": True},
{
**base_hparams,
"p_dropout_fc": 0.50,
"p_dropout_atn": 0.50,
"weight_decay": 0.0001,
"initial_lr": 0.0001,
"class_weighting": True,
},
]
hps = hparam_sets[args.hp]
run_id = f"{train_run_id}_hp{args.hp}"
print(f"Running train-eval loop {args.hp} for {run_id}")
print(hps)
train_loader = get_train_loader(
train_split,
hps["sampling_method"],
args.workers,
)
val_loader = get_val_loader(val_split, args.workers)
device = torch.device("cuda")
# Set up loss function with class weighting if specified
loss_function = nn.BCEWithLogitsLoss()
if hps.get("class_weighting", False):
labels_tensor = torch.tensor(
train_split.slide_df["label"].values, dtype=torch.float32
)
num_pos = int((labels_tensor == 1).sum().item())
num_neg = int((labels_tensor == 0).sum().item())
if num_pos == 0 or num_neg == 0:
raise ValueError(
"Class weighting requested but only one class is present in the training split."
)
pos_weight_value = num_neg / num_pos
print(f"Using class weighting with pos_weight: {pos_weight_value:.2f}")
pos_weight = torch.tensor(pos_weight_value, dtype=torch.float32, device=device)
loss_function = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
# Start the training and evaluation loop
run_train_eval_loop(
train_loader=train_loader,
val_loader=val_loader,
loss_fn=loss_function,
hparams=hps,
run_id=run_id,
hpset=args.hp,
device=device,
output_dir=args.output_dir,
)
print("Finished training.")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Training script")
parser.add_argument(
"--manifest",
type=str,
help="CSV file listing all slides, their labels, and which split (train/test/val) they belong to.",
)
parser.add_argument(
"--data_dir",
type=str,
help="Directory where all *_features.h5 files are stored",
)
parser.add_argument(
"--workers",
help="The number of workers to use for the data loaders.",
type=int,
default=4,
)
parser.add_argument(
"--hp",
type=int,
required=True,
)
parser.add_argument(
"--output_dir",
type=str,
default="./runs/final",
help="Base directory where checkpoints, predictions, and TensorBoard logs are stored.",
)
args = parser.parse_args()
main(args)