-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
212 lines (179 loc) · 8.56 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
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
import os
import torch
import numpy as np
from batchgenerators.utilities.file_and_folder_operations import *
from typing import Union, Tuple, List
from scipy.ndimage.filters import gaussian_filter
from sklearn.model_selection import KFold
def get_split_chd(data_dir, fold, cross_vali_num, seed=12345):
# this is seeded, will be identical each time
all_keys = np.arange(0, 68)
cases = os.listdir(data_dir)
cases.sort()
i = 0
for case in cases:
all_keys[i] = int(case[-4:])
i = i + 1
kf = KFold(n_splits=cross_vali_num, shuffle=True, random_state=seed)
splits = kf.split(all_keys)
for i, (train_idx, test_idx) in enumerate(splits):
train_keys = all_keys[train_idx]
test_keys = all_keys[test_idx]
if i == fold:
break
return train_keys, test_keys
def get_split_mmwhs(fold, cross_vali_num, seed=12345):
# this is seeded, will be identical each time
all_keys = np.arange(1001, 1021)
kf = KFold(n_splits=cross_vali_num, shuffle=True, random_state=seed)
splits = kf.split(all_keys)
for i, (train_idx, test_idx) in enumerate(splits):
train_keys = all_keys[train_idx]
test_keys = all_keys[test_idx]
if i == fold:
break
return train_keys, test_keys
def get_split_acdc(fold, cross_vali_num, seed=12345):
# this is seeded, will be identical each time
kf = KFold(n_splits=cross_vali_num, shuffle=True, random_state=seed)
all_keys = np.arange(1, 101)
splits = kf.split(all_keys)
for i, (train_idx, test_idx) in enumerate(splits):
train_keys = all_keys[train_idx]
test_keys = all_keys[test_idx]
if i == fold:
break
return train_keys, test_keys
def get_split_hvsmr(fold, cross_vali_num, seed=12345):
# this is seeded, will be identical each time
kf = KFold(n_splits=cross_vali_num, shuffle=True, random_state=seed)
all_keys = np.arange(0, 10)
splits = kf.split(all_keys)
for i, (train_idx, test_idx) in enumerate(splits):
train_keys = all_keys[train_idx]
test_keys = all_keys[test_idx]
if i == fold:
break
return train_keys, test_keys
def soft_dice(y_pred, y_true):
# sum over axes
axes = tuple([0] + list(range(2, len(y_pred.size()))))
# y_pred is softmax output of shape (num_samples, num_classes)
# y_true is the label that should be converted to one hot encoding of target (shape= (num_samples, num_classes))
y_onehot = torch.zeros(y_pred.shape).to(y_pred.device)
y_onehot.scatter_(1, y_true, 1)
intersect = (y_pred * y_onehot).sum(dim=axes)
denominator = y_pred.sum(dim=axes) + y_onehot.sum(dim=axes)
dice_scores = 2 * intersect / (denominator + 1e-6)
# we do not count for background dice though
return -1 * dice_scores[1:].mean()
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def pad_nd_image(image, new_shape=None, mode="constant", kwargs=None, return_slicer=False, shape_must_be_divisible_by=None):
"""
one padder to pad them all. Documentation? Well okay. A little bit
:param image: nd image. can be anything
:param new_shape: what shape do you want? new_shape does not have to have the same dimensionality as image. If
len(new_shape) < len(image.shape) then the last axes of image will be padded. If new_shape < image.shape in any of
the axes then we will not pad that axis, but also not crop! (interpret new_shape as new_min_shape)
Example:
image.shape = (10, 1, 512, 512); new_shape = (768, 768) -> result: (10, 1, 768, 768). Cool, huh?
image.shape = (10, 1, 512, 512); new_shape = (364, 768) -> result: (10, 1, 512, 768).
:param mode: see np.pad for documentation
:param return_slicer: if True then this function will also return what coords you will need to use when cropping back
to original shape
:param shape_must_be_divisible_by: for network prediction. After applying new_shape, make sure the new shape is
divisibly by that number (can also be a list with an entry for each axis). Whatever is missing to match that will
be padded (so the result may be larger than new_shape if shape_must_be_divisible_by is not None)
:param kwargs: see np.pad for documentation
"""
if kwargs is None:
kwargs = {'constant_values': 0}
if new_shape is not None:
old_shape = np.array(image.shape[-len(new_shape):])
else:
assert shape_must_be_divisible_by is not None
assert isinstance(shape_must_be_divisible_by, (list, tuple, np.ndarray))
new_shape = image.shape[-len(shape_must_be_divisible_by):]
old_shape = new_shape
num_axes_nopad = len(image.shape) - len(new_shape)
new_shape = [max(new_shape[i], old_shape[i]) for i in range(len(new_shape))]
if not isinstance(new_shape, np.ndarray):
new_shape = np.array(new_shape)
if shape_must_be_divisible_by is not None:
if not isinstance(shape_must_be_divisible_by, (list, tuple, np.ndarray)):
shape_must_be_divisible_by = [shape_must_be_divisible_by] * len(new_shape)
else:
assert len(shape_must_be_divisible_by) == len(new_shape)
for i in range(len(new_shape)):
if new_shape[i] % shape_must_be_divisible_by[i] == 0:
new_shape[i] -= shape_must_be_divisible_by[i]
new_shape = np.array([new_shape[i] + shape_must_be_divisible_by[i] - new_shape[i] % shape_must_be_divisible_by[i] for i in range(len(new_shape))])
difference = new_shape - old_shape
pad_below = difference // 2
pad_above = difference // 2 + difference % 2
pad_list = [[0, 0]]*num_axes_nopad + list([list(i) for i in zip(pad_below, pad_above)])
if not ((all([i == 0 for i in pad_below])) and (all([i == 0 for i in pad_above]))):
res = np.pad(image, pad_list, mode, **kwargs)
else:
res = image
if not return_slicer:
return res
else:
pad_list = np.array(pad_list)
pad_list[:, 1] = np.array(res.shape) - pad_list[:, 1]
slicer = list(slice(*i) for i in pad_list)
return res, slicer
def compute_steps_for_sliding_window(patch_size: Tuple[int, ...], image_size: Tuple[int, ...], step_size: float) -> \
List[List[int]]:
assert [i >= j for i, j in zip(image_size, patch_size)], "image size must be as large or larger than patch_size"
assert 0 < step_size <= 1, 'step_size must be larger than 0 and smaller or equal to 1'
# our step width is patch_size*step_size at most, but can be narrower. For example if we have image size of
# 110, patch size of 32 and step_size of 0.5, then we want to make 4 steps starting at coordinate 0, 27, 55, 78
target_step_sizes_in_voxels = [i * step_size for i in patch_size]
num_steps = [int(np.ceil((i - k) / j)) + 1 for i, j, k in zip(image_size, target_step_sizes_in_voxels, patch_size)]
steps = []
for dim in range(len(patch_size)):
# the highest step value for this dimension is
max_step_value = image_size[dim] - patch_size[dim]
if num_steps[dim] > 1:
actual_step_size = max_step_value / (num_steps[dim] - 1)
else:
actual_step_size = 99999999999 # does not matter because there is only one step at 0
steps_here = [int(np.round(actual_step_size * i)) for i in range(num_steps[dim])]
steps.append(steps_here)
return steps
def get_gaussian(patch_size, sigma_scale=1. / 8) -> np.ndarray:
tmp = np.zeros(patch_size)
center_coords = [i // 2 for i in patch_size]
sigmas = [i * sigma_scale for i in patch_size]
tmp[tuple(center_coords)] = 1
gaussian_importance_map = gaussian_filter(tmp, sigmas, 0, mode='constant', cval=0)
gaussian_importance_map = gaussian_importance_map / np.max(gaussian_importance_map) * 1
gaussian_importance_map = gaussian_importance_map.astype(np.float32)
# gaussian_importance_map cannot be 0, otherwise we may end up with nans!
gaussian_importance_map[gaussian_importance_map == 0] = np.min(
gaussian_importance_map[gaussian_importance_map != 0])
return gaussian_importance_map
__optimizers = {
'SGD': torch.optim.SGD,
'ASGD': torch.optim.ASGD,
'Adam': torch.optim.Adam,
'Adamax': torch.optim.Adamax,
'Adagrad': torch.optim.Adagrad,
'Adadelta': torch.optim.Adadelta,
'Rprop': torch.optim.Rprop,
'RMSprop': torch.optim.RMSprop
}