-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_lightning_module.py
More file actions
318 lines (276 loc) · 11.4 KB
/
model_lightning_module.py
File metadata and controls
318 lines (276 loc) · 11.4 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
"""
This file contains the ONNX compatible LightningModule.
Author: Jonathan Renusch
Date: 2025-15-04
"""
import torch
import os
import pytorch_lightning as L
from sklearn.metrics import f1_score, roc_auc_score
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from torch.optim.lr_scheduler import (
ReduceLROnPlateau,
CosineAnnealingLR,
CosineAnnealingWarmRestarts,
LRScheduler,
)
from Classifier.utils.plot_functions import *
from Classifier.utils.utility_classes import FocalLoss
class ModelLightningModule(L.LightningModule):
def __init__(
self,
model: torch.nn.Module,
# train_dataset: Dataset = None,
comet_ml: bool = False,
learning_rate: float = 0.001,
batch_size: int = 512,
num_workers: int = 12,
lr_patience: int = 3,
metric_callbacks: str = "val_auc_epoch",
early_stopping_patience: int = 15,
weight_decay: float = 0.1,
momentum: float = 0.1,
optimizer_name: str = "AdamW",
lr_scheduler_name: str = "ReduceLROnPlateau",
loss_name: str = "BCEWithLogitsLoss",
efficiency: float = 0.99,
) -> None:
super().__init__()
self.model = model
self.criterion = torch.nn.BCELoss()
# self.train_dataset = train_dataset
self.lr = learning_rate
self.lr_patience = lr_patience
self.batch_size = batch_size
self.num_workers = num_workers
self.metric_callbacks = metric_callbacks
self.early_stopping_patience = early_stopping_patience
self.efficiency = efficiency
self.comet_ml = comet_ml
self.test_loss: list[torch.Tensor] = []
self.test_predictions: list[torch.Tensor] = []
self.test_labels: list[torch.Tensor] = []
self.val_loss: list[torch.Tensor] = []
self.val_predictions: list[torch.Tensor] = []
self.val_labels: list[torch.Tensor] = []
if "acc" in self.metric_callbacks:
self.callback_mode = "max"
elif "auc" in self.metric_callbacks:
self.callback_mode = "max"
elif "rejection_rate" in self.metric_callbacks:
self.callback_mode = "max"
elif "loss" in self.metric_callbacks:
self.callback_mode = "min"
else:
raise Exception(
"Having trouble finding callback mode 'min' or 'max'. \n \
Please give metric_calbacks argument containing string 'acc' or 'loss'."
)
self.optimizer_name = optimizer_name
self.loss_name = loss_name
self.lr_scheduler_name = lr_scheduler_name
self.momentum = momentum
self.weight_decay = weight_decay
def training_step(
self, batch: dict[str, torch.Tensor], batch_idx: torch.Tensor
) -> torch.Tensor:
out, loss, y = self.shared_step(batch)
acc = self.accurarcy(out, y)
auc = roc_auc_score(y.detach().cpu().numpy(), out.detach().cpu().numpy())
logs = {"train_loss": loss, "train_acc": acc, "train_auc": auc}
self.log_dict(
logs,
on_step=True,
on_epoch=True,
prog_bar=True,
logger=True,
batch_size=self.batch_size,
sync_dist=True,
)
return loss
def validation_step(self, batch: dict[str, torch.Tensor], batch_idx: torch.Tensor) -> dict:
out, loss, y = self.shared_step(batch)
self.val_predictions.append(out)
self.val_labels.append(y)
self.val_loss.append(loss.cpu().item())
return loss
def on_validation_epoch_end(self):
val_predictions = torch.concatenate(self.val_predictions).cpu().numpy().ravel()
val_labels = torch.concatenate(self.val_labels).cpu().numpy().ravel()
# logging of final test loss, accuracy and auc
acc = self.accurarcy(torch.tensor(val_predictions), torch.tensor(val_labels))
auc = roc_auc_score(np.array(val_labels), np.array(val_predictions))
logs = {
"val_loss_epoch": np.mean(self.val_loss),
"val_acc_epoch": acc,
"val_auc_epoch": auc,
}
self.log_dict(
logs,
sync_dist=True,
)
val_predictions = torch.concatenate(self.val_predictions).cpu().numpy().ravel()
val_labels = torch.concatenate(self.val_labels).cpu().numpy().ravel()
rejection_rate = calculate_rejection_rate(
val_labels, val_predictions, efficiency=self.efficiency
)
logs = {f"val_epoch_rejection_rate": rejection_rate}
self.log_dict(
logs,
on_step=False,
on_epoch=True,
prog_bar=True,
logger=True,
# batch_size=self.batch_size,
sync_dist=True,
)
self.val_predictions = []
self.val_labels = []
def shared_step(self, batch: dict[str, torch.Tensor]) -> torch.Tensor:
out = self.model(batch["x"], batch["edge_index"])
loss = self.criterion(out, batch["y"])
return out, loss, batch["y"]
def test_step(self, batch, batch_idx) -> torch.Tensor:
out, loss, y = self.shared_step(batch)
self.test_predictions.append(out)
self.test_labels.append(y)
self.test_loss.append(loss.cpu().item())
return out
def on_test_epoch_end(self, cuts=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) -> None:
test_predictions = torch.concatenate(self.test_predictions).cpu().numpy().ravel()
test_labels = torch.concatenate(self.test_labels).cpu().numpy().ravel()
# logging of final test loss, accuracy and auc
acc = self.accurarcy(torch.tensor(test_predictions), torch.tensor(test_labels))
auc = roc_auc_score(np.array(test_labels), np.array(test_predictions))
logs = {"test_loss": np.mean(self.test_loss), "test_acc": acc, "test_auc": auc}
self.log_dict(
logs,
sync_dist=True,
)
for cut in cuts:
figure, rejection_rate = plot_confusion_metrics_matrix(
test_labels, test_predictions, cut=cut
)
if self.comet_ml:
self.logger.experiment.log_figure(
figure_name=f"confusion matrix {cut}",
figure=figure,
)
else:
figure.savefig(os.path.join(self.logger.log_dir, f"confusion_matrix_{cut}.png"))
if self.comet_ml:
self.logger.experiment.log_figure(
figure_name="ROC curve", figure=plot_roc_curve(test_labels, test_predictions)
)
else:
figure = plot_roc_curve(test_labels, test_predictions)
figure.savefig(os.path.join(self.logger.log_dir, "roc_curve.png"))
# rejection active
figure, rejection_rate = plot_confusion(
test_labels, test_predictions, efficiency=self.efficiency
)
self.logger.log_metrics({f"rejection_rate {self.efficiency}": rejection_rate})
if self.comet_ml:
self.logger.experiment.log_figure(
figure_name=f"confusion {self.efficiency}",
figure=figure,
)
else:
figure.savefig(os.path.join(self.logger.log_dir, f"confusion_{self.efficiency}.png"))
# rejection legacy
figure, rejection_rate = plot_confusion(test_labels, test_predictions, efficiency=0.995)
self.logger.log_metrics({f"rejection_rate 0.995": rejection_rate})
if self.comet_ml:
self.logger.experiment.log_figure(
figure_name=f"confusion 0.995",
figure=figure,
)
else:
figure.savefig(os.path.join(self.logger.log_dir, f"confusion_0.995.png"))
test_predictions = (test_predictions >= 0.5).astype(int)
self.logger.log_metrics({"test_f1_score": f1_score(test_labels, test_predictions)})
def configure_optimizers(self) -> dict:
# Select optimizer
if self.optimizer_name == "Adam":
self.optimizer = torch.optim.Adam(
self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay
)
elif self.optimizer_name == "AdamW":
self.optimizer = torch.optim.AdamW(
self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay
)
elif self.optimizer_name == "RMSprop":
self.optimizer = torch.optim.RMSprop(
self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay
)
elif self.optimizer_name == "SGD":
self.optimizer = torch.optim.SGD(
self.model.parameters(),
lr=self.lr,
weight_decay=self.weight_decay,
momentum=self.momentum,
)
elif self.optimizer_name == "Adagrad":
self.optimizer = torch.optim.Adagrad(
self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay
)
else:
raise Exception("The requested optimizer is not implemented!")
# Select loss function
if self.loss_name == "BCEWithLogitsLoss":
self.criterion = torch.nn.BCEWithLogitsLoss()
elif self.loss_name == "BCELoss":
self.criterion = torch.nn.BCELoss()
elif self.loss_name == "FocalLoss":
self.criterion = FocalLoss(alpha=0.8, gamma=2)
elif self.loss_name == "MSELoss":
self.criterion = torch.nn.MSELoss()
elif self.loss_name == "SoftMarginLoss":
self.criterion = torch.nn.SoftMarginLoss()
else:
raise Exception("The requested loss is not implemented!")
# select lr scheduler:
if self.lr_scheduler_name == "ReduceLROnPlateau":
self.scheduler = ReduceLROnPlateau(
self.optimizer, mode=self.callback_mode, factor=0.1, patience=self.lr_patience
)
elif self.lr_scheduler_name == "CosineAnnealingLR":
self.scheduler = CosineAnnealingLR(self.optimizer, eta_min=1e-9, T_max=10)
elif self.lr_scheduler_name == "CosineAnnealingWarmRestarts":
self.scheduler = CosineAnnealingWarmRestarts(
self.optimizer, eta_min=1e-9, T_0=10, T_mult=2
)
elif self.lr_scheduler_name == "LRScheduler":
self.scheduler = LRScheduler(
self.optimizer,
)
else:
raise Exception("The requested lr scheduler is not implemented!")
return {
"optimizer": self.optimizer,
"lr_scheduler": {
"scheduler": self.scheduler,
"monitor": self.metric_callbacks,
"frequency": 1,
"interval": "epoch",
"strict": True,
},
}
def accurarcy(self, out: torch.Tensor, y: torch.Tensor) -> float:
predictions = (out > 0.5).float()
correct = (predictions == y).sum().item()
total = y.size(0)
return correct / total
def configure_callbacks(self) -> list:
checkpoints = ModelCheckpoint(
monitor=self.metric_callbacks,
mode=self.callback_mode,
filename="{epoch}-{val_loss_epoch:.3f}-{val_acc_epoch:.3f}",
save_top_k=1,
)
early_stop = EarlyStopping(
monitor=self.metric_callbacks,
mode=self.callback_mode,
patience=self.early_stopping_patience,
)
return [checkpoints, early_stop]