-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·447 lines (380 loc) · 16.1 KB
/
Copy pathutils.py
File metadata and controls
executable file
·447 lines (380 loc) · 16.1 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import math
import numpy as np
import torch.nn.functional as F
import torch, sys
from torch import nn
import pystrum.pynd.ndutils as nd
from scipy.ndimage import gaussian_filter
from skimage.metrics import structural_similarity as ssim
import scipy.ndimage as ndi
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
self.vals = []
self.std = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.vals.append(val)
self.std = np.std(self.vals)
def pad_image(img, target_size):
rows_to_pad = max(target_size[0] - img.shape[2], 0)
cols_to_pad = max(target_size[1] - img.shape[3], 0)
slcs_to_pad = max(target_size[2] - img.shape[4], 0)
padded_img = F.pad(img, (0, slcs_to_pad, 0, cols_to_pad, 0, rows_to_pad), "constant", 0)
return padded_img
class SpatialTransformer(nn.Module):
"""
N-D Spatial Transformer
"""
def __init__(self, size, mode='bilinear', padding_mode='zeros'):
super().__init__()
self.size = size
self.mode = mode
self.padding_mode = padding_mode
# create sampling grid
vectors = [torch.arange(0, s) for s in size]
grids = torch.meshgrid(vectors)
grid = torch.stack(grids)
grid = torch.unsqueeze(grid, 0)
grid = grid.type(torch.FloatTensor).cuda()
self.register_buffer('grid', grid)
self.padding_mode = padding_mode
def forward(self, src, flow):
# new locations
new_locs = self.grid + flow
shape = flow.shape[2:]
# need to normalize grid values to [-1, 1] for resampler
for i in range(len(shape)):
new_locs[:, i, ...] = 2 * (new_locs[:, i, ...] / (shape[i] - 1) - 0.5)
# move channels dim to last position
# also not sure why, but the channels need to be reversed
if len(shape) == 2:
new_locs = new_locs.permute(0, 2, 3, 1)
new_locs = new_locs[..., [1, 0]]
elif len(shape) == 3:
new_locs = new_locs.permute(0, 2, 3, 4, 1)
new_locs = new_locs[..., [2, 1, 0]]
return F.grid_sample(src, new_locs, align_corners=True, mode=self.mode, padding_mode=self.padding_mode)
def distance_transformation(src, flow, grid, input_mode='bilinear', padding_mode='zeros'):
"""
Transform binary mask using distance transform and morphological operations.
Args:
src (Tensor): Source binary mask tensor (N, C, D, H, W)
flow (Tensor): Deformation field (N, 3, D, H, W)
grid (Tensor): Base sampling grid
mode (str): Interpolation mode ('bilinear' or 'nearest')
padding_mode (str): Padding mode for out-of-bound values
Returns:
Tensor: Transformed binary mask
"""
device = src.device
batch_size = src.size(0)
# Convert to numpy for distance transform
src_np = src.cpu().numpy()
transformed_masks = []
for b in range(batch_size):
# Get single mask
mask = src_np[b, 0] # Assuming channel dim is 1
# Compute distance transform (both positive and negative)
dist_pos = ndi.distance_transform_edt(mask)
dist_neg = ndi.distance_transform_edt(1 - mask)
dist = dist_pos - dist_neg
# Normalize distances to [-1, 1] range
max_dist = max(dist.max(), dist_neg.max())
dist = dist / max_dist
# Convert back to tensor
dist_tensor = torch.from_numpy(dist).float().to(device)
dist_tensor = dist_tensor.unsqueeze(0).unsqueeze(0) # Add batch and channel dims
# Calculate new sampling locations
new_locs = grid + flow[b:b+1]
shape = flow.shape[2:]
# Normalize grid values to [-1, 1]
for i in range(len(shape)):
new_locs[:, i, ...] = 2 * (new_locs[:, i, ...] / (shape[i] - 1) - 0.5)
# Permute dimensions for grid_sample
if len(shape) == 3:
new_locs = new_locs.permute(0, 2, 3, 4, 1)
new_locs = new_locs[..., [2, 1, 0]]
# Transform the distance field
transformed_dist = F.grid_sample(dist_tensor, new_locs,
align_corners=True,
mode=input_mode,
padding_mode=padding_mode)
transformed_mask = (transformed_dist > 0).float()
# Morphological operations to clean up the result
transformed_mask_np = transformed_mask.cpu().numpy()[0, 0]
# Fill holes
transformed_mask_np = ndi.binary_fill_holes(transformed_mask_np)
# Convert back to tensor
transformed_mask = torch.from_numpy(transformed_mask_np).float().to(device)
transformed_masks.append(transformed_mask)
# Stack all transformed masks
result = torch.stack(transformed_masks, dim=0).unsqueeze(1)
return result
class register_model(nn.Module):
def __init__(self, img_size=(128, 256, 256), mode='bilinear', padding_mode='zeros',
use_distance_transform=False):
"""
Args:
img_size: Size of the input image
mode: Interpolation mode ('bilinear' or 'nearest')
padding_mode: Padding mode for out-of-bound values
use_binary_transform: Whether to use special binary mask transformation
"""
super(register_model, self).__init__()
self.spatial_trans = SpatialTransformer(img_size, mode, padding_mode)
self.use_binary_transform = use_distance_transform
if use_distance_transform:
vectors = [torch.arange(0, s) for s in img_size]
grids = torch.meshgrid(vectors)
grid = torch.stack(grids)
grid = torch.unsqueeze(grid, 0)
self.register_buffer('binary_grid', grid.type(torch.FloatTensor))
def forward(self, x):
img = x[0].cuda()
flow = x[1].cuda()
if self.use_binary_transform and (img.dtype == torch.bool or (img.max() == 1 and img.min() == 0)):
out = distance_transformation(img, flow, self.binary_grid.cuda(),
"bilinear",
self.spatial_trans.padding_mode)
else:
out = self.spatial_trans(img, flow)
return out
def DSC(pred, target):
smooth = 1e-5
m1 = pred.flatten()
m2 = target.flatten()
intersection = (m1 * m2).sum()
return (2. * intersection + smooth) / (m1.sum() + m2.sum() + smooth)
def dice_val(y_pred, y_true, num_clus):
if not torch.equal(torch.sort(torch.unique(y_true))[0], torch.sort(torch.unique(y_pred))[0]):
raise ValueError("GT and Pred do not contain the same unique elements")
if isinstance(y_pred, torch.Tensor):
y_pred = y_pred.cpu().detach().numpy()
if isinstance(y_true, torch.Tensor):
y_true = y_true.cpu().detach().numpy()
cls_lst = [1]
dice_lst = []
for cls in cls_lst:
dice = DSC(y_true == cls, y_pred == cls)
dice_lst.append(dice)
return np.mean(dice_lst)
def ssim_val(y_pred, y_true):
y_pred_np = y_pred.cpu().detach().numpy()
y_true_np = y_true.cpu().detach().numpy()
batch_ssim = []
for i in range(y_pred_np.shape[0]):
ssim_value = ssim(y_true_np[i, 0], y_pred_np[i, 0], data_range=y_pred_np[i, 0].max() - y_pred_np[i, 0].min())
batch_ssim.append(ssim_value)
return np.mean(batch_ssim)
def jacobian_determinant_vxm(disp):
"""
jacobian determinant of a displacement field.
NB: to compute the spatial gradients, we use np.gradient.
Parameters:
disp: 2D or 3D displacement field of size [*vol_shape, nb_dims],
where vol_shape is of len nb_dims
Returns:
jacobian determinant (scalar)
"""
# check inputs
disp = disp.transpose(1, 2, 3, 0)
volshape = disp.shape[:-1]
nb_dims = len(volshape)
assert len(volshape) in (2, 3), 'flow has to be 2D or 3D'
# compute grid
grid_lst = nd.volsize2ndgrid(volshape)
grid = np.stack(grid_lst, len(volshape))
# compute gradients
J = np.gradient(disp + grid)
# 3D glow
if nb_dims == 3:
dx = J[0]
dy = J[1]
dz = J[2]
# compute jacobian components
Jdet0 = dx[..., 0] * (dy[..., 1] * dz[..., 2] - dy[..., 2] * dz[..., 1])
Jdet1 = dx[..., 1] * (dy[..., 0] * dz[..., 2] - dy[..., 2] * dz[..., 0])
Jdet2 = dx[..., 2] * (dy[..., 0] * dz[..., 1] - dy[..., 1] * dz[..., 0])
return Jdet0 - Jdet1 + Jdet2
else: # must be 2
dfdx = J[0]
dfdy = J[1]
return dfdx[..., 0] * dfdy[..., 1] - dfdy[..., 0] * dfdx[..., 1]
import re
def process_label():
#process labeling information for FreeSurfer
seg_table = [0, 2, 3, 4, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 24, 26,
28, 30, 31, 41, 42, 43, 44, 46, 47, 49, 50, 51, 52, 53, 54, 58, 60, 62,
63, 72, 77, 80, 85, 251, 252, 253, 254, 255]
file1 = open('label_info.txt', 'r')
Lines = file1.readlines()
dict = {}
seg_i = 0
seg_look_up = []
for seg_label in seg_table:
for line in Lines:
line = re.sub(' +', ' ',line).split(' ')
try:
int(line[0])
except:
continue
if int(line[0]) == seg_label:
seg_look_up.append([seg_i, int(line[0]), line[1]])
dict[seg_i] = line[1]
seg_i += 1
return dict
def write2csv(line, name):
with open(name+'.csv', 'a') as file:
file.write(line)
file.write('\n')
def dice_val_substruct(y_pred, y_true, std_idx):
with torch.no_grad():
y_pred = nn.functional.one_hot(y_pred, num_classes=46)
y_pred = torch.squeeze(y_pred, 1)
y_pred = y_pred.permute(0, 4, 1, 2, 3).contiguous()
y_true = nn.functional.one_hot(y_true, num_classes=46)
y_true = torch.squeeze(y_true, 1)
y_true = y_true.permute(0, 4, 1, 2, 3).contiguous()
y_pred = y_pred.detach().cpu().numpy()
y_true = y_true.detach().cpu().numpy()
line = 'p_{}'.format(std_idx)
for i in range(46):
pred_clus = y_pred[0, i, ...]
true_clus = y_true[0, i, ...]
intersection = pred_clus * true_clus
intersection = intersection.sum()
union = pred_clus.sum() + true_clus.sum()
dsc = (2.*intersection) / (union + 1e-5)
line = line+','+str(dsc)
return line
def dice(y_pred, y_true, ):
intersection = y_pred * y_true
intersection = np.sum(intersection)
union = np.sum(y_pred) + np.sum(y_true)
dsc = (2.*intersection) / (union + 1e-5)
return dsc
def smooth_seg(binary_img, sigma=1.5, thresh=0.4):
binary_img = gaussian_filter(binary_img.astype(np.float32()), sigma=sigma)
binary_img = binary_img > thresh
return binary_img
def get_mc_preds(net, inputs, mc_iter: int = 25):
"""Convenience fn. for MC integration for uncertainty estimation.
Args:
net: DIP model (can be standard, MFVI or MCDropout)
inputs: input to net
mc_iter: number of MC samples
post_processor: process output of net before computing loss (e.g. downsampler in SR)
mask: multiply output and target by mask before computing loss (for inpainting)
"""
img_list = []
flow_list = []
with torch.no_grad():
for _ in range(mc_iter):
img, flow = net(inputs)
img_list.append(img)
flow_list.append(flow)
return img_list, flow_list
def calc_uncert(tar, img_list):
sqr_diffs = []
for i in range(len(img_list)):
sqr_diff = (img_list[i] - tar)**2
sqr_diffs.append(sqr_diff)
uncert = torch.mean(torch.cat(sqr_diffs, dim=0)[:], dim=0, keepdim=True)
return uncert
def calc_error(tar, img_list):
sqr_diffs = []
for i in range(len(img_list)):
sqr_diff = (img_list[i] - tar)**2
sqr_diffs.append(sqr_diff)
uncert = torch.mean(torch.cat(sqr_diffs, dim=0)[:], dim=0, keepdim=True)
return uncert
def get_mc_preds_w_errors(net, inputs, target, mc_iter: int = 25):
"""Convenience fn. for MC integration for uncertainty estimation.
Args:
net: DIP model (can be standard, MFVI or MCDropout)
inputs: input to net
mc_iter: number of MC samples
post_processor: process output of net before computing loss (e.g. downsampler in SR)
mask: multiply output and target by mask before computing loss (for inpainting)
"""
img_list = []
flow_list = []
MSE = nn.MSELoss()
err = []
with torch.no_grad():
for _ in range(mc_iter):
img, flow = net(inputs)
img_list.append(img)
flow_list.append(flow)
err.append(MSE(img, target).item())
return img_list, flow_list, err
def get_diff_mc_preds(net, inputs, mc_iter: int = 25):
"""Convenience fn. for MC integration for uncertainty estimation.
Args:
net: DIP model (can be standard, MFVI or MCDropout)
inputs: input to net
mc_iter: number of MC samples
post_processor: process output of net before computing loss (e.g. downsampler in SR)
mask: multiply output and target by mask before computing loss (for inpainting)
"""
img_list = []
flow_list = []
disp_list = []
with torch.no_grad():
for _ in range(mc_iter):
img, _, flow, disp = net(inputs)
img_list.append(img)
flow_list.append(flow)
disp_list.append(disp)
return img_list, flow_list, disp_list
def uncert_regression_gal(img_list, reduction = 'mean'):
img_list = torch.cat(img_list, dim=0)
mean = img_list[:,:-1].mean(dim=0, keepdim=True)
ale = img_list[:,-1:].mean(dim=0, keepdim=True)
epi = torch.var(img_list[:,:-1], dim=0, keepdim=True)
#if epi.shape[1] == 3:
epi = epi.mean(dim=1, keepdim=True)
uncert = ale + epi
if reduction == 'mean':
return ale.mean().item(), epi.mean().item(), uncert.mean().item()
elif reduction == 'sum':
return ale.sum().item(), epi.sum().item(), uncert.sum().item()
else:
return ale.detach(), epi.detach(), uncert.detach()
def uceloss(errors, uncert, n_bins=15, outlier=0.0, range=None):
device = errors.device
if range == None:
bin_boundaries = torch.linspace(uncert.min().item(), uncert.max().item(), n_bins + 1, device=device)
else:
bin_boundaries = torch.linspace(range[0], range[1], n_bins + 1, device=device)
bin_lowers = bin_boundaries[:-1]
bin_uppers = bin_boundaries[1:]
errors_in_bin_list = []
avg_uncert_in_bin_list = []
prop_in_bin_list = []
uce = torch.zeros(1, device=device)
for bin_lower, bin_upper in zip(bin_lowers, bin_uppers):
# Calculated |uncertainty - error| in each bin
in_bin = uncert.gt(bin_lower.item()) * uncert.le(bin_upper.item())
prop_in_bin = in_bin.float().mean() # |Bm| / n
prop_in_bin_list.append(prop_in_bin)
if prop_in_bin.item() > outlier:
errors_in_bin = errors[in_bin].float().mean() # err()
avg_uncert_in_bin = uncert[in_bin].mean() # uncert()
uce += torch.abs(avg_uncert_in_bin - errors_in_bin) * prop_in_bin
errors_in_bin_list.append(errors_in_bin)
avg_uncert_in_bin_list.append(avg_uncert_in_bin)
err_in_bin = torch.tensor(errors_in_bin_list, device=device)
avg_uncert_in_bin = torch.tensor(avg_uncert_in_bin_list, device=device)
prop_in_bin = torch.tensor(prop_in_bin_list, device=device)
return uce, err_in_bin, avg_uncert_in_bin, prop_in_bin