-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtransformations.py
224 lines (166 loc) · 6.81 KB
/
transformations.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
from functools import partial
from typing import Callable, List, Tuple
import albumentations as A
import numpy as np
from skimage.util import crop
from skimage.util import img_as_ubyte
def normalize_01(inp: np.ndarray):
"""Squash image input to the value range [0, 1] (no clipping)"""
inp_out = (inp - np.min(inp)) / np.ptp(inp)
return inp_out
def normalize(inp: np.ndarray, mean: float, std: float):
"""Normalize based on mean and standard deviation."""
inp_out = (inp - mean) / std
return inp_out
def create_dense_target(tar: np.ndarray):
classes = np.unique(tar)
dummy = np.zeros_like(tar)
for idx, value in enumerate(classes):
mask = np.where(tar == value)
dummy[mask] = idx
return dummy
def center_crop_to_size(
x: np.ndarray,
size: Tuple,
copy: bool = False,
) -> np.ndarray:
"""
Center crops a given array x to the size passed in the function.
Expects even spatial dimensions!
"""
x_shape = np.array(x.shape)
size = np.array(size)
params_list = ((x_shape - size) / 2).astype(np.int).tolist()
params_tuple = tuple([(i, i) for i in params_list])
cropped_image = crop(x, crop_width=params_tuple, copy=copy)
return cropped_image
def re_normalize(inp: np.ndarray, low: int = 0, high: int = 255):
"""Normalize the data to a certain range. Default: [0-255]"""
inp_out = img_as_ubyte(inp)
return inp_out
def random_flip(inp: np.ndarray, tar: np.ndarray, ndim_spatial: int):
flip_dims = [np.random.randint(low=0, high=2) for _ in range(ndim_spatial)]
flip_dims_inp = tuple(
[i + 1 for i, element in enumerate(flip_dims) if element == 1]
)
flip_dims_tar = tuple([i for i, element in enumerate(flip_dims) if element == 1])
inp_flipped = np.flip(inp, axis=flip_dims_inp)
tar_flipped = np.flip(tar, axis=flip_dims_tar)
return inp_flipped, tar_flipped
class Repr:
"""Evaluable string representation of an object"""
def __repr__(self):
return f"{self.__class__.__name__}: {self.__dict__}"
class FunctionWrapperSingle(Repr):
"""A function wrapper that returns a partial for input only."""
def __init__(self, function: Callable, *args, **kwargs):
self.function = partial(function, *args, **kwargs)
def __call__(self, inp: np.ndarray):
return self.function(inp)
class FunctionWrapperDouble(Repr):
"""A function wrapper that returns a partial for an input-target pair."""
def __init__(
self,
function: Callable,
input: bool = True,
target: bool = False,
*args,
**kwargs,
):
self.function = partial(function, *args, **kwargs)
self.input = input
self.target = target
def __call__(self, inp: np.ndarray, tar: dict):
if self.input:
inp = self.function(inp)
if self.target:
tar = self.function(tar)
return inp, tar
class Compose:
"""Baseclass - composes several transforms together."""
def __init__(self, transforms: List[Callable]):
self.transforms = transforms
def __repr__(self):
return str([transform for transform in self.transforms])
class ComposeDouble(Compose):
"""Composes transforms for input-target pairs."""
def __call__(self, inp: np.ndarray, target: dict):
for t in self.transforms:
inp, target = t(inp, target)
return inp, target
class ComposeSingle(Compose):
"""Composes transforms for input only."""
def __call__(self, inp: np.ndarray):
for t in self.transforms:
inp = t(inp)
return inp
class AlbuSeg2d(Repr):
"""
Wrapper for albumentations' segmentation-compatible 2D augmentations.
Wraps an augmentation, so it can be used within the provided transform pipeline.
See https://github.com/albu/albumentations for more information.
Expected input: (C, spatial_dims)
Expected target: (spatial_dims) -> No (C)hannel dimension
"""
def __init__(self, albumentation: Callable):
self.albumentation = albumentation
def __call__(self, inp: np.ndarray, tar: np.ndarray):
# input, target
out_dict = self.albumentation(image=inp, mask=tar)
input_out = out_dict["image"]
target_out = out_dict["mask"]
return input_out, target_out
class AlbuSeg3d(Repr):
"""
Wrapper for albumentations' segmentation-compatible 2D augmentations.
Wraps an augmentation, so it can be used within the provided transform pipeline.
See https://github.com/albu/albumentations for more information.
Expected input: (spatial_dims) -> No (C)hannel dimension
Expected target: (spatial_dims) -> No (C)hannel dimension
Iterates over the slices of an input-target pair stack and performs the same albumentation function.
"""
def __init__(self, albumentation: Callable):
self.albumentation = A.ReplayCompose([albumentation])
def __call__(self, inp: np.ndarray, tar: np.ndarray):
# input, target
tar = tar.astype(np.uint8) # target has to be in uint8
input_copy = np.copy(inp)
target_copy = np.copy(tar)
replay_dict = self.albumentation(image=inp[0])[
"replay"
] # perform an albu on one slice and access the replay dict
# TODO: consider cases with RGB 3D or multimodal 3D input
# only if input_shape == target_shape
for index, (input_slice, target_slice) in enumerate(zip(inp, tar)):
result = A.ReplayCompose.replay(
replay_dict, image=input_slice, mask=target_slice
)
input_copy[index] = result["image"]
target_copy[index] = result["mask"]
return input_copy, target_copy
class RandomFlip(Repr):
"""
Randomly flips spatial input and target dimensions respectively. Spatial
dimensions are considered to occur last in the input/target shape and are
flipped with probability p=0.5 (iid).
Works for 2D and 3D image-target pairs.
Expected input: (C, spatial_dims)
Expected target: (spatial_dims) -> No (C)hannel dimension
Args:
ndim_spatial: Number of spatial dimension in input, e.g.
ndim_spatial=2 for input shape (C, H, W)
ndim_spatial=3 for input shape (C, D, H, W)
"""
def __init__(self, ndim_spatial):
self.ndim_spatial = ndim_spatial
def __call__(self, inp, target):
flip_dims = [np.random.randint(low=0, high=2) for _ in range(self.ndim_spatial)]
flip_dims_inp = tuple(
[i + 1 for i, element in enumerate(flip_dims) if element == 1]
)
flip_dims_target = tuple(
[i for i, element in enumerate(flip_dims) if element == 1]
)
inp_flip = np.flip(inp, axis=flip_dims_inp)
target_flip = np.flip(target, axis=flip_dims_target)
return np.copy(inp_flip), np.copy(target_flip)