forked from clementpoiret/hsf_train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (149 loc) · 5.84 KB
/
main.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
import logging
import os
from copy import deepcopy
from datetime import datetime
import hydra
import lightning as L
import torch
import torchio as tio
from dotenv import load_dotenv
from lightning.pytorch.callbacks import ModelCheckpoint
from lightning.pytorch.loggers import WandbLogger
from omegaconf import DictConfig
from rich.logging import RichHandler
from sparseml.exporters.onnx_to_deepsparse import ONNXToDeepsparse
import wandb
from hsftrain.callbacks import SparseMLCallback
from hsftrain.data.loader import load_from_config
from hsftrain.exporter import TorchToONNX
from hsftrain.models.losses import FocalTversky_loss
from hsftrain.models.models import SegmentationModel
VERSION = "4.0.0"
FORMAT = "%(message)s"
logging.basicConfig(level="INFO",
format=FORMAT,
datefmt="[%X]",
handlers=[RichHandler(markup=True)])
log = logging.getLogger(__name__)
load_dotenv()
# from hydra import compose, initialize
# initialize(config_path="conf")
# cfg = compose(config_name="config")
@hydra.main(version_base=None, config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
dt = datetime.now()
ts = int(datetime.timestamp(dt))
name = f"arunet2_{ts}"
log.info(f"Experiment name: {name}")
mri_datamodule = load_from_config(cfg.datasets)(
preprocessing_pipeline=tio.Compose([
tio.ToCanonical(),
tio.ZNormalization(),
tio.OneHot(),
]),
augmentation_pipeline=tio.Compose([
tio.RandomFlip(axes=('LR',), p=.5),
tio.RandomMotion(degrees=5, translation=5, num_transforms=3, p=.1),
tio.RandomBlur(std=(0, 0.5), p=.1),
tio.RandomNoise(mean=0, std=0.5, p=.1),
tio.RandomGamma(log_gamma=0.4, p=.1),
tio.RandomAffine(scales=.3,
degrees=30,
translation=5,
isotropic=False,
p=.2),
# tio.RandomAnisotropy(p=.1, scalars_only=False),
tio.transforms.RandomElasticDeformation(num_control_points=4,
max_displacement=4,
locked_borders=0,
p=.1),
# tio.RandomSpike(p=.01),
# tio.RandomBiasField(coefficients=.2, p=.01),
]),
postprocessing_pipeline=tio.Compose([
tio.CopyAffine("mri"),
tio.EnsureShapeMultiple(8),
]))
wandb.login(key=os.getenv("WANDB_API_KEY"))
wandb_logger = WandbLogger(name=name, project="arunet2")
_dm = deepcopy(mri_datamodule)
_dm.setup()
N = len(_dm.subjects_train_list)
N_val = len(_dm.subjects_val_list)
log.info(f"Train dataset size: {N}")
log.info(f"Validation dataset size: {N_val}")
steps_per_epoch = N // cfg.datasets.batch_size
steps_per_epoch = steps_per_epoch // cfg.lightning.accumulate_grad_batches
wandb_logger.experiment.config.update({"train_size": N, "val_size": N_val})
seg_loss = FocalTversky_loss({"apply_nonlin": None})
hparams = cfg.models.hparams
model = SegmentationModel(hparams=hparams,
learning_rate=cfg.models.lr,
seg_loss=seg_loss,
use_forgiving_loss=cfg.models.use_forgiving_loss,
epochs=cfg.lightning.max_epochs,
steps_per_epoch=steps_per_epoch,
precision=cfg.lightning.precision)
callbacks = [
ModelCheckpoint(monitor="val/epoch/loss",
mode="min",
save_top_k=1,
save_last=True,
verbose=True,
dirpath=f"{cfg.datasets.output_path}ckpt/",
filename=f"arunet_{VERSION}_{ts}"),
]
if cfg.models.use_sparseml:
sparseml = SparseMLCallback(recipe_path="sparseml/scratch.yaml",
steps_per_epoch=steps_per_epoch)
callbacks.append(sparseml)
trainer = L.Trainer(logger=wandb_logger,
callbacks=callbacks,
**cfg.lightning)
trainer.fit(model, datamodule=mri_datamodule)
dummy_input = torch.randn(1, 1, 16, 16, 16)
model.eval()
if cfg.models.use_sparseml:
exporter = TorchToONNX(
sample_batch=dummy_input.to(model.device),
input_names=["cropped_hippocampus"],
output_names=["segmented_hippocampus"],
opset=17,
)
exporter.export(
model,
f"{cfg.datasets.output_path}onnx/arunet_{VERSION}_{ts}.onnx",
)
# Convert to deepsparse
exporter = ONNXToDeepsparse(skip_input_quantize=False)
exporter.export(
f"{cfg.datasets.output_path}onnx/arunet_{VERSION}_{ts}.onnx",
f"{cfg.datasets.output_path}onnx/arunet_{VERSION}_{ts}_deepsparse.onnx",
)
else:
model = model.to("cpu").float()
torch.onnx.export(
model,
dummy_input,
f"{cfg.datasets.output_path}onnx/arunet_{VERSION}_{ts}.onnx",
input_names=["cropped_hippocampus"],
output_names=["segmented_hippocampus"],
dynamic_axes={
'cropped_hippocampus': {
0: 'batch',
2: "x",
3: "y",
4: "z"
},
'segmented_hippocampus': {
0: 'batch',
2: "x",
3: "y",
4: "z"
}
},
opset_version=17)
if __name__ == "__main__":
torch.set_float32_matmul_precision("medium")
L.seed_everything(42)
main()