-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_base.py
198 lines (174 loc) · 5.92 KB
/
engine_base.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
from abc import ABC, abstractmethod
from typing import Optional
import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchmetrics import (AUROC, AveragePrecision, ConfusionMatrix, F1Score,
Precision, Recall)
class BaseEngine(pl.LightningModule, ABC):
@abstractmethod
def __init__(
self,
in_channels: int,
intermediate_dim: int,
n_classes: int,
stain_info: bool,
dropout: bool,
) -> None:
super().__init__()
self.in_channels = in_channels
self.intermediate_dim = intermediate_dim
self.n_classes = n_classes
self.stain_info = stain_info
self.dropout = dropout
# Bag loss function
self.bag_loss_fn = nn.CrossEntropyLoss()
# Metrics
self.recall = Recall(num_classes=self.n_classes,
average="weighted")
self.prec = Precision(num_classes=self.n_classes,
average="weighted")
self.auprc = AveragePrecision(average="weighted",
num_classes=self.n_classes)
self.auroc = AUROC(average="weighted",
num_classes=self.n_classes, pos_label=None)
self.f1_score = F1Score(average="weighted",
num_classes=self.n_classes)
self.confmat = ConfusionMatrix(num_classes=self.n_classes)
@abstractmethod
def training_step(self, batch, batch_idx):
pass
def training_epoch_end(self, outputs) -> None:
avg_loss = torch.stack(
[x['loss'] for x in outputs]
)
# print(avg_loss.shape)
avg_loss = avg_loss[~torch.any(avg_loss.isnan(), dim=1)]
avg_loss = avg_loss.mean()
self.log("avg_train_loss",
avg_loss,
on_step=False,
on_epoch=True,
prog_bar=True,
logger=True,
batch_size=1
)
@abstractmethod
def validation_step(self, batch, batch_idx):
pass
@torch.no_grad()
def validation_epoch_end(self, outputs):
avg_loss = torch.stack(
[x['loss'] for x in outputs]
)
if avg_loss.dim() == 1:
avg_loss = avg_loss.unsqueeze_(1)
# print(avg_loss.shape, avg_loss.dim())
avg_loss = avg_loss[~torch.any(avg_loss.isnan(), dim=1)]
avg_loss = avg_loss.mean()
self.log("avg_val_loss",
avg_loss,
on_step=False,
on_epoch=True,
prog_bar=True,
logger=True,
batch_size=1
)
@abstractmethod
def test_step(self, batch, batch_idx):
pass
@torch.no_grad()
def test_epoch_end(self, outputs):
avg_loss = torch.stack(
[x['loss'] for x in outputs]
)
if avg_loss.dim() == 1:
avg_loss = avg_loss.unsqueeze_(1)
avg_loss = avg_loss[~torch.any(avg_loss.isnan(), dim=1)]
avg_loss = avg_loss.mean()
test_preds = []
test_targets = []
test_probs = []
fnames_topk_ids = dict()
for x in outputs:
pred = x['y_pred']
target = x['target']
probs = x['y_prob']
attn_raw = x['A_raw']
top_ids = x['top_ids']
fname = x['filename'][0]
fnames_topk_ids[f'{fname}'] = {
'A_raw': attn_raw,
'top_ids': top_ids,
'pred': pred,
'target': target,
'probs': probs,
}
test_preds.append(pred)
test_targets.append(target)
test_probs.append(probs)
test_preds = torch.cat(test_preds, dim=0)
test_targets = torch.cat(test_targets, dim=0)
test_probs = torch.cat(test_probs, dim=0)
# METRICS
recall = self.recall(test_preds.long(), test_targets.long()).cpu()
precision = self.prec(
test_preds.long(), test_targets.long()).cpu()
auprc = self.auprc(test_probs, test_targets.long()).cpu()
f1 = self.f1_score(test_preds.long(), test_targets.long()).cpu()
auroc = self.auroc(test_probs, test_targets.long()).cpu()
confmat = self.confmat(test_preds.long(), test_targets.long()).cpu()
self.log("avg_test_loss",
avg_loss,
batch_size=1)
self.log("avg_test_recall",
recall,
batch_size=1)
self.log("avg_test_precision",
precision,
batch_size=1)
self.log("AUPRC",
auprc,
batch_size=1)
self.log("avg_f1",
f1,
batch_size=1)
self.log("AUROC",
auroc,
batch_size=1)
self.test_results = {
'test_loss': avg_loss,
'predictions': test_preds,
'probabilities': test_probs,
'targets': test_targets,
'test_recall': recall,
'test_precision': precision,
'auprc': auprc,
'test_f1_score': f1,
'confmat': confmat,
'auroc': auroc,
'fnames_topk_ids': fnames_topk_ids
}
def configure_optimizers(self):
optim = torch.optim.Adam(
self.parameters(),
lr=3e-4,
betas=(0.9, 0.95),
weight_decay=1e-4
)
schedule = torch.optim.lr_scheduler.OneCycleLR(
optim,
max_lr=3e-4,
total_steps=self.trainer.estimated_stepping_batches,
pct_start=0.2,
anneal_strategy='cos',
cycle_momentum=True,
)
return {
'optimizer': optim,
'lr_scheduler': {
'scheduler': schedule,
'interval': 'step'
}
}