/
normalized_gradient_field.py
361 lines (286 loc) · 14.1 KB
/
normalized_gradient_field.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
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
import warnings
from typing import Callable, List, Optional, Sequence, Union, Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.loss import _Loss
from kernels import gauss_kernel_1d, gauss_kernel_2d, gauss_kernel_3d
from kernels import gradient_kernel_1d, gradient_kernel_2d, gradient_kernel_3d
from kernels import spatial_filter_nd
from torch.nn.parameter import Parameter
from monai.utils.enums import LossReduction
def _pair(x):
if hasattr(x, '__getitem__'):
return x
return [x, x]
def _grad_param(ndim, method, axis):
if ndim == 1:
kernel = gradient_kernel_1d(method)
elif ndim == 2:
kernel = gradient_kernel_2d(method, axis)
elif ndim == 3:
kernel = gradient_kernel_3d(method, axis)
else:
raise NotImplementedError
kernel = kernel.reshape(1, 1, *kernel.shape)
return Parameter(torch.Tensor(kernel).float())
def _gauss_param(ndim, sigma, truncate):
if ndim == 1:
kernel = gauss_kernel_1d(sigma, truncate)
elif ndim == 2:
kernel = gauss_kernel_2d(sigma, truncate)
elif ndim == 3:
kernel = gauss_kernel_3d(sigma, truncate)
else:
raise NotImplementedError
kernel = kernel.reshape(1, 1, *kernel.shape)
return Parameter(torch.Tensor(kernel).float())
class NormalizedGradientField2d(_Loss):
"""
Compute the normalized gradient fields defined in:
Haber, Eldad, and Jan Modersitzki. "Intensity gradient based registration and fusion of multi-modal images."
In International Conference on Medical Image Computing and Computer-Assisted Intervention, pp. 726-733. Springer,
Berlin, Heidelberg, 2006.
Häger, Stephanie, et al. "Variable Fraunhofer MEVIS RegLib Comprehensively Applied to Learn2Reg Challenge."
International Conference on Medical Image Computing and Computer-Assisted Intervention. Springer, Cham, 2020.
Adopted from:
https://github.com/yuta-hi/pytorch_similarity
https://github.com/visva89/pTVreg/blob/master/mutils/My/image_metrics/metric_ngf.m
"""
def __init__(self,
grad_method: str = 'default',
gauss_sigma: float = None,
gauss_truncate: float = 4.0,
eps: Optional[float] = 1e-5,
mm_spacing: Optional[Union[int, float, Tuple[int, ...], List[int]]] = None,
reduction: Union[LossReduction, str] = LossReduction.MEAN) -> None:
"""
Args:
grad_method: {'default', 'sobel', 'prewitt', 'isotropic'}
type of gradient kernel. Defaults to 'default' (finite difference).
gauss_sigma: standard deviation from Gaussian kernel. Defaults to None.
gauss_truncate: trunncate the Gaussian kernel at this number of sd. Defaults to 4.0.
eps_src: smooth constant for denominator in computing norm of source/moving gradient
eps_tar: smooth constant for denominator in computing norm of target/fixed gradient
mm_spacing: pixel spacing of input images
reduction: {``"none"``, ``"mean"``, ``"sum"``}
Specifies the reduction to apply to the output. Defaults to ``"mean"``.
- ``"none"``: no reduction will be applied.
- ``"mean"``: the sum of the output will be divided by the number of elements in the output.
- ``"sum"``: the output will be summed.
"""
super().__init__(reduction=LossReduction(reduction).value)
self.eps = eps
if isinstance(mm_spacing, (int, float)):
self.mm_spacing = [mm_spacing] * 2
if isinstance(mm_spacing, (list, tuple)):
if len(mm_spacing) == 2:
self.mm_spacing = mm_spacing
else:
raise ValueError(f'expected length 2 spacing, got {mm_spacing}')
self.grad_method = grad_method
self.gauss_sigma = _pair(gauss_sigma)
self.gauss_truncate = gauss_truncate
self.grad_u_kernel = None
self.grad_v_kernel = None
self.gauss_kernel_x = None
self.gauss_kernel_y = None
self._initialize_params()
self._freeze_params()
def _initialize_params(self):
self._initialize_grad_kernel()
self._initialize_gauss_kernel()
def _initialize_grad_kernel(self):
self.grad_u_kernel = _grad_param(2, self.grad_method, axis=0)
self.grad_v_kernel = _grad_param(2, self.grad_method, axis=1)
def _initialize_gauss_kernel(self):
if self.gauss_sigma[0] is not None:
self.gauss_kernel_x = _gauss_param(2, self.gauss_sigma[0], self.gauss_truncate)
if self.gauss_sigma[1] is not None:
self.gauss_kernel_y = _gauss_param(2, self.gauss_sigma[1], self.gauss_truncate)
def _check_type_forward(self, x: torch.Tensor):
if x.dim() != 4:
raise ValueError(f'expected 4D input (BCHW), (got {x.dim()}D input)')
def _freeze_params(self):
self.grad_u_kernel.requires_grad = False
self.grad_v_kernel.requires_grad = False
if self.gauss_kernel_x is not None:
self.gauss_kernel_x.requires_grad = False
if self.gauss_kernel_y is not None:
self.gauss_kernel_y.requires_grad = False
def forward(self, source, target) -> torch.Tensor:
"""
Args:
source: source/moving image, shape should be BCHW
target: target/fixed image, shape should be BCHW
Returns:
ngf: normalized gradient field between source and target
"""
self._check_type_forward(source)
self._check_type_forward(target)
self._freeze_params()
# reshape
b, c = source.shape[:2]
spatial_shape = source.shape[2:]
# [B*N, H, W]
source = source.view(b * c, 1, *spatial_shape)
target = target.view(b * c, 1, *spatial_shape)
# smoothing
if self.gauss_kernel_x is not None:
source = spatial_filter_nd(source, self.gauss_kernel_x)
if self.gauss_kernel_y is not None:
target = spatial_filter_nd(target, self.gauss_kernel_y)
# gradient
src_grad_u = spatial_filter_nd(source, self.grad_u_kernel) * self.mm_spacing[0]
src_grad_v = spatial_filter_nd(source, self.grad_v_kernel) * self.mm_spacing[1]
tar_grad_u = spatial_filter_nd(target, self.grad_u_kernel) * self.mm_spacing[0]
tar_grad_v = spatial_filter_nd(target, self.grad_v_kernel) * self.mm_spacing[1]
if self.eps is None:
with torch.no_grad():
self.eps = torch.mean(torch.abs(tar_grad_u) + torch.abs(tar_grad_v))
# gradient norm
src_grad_norm = src_grad_u ** 2 + src_grad_v ** 2 + self.eps ** 2
tar_grad_norm = tar_grad_u ** 2 + tar_grad_v ** 2 + self.eps ** 2
# nominator
product = src_grad_u * tar_grad_u + src_grad_v * tar_grad_v
# denominator
denom = src_grad_norm * tar_grad_norm
# integrator
ngf = -0.5 * (product ** 2 / denom)
# reshape back
ngf = ngf.view(b, c, *spatial_shape)
# reduction
if self.reduction == LossReduction.MEAN.value:
ngf = torch.mean(ngf) # the batch and channel average
elif self.reduction == LossReduction.SUM.value:
ngf = torch.sum(ngf) # sum over batch and channel dims
elif self.reduction != LossReduction.NONE.value:
raise ValueError(f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].')
return ngf
class NormalizedGradientField3d(_Loss):
"""
Compute the normalized gradient fields defined in:
Haber, Eldad, and Jan Modersitzki. "Intensity gradient based registration and fusion of multi-modal images."
In International Conference on Medical Image Computing and Computer-Assisted Intervention, pp. 726-733. Springer,
Berlin, Heidelberg, 2006.
Häger, Stephanie, et al. "Variable Fraunhofer MEVIS RegLib Comprehensively Applied to Learn2Reg Challenge."
International Conference on Medical Image Computing and Computer-Assisted Intervention. Springer, Cham, 2020.
Adopted from:
https://github.com/yuta-hi/pytorch_similarity
https://github.com/visva89/pTVreg/blob/master/mutils/My/image_metrics/metric_ngf.m
"""
def __init__(self,
grad_method: str = 'default',
gauss_sigma: float = None,
gauss_truncate: float = 4.0,
eps: Optional[float] = 1e-5,
mm_spacing: Optional[Union[int, float, Tuple[int, ...], List[int]]] = None,
reduction: Union[LossReduction, str] = LossReduction.MEAN) -> None:
"""
Args:
grad_method: {'default', 'sobel', 'prewitt', 'isotropic'}
type of gradient kernel. Defaults to 'default' (finite difference).
gauss_sigma: standard deviation from Gaussian kernel. Defaults to None.
gauss_truncate: trunncate the Gaussian kernel at this number of sd. Defaults to 4.0.
eps_src: smooth constant for denominator in computing norm of source/moving gradient
eps_tar: smooth constant for denominator in computing norm of target/fixed gradient
mm_spacing: pixel spacing of input images
reduction: {``"none"``, ``"mean"``, ``"sum"``}
Specifies the reduction to apply to the output. Defaults to ``"mean"``.
- ``"none"``: no reduction will be applied.
- ``"mean"``: the sum of the output will be divided by the number of elements in the output.
- ``"sum"``: the output will be summed.
"""
super().__init__(reduction=LossReduction(reduction).value)
self.eps = eps
if isinstance(mm_spacing, (int, float)):
self.mm_spacing = [mm_spacing] * 3
if isinstance(mm_spacing, (list, tuple)):
if len(mm_spacing) == 3:
self.mm_spacing = mm_spacing
else:
raise ValueError(f'expected length 2 spacing, got {mm_spacing}')
self.grad_method = grad_method
self.gauss_sigma = _pair(gauss_sigma)
self.gauss_truncate = gauss_truncate
self.grad_u_kernel = None
self.grad_v_kernel = None
self.grad_w_kernel = None
self.gauss_kernel_x = None
self.gauss_kernel_y = None
self._initialize_params()
self._freeze_params()
def _initialize_params(self):
self._initialize_grad_kernel()
self._initialize_gauss_kernel()
def _initialize_grad_kernel(self):
self.grad_u_kernel = _grad_param(3, self.grad_method, axis=0)
self.grad_v_kernel = _grad_param(3, self.grad_method, axis=1)
self.grad_w_kernel = _grad_param(3, self.grad_method, axis=2)
def _initialize_gauss_kernel(self):
if self.gauss_sigma[0] is not None:
self.gauss_kernel_x = _gauss_param(3, self.gauss_sigma[0], self.gauss_truncate)
if self.gauss_sigma[1] is not None:
self.gauss_kernel_y = _gauss_param(3, self.gauss_sigma[1], self.gauss_truncate)
def _check_type_forward(self, x: torch.Tensor):
if x.dim() != 5:
raise ValueError('expected 5D input (got {}D input)'.format(x.dim()))
def _freeze_params(self):
self.grad_u_kernel.requires_grad = False
self.grad_v_kernel.requires_grad = False
self.grad_w_kernel.requires_grad = False
if self.gauss_kernel_x is not None:
self.gauss_kernel_x.requires_grad = False
if self.gauss_kernel_y is not None:
self.gauss_kernel_y.requires_grad = False
def forward(self, source: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
Args:
source: source/moving image, shape should be BCHWD
target: target/fixed image, shape should be BCHWD
Returns:
ngf: normalized gradient field between source and target
"""
self._check_type_forward(source)
self._check_type_forward(target)
self._freeze_params()
# reshape
b, c = source.shape[:2]
spatial_shape = source.shape[2:]
source = source.view(b * c, 1, *spatial_shape)
target = target.view(b * c, 1, *spatial_shape)
# smoothing
if self.gauss_kernel_x is not None:
source = spatial_filter_nd(source, self.gauss_kernel_x)
if self.gauss_kernel_y is not None:
target = spatial_filter_nd(target, self.gauss_kernel_y)
# gradient
src_grad_u = spatial_filter_nd(source, self.grad_u_kernel) * self.mm_spacing[0]
src_grad_v = spatial_filter_nd(source, self.grad_v_kernel) * self.mm_spacing[1]
src_grad_w = spatial_filter_nd(source, self.grad_w_kernel) * self.mm_spacing[2]
tar_grad_u = spatial_filter_nd(target, self.grad_u_kernel) * self.mm_spacing[0]
tar_grad_v = spatial_filter_nd(target, self.grad_v_kernel) * self.mm_spacing[1]
tar_grad_w = spatial_filter_nd(target, self.grad_w_kernel) * self.mm_spacing[2]
if self.eps is None:
with torch.no_grad():
self.eps = torch.mean(torch.abs(src_grad_u) + torch.abs(src_grad_v) + torch.abs(src_grad_w))
# gradient norm
src_grad_norm = src_grad_u ** 2 + src_grad_v ** 2 + src_grad_w ** 2 + self.eps ** 2
tar_grad_norm = tar_grad_u ** 2 + tar_grad_v ** 2 + tar_grad_w ** 2 + self.eps ** 2
# nominator
product = src_grad_u * tar_grad_u + src_grad_v * tar_grad_v + src_grad_w * tar_grad_w
# denominator
denom = src_grad_norm * tar_grad_norm
# integrator
ngf = -0.5 * (product ** 2 / denom)
# reshape back
ngf = ngf.view(b, c, *spatial_shape)
# reduction
if self.reduction == LossReduction.MEAN.value:
ngf = torch.mean(ngf) # the batch and channel average
elif self.reduction == LossReduction.SUM.value:
ngf = torch.sum(ngf) # sum over batch and channel dims
elif self.reduction != LossReduction.NONE.value:
raise ValueError(f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].')
return ngf