-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.py
376 lines (348 loc) · 14.1 KB
/
engine.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import logging
import os
from typing import Callable, Literal, Sequence
import numpy as np
import torch
import torch.nn.functional as F
from imageio import imwrite
from lightning.pytorch import LightningDataModule, LightningModule, Trainer
from lightning.pytorch.utilities.compile import _maybe_unwrap_optimized
from matplotlib.cm import get_cmap
from monai.optimizers import WarmupCosineSchedule
from monai.transforms import DivisiblePad
from skimage.exposure import rescale_intensity
from torch.onnx import OperatorExportTypes
from torch.optim.lr_scheduler import ConstantLR
from torchmetrics.functional import (
accuracy,
cosine_similarity,
dice,
jaccard_index,
mean_absolute_error,
mean_squared_error,
pearson_corrcoef,
r2_score,
structural_similarity_index_measure,
)
from viscy.evaluation.evaluation_metrics import mean_average_precision
from viscy.light.data import Sample
from viscy.unet.networks.Unet2D import Unet2d
from viscy.unet.networks.Unet21D import Unet21d
from viscy.unet.networks.Unet25D import Unet25d
try:
from cellpose.models import CellposeModel
except ImportError:
CellposeModel = None
_UNET_ARCHITECTURE = {
"2D": Unet2d,
"2.1D": Unet21d,
# same class with out_stack_depth > 1
"2.2D": Unet21d,
"2.5D": Unet25d,
}
class VSTrainer(Trainer):
def export(
self,
model: LightningModule,
export_path: str,
ckpt_path: str,
format="onnx",
datamodule: LightningDataModule = None,
dataloaders: Sequence = None,
):
"""Export the model for deployment (currently only ONNX is supported).
:param LightningModule model: module to export
:param str export_path: output file name
:param str ckpt_path: model checkpoint
:param str format: format (currently only ONNX is supported), defaults to "onnx"
:param LightningDataModule datamodule: placeholder for datamodule,
defaults to None
:param Sequence dataloaders: placeholder for dataloaders, defaults to None
"""
if dataloaders or datamodule:
logging.debug("Ignoring datamodule and dataloaders during export.")
if not format.lower() == "onnx":
raise NotImplementedError(f"Export format '{format}'")
model = _maybe_unwrap_optimized(model)
self.strategy._lightning_module = model
model.load_state_dict(torch.load(ckpt_path)["state_dict"])
model.eval()
model.to_onnx(
export_path,
input_sample=model.example_input_array,
export_params=True,
opset_version=18,
operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK,
input_names=["input"],
output_names=["output"],
dynamic_axes={
"input": {
0: "batch_size",
1: "channels",
3: "num_rows",
4: "num_cols",
},
"output": {
0: "batch_size",
1: "channels",
3: "num_rows",
4: "num_cols",
},
},
)
logging.info(f"ONNX exported at {export_path}")
class VSUNet(LightningModule):
"""Regression U-Net module for virtual staining.
:param dict model_config: model config,
defaults to :py:class:`viscy.unet.utils.model.ModelDefaults25D`
:param Callable[[torch.Tensor, torch.Tensor], torch.Tensor] loss_function:
loss function in training/validation, defaults to L2 (mean squared error)
:param float lr: learning rate in training, defaults to 1e-3
:param Literal['WarmupCosine', 'Constant'] schedule:
learning rate scheduler, defaults to "Constant"
:param int log_num_samples:
number of image samples to log each training/validation epoch,
has to be smaller than batch size, defaults to 8
:param Sequence[int] example_input_yx_shape:
XY shape of the example input for network graph tracing, defaults to (256, 256)
:param str test_cellpose_model_path:
path to the CellPose model for testing segmentation, defaults to None
:param float test_cellpose_diameter:
diameter parameter of the CellPose model for testing segmentation,
defaults to None
:param bool test_evaluate_cellpose:
evaluate the performance of the CellPose model instead of the trained model
in test stage, defaults to False
"""
def __init__(
self,
architecture: Literal["2D", "2.1D", "2.2D", "2.5D", "3D"],
model_config: dict = {},
loss_function: Callable[[torch.Tensor, torch.Tensor], torch.Tensor] = None,
lr: float = 1e-3,
schedule: Literal["WarmupCosine", "Constant"] = "Constant",
log_num_samples: int = 8,
example_input_yx_shape: Sequence[int] = (256, 256),
test_cellpose_model_path: str = None,
test_cellpose_diameter: float = None,
test_evaluate_cellpose: bool = False,
) -> None:
super().__init__()
net_class = _UNET_ARCHITECTURE.get(architecture)
if not net_class:
raise ValueError(
f"Architecture {architecture} not in {_UNET_ARCHITECTURE.keys()}"
)
if architecture == "2.2D":
model_config["out_stack_depth"] = model_config["in_stack_depth"]
self.model = net_class(**model_config)
# TODO: handle num_outputs in metrics
# self.out_channels = self.model.terminal_block.out_filters
self.loss_function = loss_function if loss_function else F.mse_loss
self.lr = lr
self.schedule = schedule
self.log_num_samples = log_num_samples
self.training_step_outputs = []
self.validation_step_outputs = []
# required to log the graph
if architecture == "2D":
example_depth = 1
else:
example_depth = model_config.get("in_stack_depth") or 5
self.example_input_array = torch.rand(
1,
1,
example_depth,
*example_input_yx_shape,
)
self.test_cellpose_model_path = test_cellpose_model_path
self.test_cellpose_diameter = test_cellpose_diameter
self.test_evaluate_cellpose = test_evaluate_cellpose
def forward(self, x) -> torch.Tensor:
return self.model(x)
def training_step(self, batch: Sample, batch_idx: int):
source = batch["source"]
target = batch["target"]
pred = self.forward(source)
loss = self.loss_function(pred, target)
self.log(
"loss/train",
loss,
on_step=True,
on_epoch=True,
prog_bar=True,
logger=True,
sync_dist=True,
)
if batch_idx == 0:
self.training_step_outputs.extend(
self._detach_sample((source, target, pred))
)
return loss
def validation_step(self, batch: Sample, batch_idx: int):
source = batch["source"]
target = batch["target"]
pred = self.forward(source)
loss = self.loss_function(pred, target)
self.log("loss/validate", loss, sync_dist=True)
if batch_idx == 0:
self.validation_step_outputs.extend(
self._detach_sample((source, target, pred))
)
def test_step(self, batch: Sample, batch_idx: int):
source = batch["source"]
target = batch["target"][:, 0]
if self.test_evaluate_cellpose:
pred = target
else:
pred = self.forward(source)[:, 0]
# FIXME: Only works for batch size 1 and the first channel
self._log_regression_metrics(pred, target)
img_names, ts, zs = batch["index"]
position = float(img_names[0].split("/")[-2])
self.log_dict(
{
"position": position,
"time": float(ts[0]),
"slice": float(zs[0]),
},
on_step=True,
on_epoch=False,
)
if "labels" in batch:
pred_labels = self._cellpose_predict(
pred, f"p{int(position)}_t{ts[0]}_z{zs[0]}"
)
self._log_segmentation_metrics(pred_labels, batch["labels"][0])
else:
self._log_segmentation_metrics(None, None)
def _log_regression_metrics(self, pred: torch.Tensor, target: torch.Tensor):
# paired image translation metrics
self.log_dict(
{
# regression
"test_metrics/MAE": mean_absolute_error(pred, target),
"test_metrics/MSE": mean_squared_error(pred, target),
"test_metrics/cosine": cosine_similarity(
pred, target, reduction="mean"
),
"test_metrics/pearson": pearson_corrcoef(
pred.flatten() * 1e4, target.flatten() * 1e4
),
"test_metrics/r2": r2_score(pred.flatten(), target.flatten()),
# image perception
"test_metrics/SSIM": structural_similarity_index_measure(
pred, target, gaussian_kernel=False, kernel_size=21
),
},
on_step=True,
on_epoch=True,
)
def _cellpose_predict(self, pred: torch.Tensor, name: str) -> torch.ShortTensor:
pred_labels_np = self.cellpose_model.eval(
pred.cpu().numpy(), channels=[0, 0], diameter=self.test_cellpose_diameter
)[0].astype(np.int16)
imwrite(os.path.join(self.logger.log_dir, f"{name}.png"), pred_labels_np)
return torch.from_numpy(pred_labels_np).to(self.device)
def _log_segmentation_metrics(
self, pred_labels: torch.ShortTensor, target_labels: torch.ShortTensor
):
compute = pred_labels is not None
if compute:
pred_binary = pred_labels > 0
target_binary = target_labels > 0
coco_metrics = mean_average_precision(pred_labels, target_labels)
logging.debug(coco_metrics)
self.log_dict(
{
# semantic segmentation
"test_metrics/accuracy": accuracy(
pred_binary, target_binary, task="binary"
)
if compute
else -1,
"test_metrics/dice": dice(pred_binary, target_binary)
if compute
else -1,
"test_metrics/jaccard": jaccard_index(
pred_binary, target_binary, task="binary"
)
if compute
else -1,
"test_metrics/mAP": coco_metrics["map"] if compute else -1,
"test_metrics/mAP_50": coco_metrics["map_50"] if compute else -1,
"test_metrics/mAP_75": coco_metrics["map_75"] if compute else -1,
"test_metrics/mAR_100": coco_metrics["mar_100"] if compute else -1,
},
on_step=True,
on_epoch=False,
)
def predict_step(self, batch: Sample, batch_idx: int, dataloader_idx: int = 0):
source = self._predict_pad(batch["source"])
return self._predict_pad.inverse(self.forward(source))
def on_train_epoch_end(self):
self._log_samples("train_samples", self.training_step_outputs)
self.training_step_outputs = []
def on_validation_epoch_end(self):
self._log_samples("val_samples", self.validation_step_outputs)
self.validation_step_outputs = []
def on_test_start(self):
"""Load CellPose model for segmentation."""
if CellposeModel is None:
# raise ImportError(
# "CellPose not installed. "
# "Please install the metrics dependency with "
# '`pip install viscy".[metrics]"`'
# )
logging.warning(
"CellPose not installed. "
"Please install the metrics dependency with "
'`pip install viscy"[metrics]"`'
)
if self.test_cellpose_model_path is not None:
self.cellpose_model = CellposeModel(
model_type=self.test_cellpose_model_path, device=self.device
)
def on_predict_start(self):
"""Pad the input shape to be divisible by the downsampling factor.
The inverse of this transform crops the prediction to original shape.
"""
down_factor = 2**self.model.num_blocks
self._predict_pad = DivisiblePad((0, 0, down_factor, down_factor))
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.model.parameters(), lr=self.lr)
if self.schedule == "WarmupCosine":
scheduler = WarmupCosineSchedule(
optimizer,
warmup_steps=3,
t_total=self.trainer.max_epochs,
warmup_multiplier=1e-3,
)
elif self.schedule == "Constant":
scheduler = ConstantLR(
optimizer, factor=1, total_iters=self.trainer.max_epochs
)
return [optimizer], [scheduler]
def _detach_sample(self, imgs: Sequence[torch.Tensor]):
num_samples = min(imgs[0].shape[0], self.log_num_samples)
return [
[np.squeeze(img[i].detach().cpu().numpy().max(axis=1)) for img in imgs]
for i in range(num_samples)
]
def _log_samples(self, key: str, imgs: Sequence[Sequence[np.ndarray]]):
images_grid = []
for sample_images in imgs:
images_row = []
for i, image in enumerate(sample_images):
cm_name = "gray" if i == 0 else "inferno"
if image.ndim == 2:
image = image[np.newaxis]
for channel in image:
channel = rescale_intensity(channel, out_range=(0, 1))
render = get_cmap(cm_name)(channel, bytes=True)[..., :3]
images_row.append(render)
images_grid.append(np.concatenate(images_row, axis=1))
grid = np.concatenate(images_grid, axis=0)
self.logger.experiment.add_image(
key, grid, self.current_epoch, dataformats="HWC"
)