-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_synapse.py
216 lines (180 loc) · 6.55 KB
/
train_synapse.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
from __future__ import annotations
import os
from os.path import join
from collections import defaultdict
import torch
import numpy as np
import monai
from monai import data
from monai.metrics import CumulativeAverage
import lightning as L
from lightning.pytorch.callbacks import ModelCheckpoint, EarlyStopping
from loguru import logger
from lr_scheduler import LR_SCHEDULERS
from loss import LOSSES
from eval import eval_single_volume
from model import build_model
from dataset_synapse import SynapseDataset
from torchvision.transforms import transforms
from typing import Callable
torch.set_float32_matmul_precision("medium")
device: str = "cuda" if torch.cuda.is_available() else "cpu"
OPTIMIZERS = {
"Adam": torch.optim.Adam,
"SGD": torch.optim.SGD,
"RMSprop": torch.optim.RMSprop,
"AdamW": torch.optim.AdamW
}
class Synapse(L.LightningModule):
def __init__(self, name: str) -> None:
super(Synapse, self).__init__()
self.name = name
self.num_classes = 9
self.max_epochs = 300
self.freeze_encoder_epochs = 10
self._model = build_model(
in_channels=3,
num_classes=self.num_classes,
).to(device)
self.build_loss()
self.tl_metric = CumulativeAverage()
self.vs_metric = defaultdict(lambda: defaultdict(list))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self._model(x)
def prepare_data(self) -> None:
self.norm_x_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
self.train_dataset = SynapseDataset(
base_dir="dataset/synapse/train_slice",
split="train",
norm_x_transform=self.norm_x_transform,
norm_y_transform=transforms.ToTensor(),
)
self.val_dataset = SynapseDataset(base_dir="dataset/synapse/test_vol", split="test_vol")
def train_dataloader(self) -> data.DataLoader:
tdl_0 = {
"batch_size": 32,
"num_workers": 6,
"shuffle": True,
"pin_memory": True,
"persistent_workers": True,
"worker_init_fn": None
}
return data.DataLoader(self.train_dataset, **tdl_0)
def val_dataloader(self) -> data.DataLoader:
vdl_0 = {
"batch_size": 1,
"shuffle": False,
"pin_memory": True,
"num_workers": 4,
"persistent_workers": True
}
return data.DataLoader(self.val_dataset, **vdl_0)
def build_loss(self):
loss_0 = ("DiceCELoss", {
"ce_weight": 0.4,
"dc_weight": 0.6,
})
self.loss = LOSSES[loss_0[0]](**loss_0[1])
@property
def criterion(self) -> Callable[..., torch.Tensor]:
return self.loss
def configure_optimizers(self) -> dict:
optimizer_0 = ("AdamW", {
"lr": 5e-4,
"weight_decay": 1e-3,
"eps": 1e-8,
"amsgrad": False,
"betas": (0.9, 0.999)
})
optimizer = OPTIMIZERS[optimizer_0[0]](self._model.parameters(), **optimizer_0[1])
scheduler_1 = ("CosineAnnealingLR", {
"T_max": self.max_epochs,
"eta_min": 1e-6
})
scheduler = LR_SCHEDULERS[scheduler_1[0]](optimizer, **scheduler_1[1])
return {
"optimizer": optimizer,
"lr_scheduler": scheduler
}
def log_and_logger(self, name: str, value: ...) -> None:
self.log(name, value)
logger.info(f"epoch: {self.current_epoch} - {name}: {value}")
def on_train_epoch_start(self) -> None:
if self.current_epoch < self.freeze_encoder_epochs:
self._model.freeze_encoder()
else:
self._model.unfreeze_encoder()
super().on_train_epoch_start()
def training_step(self, batch: dict[str, torch.Tensor], batch_idx: int) -> torch.Tensor:
image, label = batch["image"].to(device), batch["label"]
label = label.to(device)
pred = self.forward(image)
loss = self.criterion(pred, label)
self.log("loss", loss.item(), prog_bar=True)
self.tl_metric.append(loss.item())
self.log("lr", self.optimizers().param_groups[0]["lr"], prog_bar=True)
return loss
def on_train_epoch_end(self) -> None:
tl = self.tl_metric.aggregate().item()
self.log_and_logger("mean_train_loss", tl)
self.tl_metric.reset()
def validation_step(self, batch: dict[str, torch.Tensor]) -> None:
volume, label = batch["image"], batch["label"]
metric = eval_single_volume(
model=self._model,
volume=volume,
label=label,
num_classes=self.num_classes,
output=join(self.name, str(self.current_epoch)),
patch_size=(224, 224),
device=device,
norm_x_transform=getattr(self, "norm_x_transform", None),
)
for metric_name, class_metric in metric.items():
for class_name, value in class_metric.items():
self.vs_metric[metric_name][class_name].append(np.mean(value))
def on_validation_epoch_end(self) -> None:
for metric_name, class_metric in self.vs_metric.items():
avg_metric = []
for class_name, value in class_metric.items():
t = np.mean(value)
self.log(f"val_{metric_name}_{class_name}", t)
avg_metric.append(t)
self.log_and_logger(f"val_mean_{metric_name}", np.mean(avg_metric))
self.vs_metric = defaultdict(lambda: defaultdict(list))
def train(name: str) -> None:
os.makedirs(name, exist_ok=True)
logger.add(join(name, "training.log"))
model = Synapse(name)
checkpoint_callback = ModelCheckpoint(
dirpath=join(name, "checkpoints"),
monitor="val_mean_dice",
mode="max",
filename="{epoch:02d}-{val_mean_dice:.4f}",
save_last=True
)
early_stop_callback = EarlyStopping(
monitor="mean_train_loss",
mode="min",
min_delta=0.00,
patience=15
)
trainer = L.Trainer(
precision=32,
accelerator=device,
devices="auto",
max_epochs=model.max_epochs,
check_val_every_n_epoch=20,
gradient_clip_val=None,
default_root_dir=name,
callbacks=[checkpoint_callback, early_stop_callback],
enable_checkpointing=True
)
trainer.fit(model, ckpt_path=None)
if __name__ == "__main__":
L.seed_everything(42)
monai.utils.set_determinism(42)
train("log/msvm-unet-synapse")