Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
178 lines (117 loc) · 5.02 KB
/
Copy pathutils.py
File metadata and controls
178 lines (117 loc) · 5.02 KB
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
#!/usr/bin/env python3
# MIT License
# Copyright (c) 2025 Hoel Kervadec
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from pathlib import Path
from functools import partial
from multiprocessing import Pool
from contextlib import AbstractContextManager
from typing import Callable, Iterable, List, Set, Tuple, TypeVar, cast
import torch
import numpy as np
from PIL import Image
from tqdm import tqdm
from torch import Tensor, einsum
tqdm_ = partial(tqdm, dynamic_ncols=True,
leave=True,
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{rate_fmt}{postfix}]')
class Dcm(AbstractContextManager):
# Dummy Context manager
def __exit__(self, *args, **kwargs):
pass
# Functools
A = TypeVar("A")
B = TypeVar("B")
def map_(fn: Callable[[A], B], iter: Iterable[A]) -> List[B]:
return list(map(fn, iter))
def mmap_(fn: Callable[[A], B], iter: Iterable[A]) -> List[B]:
return Pool().map(fn, iter)
def starmmap_(fn: Callable[[Tuple[A]], B], iter: Iterable[Tuple[A]]) -> List[B]:
return Pool().starmap(fn, iter)
# Assert utils
def uniq(a: Tensor) -> Set:
return set(torch.unique(a.cpu()).numpy())
def sset(a: Tensor, sub: Iterable) -> bool:
return uniq(a).issubset(sub)
def eq(a: Tensor, b) -> bool:
return torch.eq(a, b).all()
def simplex(t: Tensor, axis=1) -> bool:
_sum = cast(Tensor, t.sum(axis).type(torch.float32))
_ones = torch.ones_like(_sum, dtype=torch.float32)
return torch.allclose(_sum, _ones)
def one_hot(t: Tensor, axis=1) -> bool:
return simplex(t, axis) and sset(t, [0, 1])
def class2one_hot(seg: Tensor, K: int) -> Tensor:
# Breaking change but otherwise can't deal with both 2d and 3d
# if len(seg.shape) == 3: # Only w, h, d, used by the dataloader
# return class2one_hot(seg.unsqueeze(dim=0), K)[0]
assert sset(seg, list(range(K))), (uniq(seg), K)
b, *img_shape = seg.shape
device = seg.device
res = torch.zeros((b, K, *img_shape), dtype=torch.int32, device=device).scatter_(1, seg[:, None, ...], 1)
assert res.shape == (b, K, *img_shape)
assert one_hot(res)
return res
def probs2class(probs: Tensor) -> Tensor:
b, _, *img_shape = probs.shape
assert simplex(probs)
res = probs.argmax(dim=1)
assert res.shape == (b, *img_shape)
return res
def probs2one_hot(probs: Tensor) -> Tensor:
_, K, *_ = probs.shape
assert simplex(probs)
res = class2one_hot(probs2class(probs), K)
assert res.shape == probs.shape
assert one_hot(res)
return res
# Save the raw predictions
def save_images(segs: Tensor, names: Iterable[str], root: Path) -> None:
for seg, name in zip(segs, names):
save_path = (root / name).with_suffix(".png")
save_path.parent.mkdir(parents=True, exist_ok=True)
if len(seg.shape) == 2:
Image.fromarray(seg.detach().cpu().numpy().astype(np.uint8)).save(save_path)
elif len(seg.shape) == 3:
np.save(str(save_path), seg.detach().cpu().numpy())
else:
raise ValueError(seg.shape)
# Metrics
def meta_dice(sum_str: str, label: Tensor, pred: Tensor, smooth: float = 1e-8) -> Tensor:
assert label.shape == pred.shape
assert one_hot(label)
assert one_hot(pred)
inter_size: Tensor = einsum(sum_str, [intersection(label, pred)]).type(torch.float32)
sum_sizes: Tensor = (einsum(sum_str, [label]) + einsum(sum_str, [pred])).type(torch.float32)
dices: Tensor = (2 * inter_size + smooth) / (sum_sizes + smooth)
return dices
dice_coef = partial(meta_dice, "bk...->bk")
dice_batch = partial(meta_dice, "bk...->k") # used for 3d dice
def intersection(a: Tensor, b: Tensor) -> Tensor:
assert a.shape == b.shape
assert sset(a, [0, 1])
assert sset(b, [0, 1])
res = a & b
assert sset(res, [0, 1])
return res
def union(a: Tensor, b: Tensor) -> Tensor:
assert a.shape == b.shape
assert sset(a, [0, 1])
assert sset(b, [0, 1])
res = a | b
assert sset(res, [0, 1])
return res