-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
151 lines (136 loc) · 4.61 KB
/
callbacks.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
import logging
from typing import Any
from lightning import LightningModule, Trainer
import torch
from lightning import Callback
from lightning.pytorch.utilities.types import STEP_OUTPUT
from models import AbstractCPTModule
from utils import (
fit_rs2table,
test_rs2table,
CLSandCPTMetrics,
)
class MetricsCaculator(Callback):
def __init__(
self,
verbose: bool = True,
) -> None:
super().__init__()
self.verbose = verbose
self.cls_avg = "micro"
self.cpt_avg = "micro"
def _unpack_outputs(self, pl_module, outputs: STEP_OUTPUT):
if isinstance(pl_module, AbstractCPTModule):
ret_keys = [
"disease_logits",
"disease_lbls",
"lesion_logits",
"lesion_lbls",
]
else:
raise ValueError(
f"pl_module must be AbstractCPTModule, got {type(pl_module)}"
)
return (outputs[key] for key in ret_keys)
def on_fit_start(
self, trainer: Trainer, pl_module: LightningModule
) -> None:
self.best_metrics = None
self.best_epoch = None
if isinstance(pl_module, AbstractCPTModule):
self.train_metrics = CLSandCPTMetrics(
pl_module.disease_names,
pl_module.lesion_names,
self.cls_avg,
self.cpt_avg,
).to(pl_module.device)
self.val_metrics = CLSandCPTMetrics(
pl_module.disease_names,
pl_module.lesion_names,
self.cls_avg,
self.cpt_avg,
).to(pl_module.device)
else:
raise ValueError(
f"pl_module must be AbstractCPTModule, got {type(pl_module)}"
)
def on_test_start(self, trainer: Trainer, pl_module: LightningModule) -> None:
if isinstance(pl_module, AbstractCPTModule):
self.test_metrics = CLSandCPTMetrics(
pl_module.disease_names,
pl_module.lesion_names,
self.cls_avg,
self.cpt_avg,
).to(pl_module.device)
else:
raise ValueError(
f"pl_module must be AbstractCPTModule, got {type(pl_module)}"
)
def on_train_batch_end(
self,
trainer: Trainer,
pl_module: LightningModule,
outputs: STEP_OUTPUT,
batch: Any,
batch_idx: int,
) -> None:
self.train_metrics.update(*self._unpack_outputs(pl_module, outputs))
def on_validation_batch_end(
self,
trainer: Trainer,
pl_module: LightningModule,
outputs: STEP_OUTPUT,
batch: Any,
batch_idx: int,
dataloader_idx: int = 0,
) -> None:
self.val_metrics.update(*self._unpack_outputs(pl_module, outputs))
def on_test_batch_end(
self,
trainer: Trainer,
pl_module: LightningModule,
outputs: STEP_OUTPUT,
batch: Any,
batch_idx: int,
dataloader_idx: int = 0,
) -> None:
self.test_metrics.update(*self._unpack_outputs(pl_module, outputs))
def on_train_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None:
train_rs = self.train_metrics.compute()
val_rs = self.val_metrics.compute()
self.train_metrics.reset()
self.val_metrics.reset()
self.current_val_rs = val_rs
table = fit_rs2table(
trainer.current_epoch, train_rs, val_rs, self.best_metrics, self.best_epoch
)
logging.info(f"\n{table}") if self.verbose else None
for name, value in train_rs.items():
pl_module.log(
f"train/{name}",
value,
prog_bar=True if name in ["kappa"] else False,
on_step=False,
on_epoch=True,
sync_dist=True,
)
for name, value in val_rs.items():
pl_module.log(
f"val/{name}",
value,
prog_bar=True if name in ["kappa"] else False,
on_step=False,
on_epoch=True,
sync_dist=True,
)
def on_test_end(self, trainer: Trainer, pl_module: LightningModule) -> None:
table = test_rs2table(self.test_metrics.compute())
logging.info(f"\n{table}") if self.verbose else None
def on_save_checkpoint(
self,
trainer: Trainer,
pl_module: LightningModule,
checkpoint: torch.Dict[str, Any],
) -> None:
self.best_metrics = self.current_val_rs
self.best_epoch = trainer.current_epoch