-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathunet_lightning.py
332 lines (276 loc) · 10.9 KB
/
unet_lightning.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
import pytorch_lightning as pl
import torch
import torchmetrics
from torch.nn import CrossEntropyLoss
from losses import DiceLoss
class Segmentation_UNET(pl.LightningModule):
def __init__(self, model, lr, num_classes, weight_ce, weight_dice, metrics=True):
super().__init__()
# model
self.model = model
# learning rate
self.lr = lr
# number of classes
self.num_classes = num_classes
# loss
self.register_buffer("weight_ce", weight_ce)
self.register_buffer("weight_dice", weight_dice)
self.dice_loss = DiceLoss(weight=self.weight_dice)
self.ce_loss = CrossEntropyLoss(weight=self.weight_ce)
# save hyperparameters
self.save_hyperparameters()
# metrics
self.metrics = metrics
if self.metrics:
self.f1_train = CustomMetric(
metric=torchmetrics.functional.f1,
metric_name="F1",
num_classes=self.num_classes,
average="none",
mdmc_average="samplewise",
)
self.f1_valid = CustomMetric(
metric=torchmetrics.functional.f1,
metric_name="F1",
num_classes=self.num_classes,
average="none",
mdmc_average="samplewise",
)
self.f1_test = CustomMetric(
metric=torchmetrics.functional.f1,
metric_name="F1",
num_classes=self.num_classes,
average="none",
mdmc_average="samplewise",
)
self.iou_train = CustomMetric(
metric=torchmetrics.functional.iou,
metric_name="IoU",
num_classes=self.num_classes,
reduction="none",
)
self.iou_valid = CustomMetric(
metric=torchmetrics.functional.iou,
metric_name="IoU",
num_classes=self.num_classes,
reduction="none",
)
self.iou_test = CustomMetric(
metric=torchmetrics.functional.iou,
metric_name="IoU",
num_classes=self.num_classes,
reduction="none",
)
def shared_step(self, batch):
# Batch
x, y, x_name, y_name = batch["x"], batch["y"], batch["x_name"], batch["y_name"]
# Prediction
out = self.model(x)
# Softmax
out_soft = torch.nn.functional.softmax(out, dim=1)
# Loss
ce_loss = self.ce_loss(out, y) # cross entropy loss (LogSoftmax + NLLLoss)
dice_loss = self.dice_loss(out, y) # soft dice loss (Softmax + soft dice)
loss = (ce_loss + dice_loss) / 2 # Linear combination of both losses
return {**batch, "pred": out_soft, "loss": loss}
def training_step(self, batch, batch_idx):
# Loss
shared_step = self.shared_step(batch)
# Metrics
if self.metrics:
self.compute_and_log_metrics_batch(
pred=shared_step["pred"],
tar=shared_step["y"],
name_phase="Train",
metrics_module=self.f1_train,
) # F1
self.compute_and_log_metrics_batch(
pred=torchmetrics.utilities.data.to_categorical(shared_step["pred"]),
tar=shared_step["y"],
name_phase="Train",
metrics_module=self.iou_train,
) # IoU
return shared_step["loss"]
def training_epoch_end(self, outputs):
if self.metrics:
self.compute_and_log_metrics_epoch(
name_phase="Train", metrics_module=self.f1_train
) # F1
self.compute_and_log_metrics_epoch(
name_phase="Train", metrics_module=self.iou_train
) # IoU
def validation_step(self, batch, batch_idx):
# Loss
shared_step = self.shared_step(batch)
# Metrics
if self.metrics:
self.compute_and_log_metrics_batch(
pred=shared_step["pred"],
tar=shared_step["y"],
name_phase="Valid",
metrics_module=self.f1_valid,
) # F1
self.compute_and_log_metrics_batch(
pred=pl.metrics.utils.to_categorical(shared_step["pred"]),
tar=shared_step["y"],
name_phase="Valid",
metrics_module=self.iou_valid,
) # IoU
# Logging for checkpoint
self.log(
"checkpoint_valid_f1_epoch", self.f1_valid.get_metrics_batch(mean=True)
) # per epoch automatically
def validation_epoch_end(self, outputs):
if self.metrics:
self.compute_and_log_metrics_epoch(
name_phase="Valid", metrics_module=self.f1_valid
) # F1
self.compute_and_log_metrics_epoch(
name_phase="Valid", metrics_module=self.iou_valid
) # IoU
def test_step(self, batch, batch_idx):
# Loss
shared_step = self.shared_step(batch)
# Metrics
if self.metrics:
self.compute_and_log_metrics_batch(
pred=shared_step["pred"],
tar=shared_step["y"],
name_phase="Test",
metrics_module=self.f1_test,
name=shared_step["x_name"],
) # F1
self.compute_and_log_metrics_batch(
pred=pl.metrics.utils.to_categorical(shared_step["pred"]),
tar=shared_step["y"],
name_phase="Test",
metrics_module=self.iou_test,
name=shared_step["x_name"],
) # IoU
# Names
if shared_step["y"].shape[0] == 1:
# Log the name of target only if batch_size=1
# Logging a list of strings is not yet supported
self.log_names_batch(name_phase="Test", name=shared_step["x_name"][0])
def test_epoch_end(self, outputs):
if self.metrics:
self.compute_and_log_metrics_epoch(
name_phase="Valid", metrics_module=self.f1_test
) # F1
self.compute_and_log_metrics_epoch(
name_phase="Valid", metrics_module=self.iou_test
) # IoU
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode="max", factor=0.75, patience=10, min_lr=0
)
return {
"optimizer": optimizer,
"lr_scheduler": lr_scheduler,
"monitor": "checkpoint_valid_f1_epoch",
}
def compute_and_log_metrics_batch(
self, pred, tar, name_phase, metrics_module, name=None
):
# Metrics
metrics_module.batch(pred, tar, name=name) # e.g. [0.2, 0.3, 0.25, 0.25]
# Logging mean
self.logger.experiment.log_metric(
f"{name_phase}/{metrics_module}/Batch",
metrics_module.get_metrics_batch(mean=True),
)
# Logging per class
for class_idx, metric in zip(
metrics_module.valid_class, metrics_module.get_metrics_batch(mean=False)
):
self.logger.experiment.log_metric(
f"{name_phase}/{metrics_module}/Batch/Class/{class_idx}", metric
)
def compute_and_log_metrics_epoch(self, name_phase, metrics_module):
# Class
for class_idx, value in enumerate(metrics_module.get_metrics_epoch()):
self.logger.experiment.log_metric(
f"{name_phase}/{metrics_module}/Epoch/Class/{class_idx}", value
)
# Total
self.logger.experiment.log_metric(
f"{name_phase}/{metrics_module}/Epoch", metrics_module.epoch()
) # Total F1
def log_names_batch(self, name_phase, name):
self.logger.experiment.log_text(f"{name_phase}/Batch/Names", name)
class CustomMetric:
def __init__(self, metric, metric_name, **kwargs):
self.metric = metric
self.metric_name = metric_name
self.kwargs = kwargs
self.scores = []
self.valid_classes = []
self.valid_matrices = []
self.names = []
self.score = None
self.valid_class = None
self.valid_matrix = None
self.name = None
self.last_scores = None
self.last_valid_classes = None
self.last_valid_matrices = None
self.last_names = None
def batch(self, prediction, target, name=None):
# compute scores for every batch
self.score = self.metric(prediction, target, **self.kwargs).to("cpu")
# compute valid classes for every batch
self.valid_class = target.unique().to("cpu")
# compute valid_matrix for every batch
dummy = torch.zeros_like(self.score).to("cpu")
dummy[self.valid_class] = 1
self.valid_matrix = dummy.type(torch.bool).to("cpu")
self.scores.append(self.score)
self.valid_classes.append(self.valid_class)
self.valid_matrices.append(self.valid_matrix)
# store name(s)
if name:
self.name = name
self.names.append(self.name)
def get_metrics_batch(self, mean=True):
# returns the class metrics of the batch for the classes that are present in the image
if mean:
return self.score[self.valid_class].mean()
else:
return self.score[self.valid_class]
def get_metrics_epoch(self, last=False, transpose=True):
# transpose=True gives the per class metrics (mean)
# transpose=False, gives the per batch metrics (mean)
if last:
if transpose:
scores = torch.stack(self.last_scores).T
masks = torch.stack(self.last_valid_matrices).T
else:
scores = torch.stack(self.last_scores)
masks = torch.stack(self.last_valid_matrices)
else:
if transpose:
scores = torch.stack(self.scores).T
masks = torch.stack(self.valid_matrices).T
else:
scores = torch.stack(self.scores)
masks = torch.stack(self.valid_matrices)
# iterate over columns (classes) and only select the present classes
filtered = [s[m] for s, m in zip(scores, masks)]
return torch.stack([c.mean() for c in filtered])
def epoch(self):
# compute scores for every epoch
self.last_scores = self.scores
self.last_valid_classes = self.valid_classes
self.last_valid_matrices = self.valid_matrices
self.last_names = self.names
result = self.get_metrics_epoch()
self.reset()
return result.mean()
def reset(self):
self.scores = []
self.valid_classes = []
self.valid_matrices = []
self.names = []
def __repr__(self):
return self.metric_name