-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_metrics.py
327 lines (272 loc) · 10.5 KB
/
evaluate_metrics.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
from __future__ import annotations
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
from medpy.metric.binary import asd, dc, hd95
from torch.utils.data import DataLoader
from typing_extensions import TypeVar
from model.ema import EMA
from model.siab import SIABWrapper
from utils.mask_convert import converter, pred_mask, to_image, to_label
Collector = TypeVar("Collector", bound="ResultCollector")
class ResultCollector(ABC):
def __init__(self, metric: str, n_classes: int, n_domains: int):
self.metric = metric
self.n_classes = n_classes
self.n_domains = n_domains
@abstractmethod
def update(self, instance_metric_per_cls, domain_id):
raise NotImplementedError
@abstractmethod
def get(self):
raise NotImplementedError
class VolumewiseCollector(ResultCollector):
def __init__(self, metric: str, n_classes: int, n_domains: int):
super().__init__(metric, n_classes, n_domains)
self.domain_instances: list[list[list[float]]] = [
[] for _ in range(n_domains)
] # (domain, instance, class)
@property
def averaged_instances(self) -> list[list[float]]:
"""Return the instance metric averaged over all classes."""
return [[
sum(inst) / len(inst) if len(inst) > 0 else 0 for inst in instances
] for instances in self.domain_instances]
def update(self, instance_metric_per_cls, domain_id):
k = 100 if self.metric.lower() in ["dice", "iou", "jaccard"] else 1
self.domain_instances[domain_id].append(
[v * k for v in instance_metric_per_cls])
def get(self):
sum_domain_class = [[
sum(inst[c] for inst in instances) for c in range(self.n_classes)
] for instances in self.domain_instances]
count_domain = [len(v) for v in self.domain_instances]
metric_domain_classwise = []
metric_domain_mean = []
mean_metric_sum = 0
mean_metric_cnt = 0
for sum_class, cnt in zip(sum_domain_class, count_domain):
if cnt == 0:
continue
classwise = [v / cnt for v in sum_class]
mean = sum(classwise) / len(classwise)
metric_domain_classwise.append(classwise)
metric_domain_mean.append(mean)
mean_metric_sum += sum(classwise)
mean_metric_cnt += len(classwise)
mean_dice = mean_metric_sum / mean_metric_cnt
return mean_dice, metric_domain_mean, metric_domain_classwise
def siab_enabled(model) -> bool:
if isinstance(model, EMA):
name = model.module.__class__.__name__
else:
name = model.__class__.__name__
return name == SIABWrapper.__name__
def evaluate_volume_metrics(
model,
dataloader: DataLoader,
cfg: dict,
is_target_domain: bool = False,
verbose: bool = False,
eval: bool = True,
save_dir: Path | None = None,
collector_cls: type[Collector] = VolumewiseCollector,
) -> list[Collector]:
if eval:
model.eval()
has_siab = siab_enabled(model)
pred_fn = model
current_volume = None
num_volumes = 0
if cfg["dataset"] == "fundus":
# fundus is 2D dataset, treat each image as a volume
def is_next_volume(path): # type: ignore
return True
elif cfg["dataset"] == "mnms":
# must be careful with this dataset
# vendorB contains 2 centers, whose patient ids are overlapped
# however, the volume ids are ascending for each center
# and we visit the dataset center by center
# so the volume can be correctly identified by the first 3 digits
def is_next_volume(path: str):
nonlocal current_volume
nonlocal num_volumes
# {patient_id:03d}{slice_id:03d}
if current_volume != path[:3]:
current_volume = path[:3]
num_volumes += 1
return True
return False
elif cfg["dataset"] == "prostate":
def is_next_volume(path: str):
nonlocal current_volume
nonlocal num_volumes
# {volume}_{slice}_{type}
vol = path.split("_")[0]
if current_volume != vol:
current_volume = vol
num_volumes += 1
return True
return False
else:
raise ValueError(f"Unknown dataset {cfg['dataset']}")
ret = evaluate_with_pred_function_volume(pred_fn,
dataloader,
cfg,
is_next_volume,
has_siab,
is_target_domain,
save_dir,
collector_cls=collector_cls)
if verbose:
print(num_volumes)
return ret
def evaluate_slice_metrics(
model,
dataloader: DataLoader,
cfg: dict,
is_target_domain: bool = False,
eval: bool = True,
save_dir: Path | None = None,
penalty: int | None = None,
):
if eval:
model.eval()
has_siab = siab_enabled(model)
pred_fn = model
# treat each slice as a volume, i.e., slice-wise evaluation
def is_next_volume(path):
return True
return evaluate_with_pred_function_volume(
pred_fn,
dataloader,
cfg,
is_next_volume,
has_siab,
is_target_domain,
save_dir,
collector_cls=VolumewiseCollector,
dist_penalty=penalty)
@torch.no_grad()
def evaluate_with_pred_function_volume(
pred_fn,
dataloader: DataLoader,
cfg: dict,
is_next_volume: Callable[[str], bool],
has_siab: bool = False,
is_target_domain: bool = False,
save_dir: Path | None = None,
dist_penalty: int | None = None,
collector_cls: type[Collector] = VolumewiseCollector,
) -> list[Collector]:
# pred_fn: image tensor -> logits tensor
convert = converter[cfg["dataset"]]
n_domains = cfg["n_domains"]
n_classes = cfg["n_classes"] - 1 # exclude background
size = cfg["image_size"]
metrics = [("dice", dc, {}), ("asd", asd, {}), ("hd", hd95, {})]
results = [
collector_cls(metric, n_classes, n_domains) for metric, *_ in metrics
]
dist_metric = ["asd", "assd", "hd"]
dist_penalty = dist_penalty or 2
current_volume_pred = []
current_volume_mask = []
domain = -1
num_samples = 0
for img, mask, domain, path in dataloader:
img, mask = img.cuda(), mask.cuda()
domain = domain.item()
h, w = img.shape[-2:]
# down
if h != size or w != size:
img = F.interpolate(img, (size, size),
mode="bilinear",
align_corners=False)
# pred
if has_siab:
# target domain uses the statistics-aggregated branch (id=-1)
kwargs = dict(domain_id=-1 if is_target_domain else domain)
else:
kwargs = {}
pred = pred_fn(img, **kwargs)
# up
if h != size or w != size:
pred = F.interpolate(pred, (h, w),
mode="bilinear",
align_corners=False)
if is_next_volume(path[0]) and len(current_volume_pred) > 0:
# update current volume to dice_sum_class_domain
vol_pred = torch.cat(current_volume_pred, dim=0)
vol_mask = torch.cat(current_volume_mask, dim=0)
result = np.zeros((len(metrics), n_classes))
for cls in range(n_classes):
p, m = pred_mask[cfg["dataset"]](vol_pred, vol_mask, cls)
p = p.cpu().numpy().astype(np.bool8)
m = m.cpu().numpy().astype(np.bool8)
for i, (metric, fn, kwargs) in enumerate(metrics):
if metric not in dist_metric:
result[i, cls] = fn(p, m, **kwargs)
else:
if p.sum() == 0 and m.sum() == 0:
result[i, cls] = 0
elif p.sum() == 0 or m.sum() == 0:
result[i, cls] = dist_penalty
else:
result[i, cls] = fn(p, m, **kwargs)
for i, _ in enumerate(metrics):
results[i].update(result[i], domain)
# clear current volume
current_volume_pred = []
current_volume_mask = []
pred = pred.argmax(dim=1)
mask = convert(mask)
current_volume_pred.append(pred)
current_volume_mask.append(mask)
if save_dir is not None:
im = to_image[cfg["dataset"]](img[0])
lb = to_label(mask[0], n_classes)
pd = to_label(pred[0], n_classes)
# dump as pdf
if cfg["dataset"] == "prostate":
im_kwargs = {"cmap": "gray"}
else:
im_kwargs = {}
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
axes[0].imshow(im, **im_kwargs)
axes[1].imshow(lb)
axes[2].imshow(pd)
fig.tight_layout()
fig.savefig((save_dir / f"{num_samples:04d}.pdf").as_posix())
plt.close(fig)
num_samples += 1
# update last volume
if len(current_volume_pred) > 0:
vol_pred = torch.cat(current_volume_pred, dim=0)
vol_mask = torch.cat(current_volume_mask, dim=0)
result = np.zeros((len(metrics), n_classes))
for cls in range(n_classes):
p, m = pred_mask[cfg["dataset"]](vol_pred, vol_mask, cls)
p = p.cpu().numpy()
m = m.cpu().numpy()
for i, (metric, fn, kwargs) in enumerate(metrics):
if metric not in dist_metric:
result[i, cls] = fn(p, m, **kwargs)
else:
if p.sum() == 0 and m.sum() == 0:
result[i, cls] = 0
elif p.sum() == 0 or m.sum() == 0:
result[i, cls] = dist_penalty
else:
result[i, cls] = fn(p, m, **kwargs)
for i, _ in enumerate(metrics):
results[i].update(result[i], domain)
# clear current volume
current_volume_pred = []
current_volume_mask = []
# get the final result
return results