-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
170 lines (153 loc) · 5.44 KB
/
utils.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
import itertools
import chainer
import numpy as np
import nibabel as nib
def load_nifti(filename, with_affine=False):
"""
load image from NIFTI file
Parameters
----------
filename : str
filename of NIFTI file
with_affine : bool
if True, returns affine parameters
Returns
-------
data : np.ndarray
image data
"""
img = nib.load(filename)
data = img.get_data()
data = np.copy(data, order="C")
if with_affine:
return data, img.affine
return data
def load_sample(df, n, input_shape, output_shape):
"""
randomly sample patch images from DataFrame
Parameters
----------
df : pd.DataFrame
DataFrame containing name of image files
n : int
number of patches to extract
input_shape : list
shape of input patches to extract
output_shape : list
shape of output patches to extract
Returns
-------
images : (n, n_channels, input_shape[0], input_shape[1], ...) ndarray
input patches
labels : (n, output_shape[0], output_shape[1], ...) ndarray
label patches
"""
N = len(df)
if "weight" in list(df):
weights = np.asarray(df["weight"])
weights /= np.sum(weights)
indices = np.random.choice(N, n, replace=True, p=weights)
else:
indices = np.random.choice(N, n, replace=True)
image_files = df["image"][indices]
label_files = df["label"][indices]
images = []
labels = []
for image_file, label_file in zip(image_files, label_files):
image = load_nifti(image_file)
label = load_nifti(label_file).astype(np.int32)
mask = np.int32(label > 0)
slices = [slice(len_ // 2, -len_ // 2) for len_ in input_shape]
mask[slices] *= 2
indices = np.where(mask > 1.5)
i = np.random.choice(len(indices[0]))
input_slices = [
slice(index[i] - len_ // 2, index[i] + len_ // 2)
for index, len_ in zip(indices, input_shape)
]
output_slices = [
slice(index[i] - len_ // 2, index[i] + len_ // 2)
for index, len_ in zip(indices, output_shape)
]
image_patch = image[input_slices]
label_patch = label[output_slices]
image_patch = image_patch.transpose(3, 0, 1, 2)
images.append(image_patch)
labels.append(label_patch)
images = np.array(images)
labels = np.array(labels)
return images, labels
def crop_patch(image, center, shape):
"""
crop patch from an image
Parameters
----------
image : (xlen, ylen, zlen, n_channels) np.ndarray
input image to extract patch from
center : [x, y, z] iterable
center index of a patch
shape : iterable
shape of patch
Returns
-------
patch : (n_channels, xlen, ylen, zlen) np.ndarray
extracted patch
"""
mini = [c - len_ // 2 for c, len_ in zip(center, shape)]
maxi = [c + len_ // 2 for c, len_ in zip(center, shape)]
if all(m >= 0 for m in mini) and all(m < img_len for m, img_len in zip(maxi, image.shape)):
slices = [slice(mi, ma) for mi, ma in zip(mini, maxi)]
else:
slices = [
np.clip(range(mi, ma), 0, img_len - 1)
for mi, ma, img_len in zip(mini, maxi, image.shape)
]
slices = np.meshgrid(*slices, indexing="ij")
patch = image[slices]
patch = patch.transpose(3, 0, 1, 2)
return patch
def dice_coefficients(label1, label2, labels=None):
if labels is None:
labels = np.unique(np.hstack((np.unique(label1), np.unique(label2))))
dice_coefs = []
for label in labels:
match1 = (label1 == label)
match2 = (label2 == label)
denominator = 0.5 * (np.sum(match1.astype(np.float)) + np.sum(match2.astype(np.float)))
numerator = np.sum(np.logical_and(match1, match2).astype(np.float))
if denominator == 0:
dice_coefs.append(0.)
else:
dice_coefs.append(numerator / denominator)
return dice_coefs
def feedforward(model, image, input_shape, output_shape, n_tiles, n_classes):
centers = [[] for _ in range(3)]
for img_len, len_out, center, n_tile in zip(image.shape,
output_shape,
centers,
n_tiles):
if img_len >= len_out * n_tile:
raise ValueError(
f"{img_len} must be smaller than {len_out} x {n_tile}"
)
stride = (img_len - len_out) // (n_tile - 1)
center.append(len_out // 2)
for i in range(n_tile - 2):
center.append(center[-1] + stride)
center.append(img_len - len_out // 2)
output = np.zeros((n_classes,) + image.shape[:-1])
for x, y, z in itertools.product(*centers):
patch = crop_patch(image, [x, y, z], input_shape)
patch = np.expand_dims(patch, 0)
patch = model.xp.asarray(patch)
slices_out = [slice(None)] + [
slice(center - len_out // 2, center + len_out // 2)
for len_out, center in zip(output_shape, [x, y, z])
]
slices_in = [0, slice(None)] + [
slice((len_in - len_out) // 2, (len_out - len_in) // 2)
for len_out, len_in, in zip(output_shape, input_shape)
]
with chainer.no_backprop_mode():
output[slices_out] += chainer.cuda.to_cpu(model(patch).data)[slices_in]
return output