diffct-mlx 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffct_mlx/__about__.py +3 -0
- diffct_mlx/__init__.py +223 -0
- diffct_mlx/constants.py +38 -0
- diffct_mlx/geometry.py +802 -0
- diffct_mlx/kernels/__init__.py +39 -0
- diffct_mlx/kernels/cone_beam.py +708 -0
- diffct_mlx/kernels/fan_beam.py +502 -0
- diffct_mlx/kernels/parallel_beam.py +431 -0
- diffct_mlx/phantoms/__init__.py +5 -0
- diffct_mlx/phantoms/shepp_logan.py +118 -0
- diffct_mlx/projectors.py +1084 -0
- diffct_mlx/real_measured_data_helper.py +601 -0
- diffct_mlx/reconstruction_algorithms/__init__.py +85 -0
- diffct_mlx/reconstruction_algorithms/_analytic.py +101 -0
- diffct_mlx/reconstruction_algorithms/_core.py +872 -0
- diffct_mlx/reconstruction_algorithms/asd_pocs.py +114 -0
- diffct_mlx/reconstruction_algorithms/awtv_pocs.py +106 -0
- diffct_mlx/reconstruction_algorithms/cases.py +988 -0
- diffct_mlx/reconstruction_algorithms/dart.py +424 -0
- diffct_mlx/reconstruction_algorithms/fbp.py +69 -0
- diffct_mlx/reconstruction_algorithms/fdk.py +67 -0
- diffct_mlx/reconstruction_algorithms/sart.py +66 -0
- diffct_mlx/reconstruction_algorithms/sirt.py +64 -0
- diffct_mlx/reconstruction_algorithms/tv_pocs.py +85 -0
- diffct_mlx/regularizers.py +122 -0
- diffct_mlx/tv_gradients.py +74 -0
- diffct_mlx/utils.py +64 -0
- diffct_mlx-1.0.1.dist-info/METADATA +315 -0
- diffct_mlx-1.0.1.dist-info/RECORD +31 -0
- diffct_mlx-1.0.1.dist-info/WHEEL +4 -0
- diffct_mlx-1.0.1.dist-info/licenses/LICENSE +201 -0
diffct_mlx/projectors.py
ADDED
|
@@ -0,0 +1,1084 @@
|
|
|
1
|
+
"""MLX differentiable projector functions for CT reconstruction.
|
|
2
|
+
|
|
3
|
+
This module provides differentiable forward projection and backprojection
|
|
4
|
+
functions using custom Metal kernels on Apple Silicon, with VJP (vector-Jacobian
|
|
5
|
+
product) support for gradient computation via ``mx.custom_function``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
import mlx.core as mx
|
|
12
|
+
|
|
13
|
+
from .constants import _MX_DTYPE
|
|
14
|
+
from .utils import _grid_2d, _grid_3d
|
|
15
|
+
from .kernels import (
|
|
16
|
+
parallel_2d_forward_kernel,
|
|
17
|
+
parallel_2d_backward_kernel,
|
|
18
|
+
parallel_2d_footprint_forward_kernel,
|
|
19
|
+
parallel_2d_footprint_backward_kernel,
|
|
20
|
+
fan_2d_forward_kernel,
|
|
21
|
+
fan_2d_backward_kernel,
|
|
22
|
+
fan_2d_footprint_forward_kernel,
|
|
23
|
+
fan_2d_footprint_backward_kernel,
|
|
24
|
+
cone_3d_forward_kernel,
|
|
25
|
+
cone_3d_backward_kernel,
|
|
26
|
+
cone_3d_footprint_forward_kernel,
|
|
27
|
+
cone_3d_footprint_backward_kernel,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _as_mx_float_array(value):
|
|
32
|
+
"""Accept NumPy or MLX arrays and return an MLX float array."""
|
|
33
|
+
return mx.array(value, dtype=_MX_DTYPE)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@lru_cache(maxsize=32)
|
|
37
|
+
def _projector_params_2d(
|
|
38
|
+
n_ang: int,
|
|
39
|
+
n_det: int,
|
|
40
|
+
nx: int,
|
|
41
|
+
ny: int,
|
|
42
|
+
detector_spacing: float,
|
|
43
|
+
cx: float,
|
|
44
|
+
cy: float,
|
|
45
|
+
voxel_spacing: float,
|
|
46
|
+
):
|
|
47
|
+
"""Cache small 2D kernel parameter buffers across repeated projector calls."""
|
|
48
|
+
return (
|
|
49
|
+
mx.array([n_ang, n_det, nx, ny], dtype=mx.int32),
|
|
50
|
+
mx.array([detector_spacing, cx, cy, voxel_spacing], dtype=mx.float32),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@lru_cache(maxsize=32)
|
|
55
|
+
def _projector_params_3d(
|
|
56
|
+
n_views: int,
|
|
57
|
+
n_u: int,
|
|
58
|
+
n_v: int,
|
|
59
|
+
nx: int,
|
|
60
|
+
ny: int,
|
|
61
|
+
nz: int,
|
|
62
|
+
du: float,
|
|
63
|
+
dv: float,
|
|
64
|
+
cx: float,
|
|
65
|
+
cy: float,
|
|
66
|
+
cz: float,
|
|
67
|
+
voxel_spacing: float,
|
|
68
|
+
):
|
|
69
|
+
"""Cache small 3D kernel parameter buffers across repeated projector calls."""
|
|
70
|
+
return (
|
|
71
|
+
mx.array([n_views, n_u, n_v, nx, ny, nz], dtype=mx.int32),
|
|
72
|
+
mx.array([du, dv, cx, cy, cz, voxel_spacing], dtype=mx.float32),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# ============================================================================
|
|
77
|
+
# 2D Parallel Beam
|
|
78
|
+
# ============================================================================
|
|
79
|
+
|
|
80
|
+
def _parallel_forward_impl(image, ray_dir, det_origin, det_u_vec,
|
|
81
|
+
num_detectors, detector_spacing=1.0, voxel_spacing=1.0):
|
|
82
|
+
"""Raw parallel beam forward projection (no VJP)."""
|
|
83
|
+
image = _as_mx_float_array(image)
|
|
84
|
+
ray_dir = _as_mx_float_array(ray_dir)
|
|
85
|
+
det_origin = _as_mx_float_array(det_origin)
|
|
86
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
87
|
+
|
|
88
|
+
Ny, Nx = image.shape
|
|
89
|
+
n_ang = ray_dir.shape[0]
|
|
90
|
+
n_det = int(num_detectors)
|
|
91
|
+
|
|
92
|
+
cx = float(Nx * 0.5)
|
|
93
|
+
cy = float(Ny * 0.5)
|
|
94
|
+
|
|
95
|
+
params, fparams = _projector_params_2d(
|
|
96
|
+
n_ang,
|
|
97
|
+
n_det,
|
|
98
|
+
Nx,
|
|
99
|
+
Ny,
|
|
100
|
+
float(detector_spacing),
|
|
101
|
+
cx,
|
|
102
|
+
cy,
|
|
103
|
+
float(voxel_spacing),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
grid, tg = _grid_2d(n_ang, n_det)
|
|
107
|
+
|
|
108
|
+
outputs = parallel_2d_forward_kernel(
|
|
109
|
+
inputs=[image, ray_dir, det_origin, det_u_vec, params, fparams],
|
|
110
|
+
output_shapes=[(n_ang, n_det)],
|
|
111
|
+
output_dtypes=[_MX_DTYPE],
|
|
112
|
+
grid=grid,
|
|
113
|
+
threadgroup=tg,
|
|
114
|
+
)
|
|
115
|
+
return outputs[0]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _parallel_backward_impl(sinogram, ray_dir, det_origin, det_u_vec,
|
|
119
|
+
detector_spacing, H, W, voxel_spacing=1.0):
|
|
120
|
+
"""Raw parallel beam backprojection (no VJP)."""
|
|
121
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
122
|
+
ray_dir = _as_mx_float_array(ray_dir)
|
|
123
|
+
det_origin = _as_mx_float_array(det_origin)
|
|
124
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
125
|
+
|
|
126
|
+
n_ang, n_det = sinogram.shape
|
|
127
|
+
Ny, Nx = int(H), int(W)
|
|
128
|
+
|
|
129
|
+
cx = float(Nx * 0.5)
|
|
130
|
+
cy = float(Ny * 0.5)
|
|
131
|
+
|
|
132
|
+
params, fparams = _projector_params_2d(
|
|
133
|
+
n_ang,
|
|
134
|
+
n_det,
|
|
135
|
+
Nx,
|
|
136
|
+
Ny,
|
|
137
|
+
float(detector_spacing),
|
|
138
|
+
cx,
|
|
139
|
+
cy,
|
|
140
|
+
float(voxel_spacing),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
grid, tg = _grid_2d(n_ang, n_det)
|
|
144
|
+
|
|
145
|
+
outputs = parallel_2d_backward_kernel(
|
|
146
|
+
inputs=[sinogram, ray_dir, det_origin, det_u_vec, params, fparams],
|
|
147
|
+
output_shapes=[(Ny, Nx)],
|
|
148
|
+
output_dtypes=[_MX_DTYPE],
|
|
149
|
+
grid=grid,
|
|
150
|
+
threadgroup=tg,
|
|
151
|
+
init_value=0,
|
|
152
|
+
)
|
|
153
|
+
return outputs[0]
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _parallel_forward_footprint_impl(image, ray_dir, det_origin, det_u_vec,
|
|
157
|
+
num_detectors, detector_spacing=1.0, voxel_spacing=1.0):
|
|
158
|
+
"""Approximate separable-footprint parallel-beam forward projector."""
|
|
159
|
+
image = _as_mx_float_array(image)
|
|
160
|
+
ray_dir = _as_mx_float_array(ray_dir)
|
|
161
|
+
det_origin = _as_mx_float_array(det_origin)
|
|
162
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
163
|
+
|
|
164
|
+
Ny, Nx = image.shape
|
|
165
|
+
n_ang = ray_dir.shape[0]
|
|
166
|
+
n_det = int(num_detectors)
|
|
167
|
+
|
|
168
|
+
cx = float(Nx * 0.5)
|
|
169
|
+
cy = float(Ny * 0.5)
|
|
170
|
+
params, fparams = _projector_params_2d(
|
|
171
|
+
n_ang,
|
|
172
|
+
n_det,
|
|
173
|
+
Nx,
|
|
174
|
+
Ny,
|
|
175
|
+
float(detector_spacing),
|
|
176
|
+
cx,
|
|
177
|
+
cy,
|
|
178
|
+
float(voxel_spacing),
|
|
179
|
+
)
|
|
180
|
+
grid, tg = _grid_2d(n_ang, Ny)
|
|
181
|
+
|
|
182
|
+
outputs = parallel_2d_footprint_forward_kernel(
|
|
183
|
+
inputs=[image, ray_dir, det_origin, det_u_vec, params, fparams],
|
|
184
|
+
output_shapes=[(n_ang, n_det)],
|
|
185
|
+
output_dtypes=[_MX_DTYPE],
|
|
186
|
+
grid=grid,
|
|
187
|
+
threadgroup=tg,
|
|
188
|
+
init_value=0,
|
|
189
|
+
)
|
|
190
|
+
return outputs[0]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _parallel_backward_footprint_impl(sinogram, ray_dir, det_origin, det_u_vec,
|
|
194
|
+
detector_spacing, H, W, voxel_spacing=1.0):
|
|
195
|
+
"""Adjoint of the approximate separable-footprint parallel-beam projector."""
|
|
196
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
197
|
+
ray_dir = _as_mx_float_array(ray_dir)
|
|
198
|
+
det_origin = _as_mx_float_array(det_origin)
|
|
199
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
200
|
+
|
|
201
|
+
n_ang, n_det = sinogram.shape
|
|
202
|
+
Ny, Nx = int(H), int(W)
|
|
203
|
+
|
|
204
|
+
cx = float(Nx * 0.5)
|
|
205
|
+
cy = float(Ny * 0.5)
|
|
206
|
+
params, fparams = _projector_params_2d(
|
|
207
|
+
n_ang,
|
|
208
|
+
n_det,
|
|
209
|
+
Nx,
|
|
210
|
+
Ny,
|
|
211
|
+
float(detector_spacing),
|
|
212
|
+
cx,
|
|
213
|
+
cy,
|
|
214
|
+
float(voxel_spacing),
|
|
215
|
+
)
|
|
216
|
+
grid, tg = _grid_2d(Nx, Ny)
|
|
217
|
+
|
|
218
|
+
outputs = parallel_2d_footprint_backward_kernel(
|
|
219
|
+
inputs=[sinogram, ray_dir, det_origin, det_u_vec, params, fparams],
|
|
220
|
+
output_shapes=[(Ny, Nx)],
|
|
221
|
+
output_dtypes=[_MX_DTYPE],
|
|
222
|
+
grid=grid,
|
|
223
|
+
threadgroup=tg,
|
|
224
|
+
)
|
|
225
|
+
return outputs[0]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
@mx.custom_function
|
|
229
|
+
def parallel_forward(image, ray_dir, det_origin, det_u_vec,
|
|
230
|
+
num_detectors=128, detector_spacing=1.0, voxel_spacing=1.0):
|
|
231
|
+
"""Differentiable 2D parallel beam forward projection.
|
|
232
|
+
|
|
233
|
+
Parameters
|
|
234
|
+
----------
|
|
235
|
+
image : mx.array
|
|
236
|
+
2D input image, shape ``(H, W)``.
|
|
237
|
+
ray_dir : mx.array
|
|
238
|
+
Ray direction unit vectors, shape ``(n_views, 2)``.
|
|
239
|
+
det_origin : mx.array
|
|
240
|
+
Detector origin positions, shape ``(n_views, 2)``.
|
|
241
|
+
det_u_vec : mx.array
|
|
242
|
+
Detector u-direction unit vectors, shape ``(n_views, 2)``.
|
|
243
|
+
num_detectors : int
|
|
244
|
+
Number of detector elements.
|
|
245
|
+
detector_spacing : float
|
|
246
|
+
Physical spacing between detector elements.
|
|
247
|
+
voxel_spacing : float
|
|
248
|
+
Physical voxel size.
|
|
249
|
+
|
|
250
|
+
Returns
|
|
251
|
+
-------
|
|
252
|
+
sinogram : mx.array
|
|
253
|
+
Shape ``(n_views, num_detectors)``.
|
|
254
|
+
"""
|
|
255
|
+
return _parallel_forward_impl(image, ray_dir, det_origin, det_u_vec,
|
|
256
|
+
num_detectors, detector_spacing, voxel_spacing)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@parallel_forward.vjp
|
|
260
|
+
def parallel_forward_vjp(primals, cotangent, _):
|
|
261
|
+
image, ray_dir, det_origin, det_u_vec = primals[:4]
|
|
262
|
+
num_detectors = primals[4] if len(primals) > 4 else 128
|
|
263
|
+
detector_spacing = primals[5] if len(primals) > 5 else 1.0
|
|
264
|
+
voxel_spacing = primals[6] if len(primals) > 6 else 1.0
|
|
265
|
+
|
|
266
|
+
Ny, Nx = image.shape
|
|
267
|
+
grad_image = _parallel_backward_impl(
|
|
268
|
+
cotangent, ray_dir, det_origin, det_u_vec,
|
|
269
|
+
detector_spacing, Ny, Nx, voxel_spacing
|
|
270
|
+
)
|
|
271
|
+
return (grad_image,
|
|
272
|
+
mx.zeros_like(ray_dir), mx.zeros_like(det_origin), mx.zeros_like(det_u_vec),
|
|
273
|
+
None, None, None)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
@mx.custom_function
|
|
277
|
+
def parallel_backward(sinogram, ray_dir, det_origin, det_u_vec,
|
|
278
|
+
detector_spacing=1.0, H=128, W=128, voxel_spacing=1.0):
|
|
279
|
+
"""Differentiable 2D parallel beam backprojection.
|
|
280
|
+
|
|
281
|
+
Parameters
|
|
282
|
+
----------
|
|
283
|
+
sinogram : mx.array
|
|
284
|
+
2D sinogram, shape ``(n_views, num_detectors)``.
|
|
285
|
+
ray_dir : mx.array
|
|
286
|
+
Ray direction unit vectors, shape ``(n_views, 2)``.
|
|
287
|
+
det_origin : mx.array
|
|
288
|
+
Detector origin positions, shape ``(n_views, 2)``.
|
|
289
|
+
det_u_vec : mx.array
|
|
290
|
+
Detector u-direction unit vectors, shape ``(n_views, 2)``.
|
|
291
|
+
detector_spacing : float
|
|
292
|
+
Physical spacing between detector elements.
|
|
293
|
+
H, W : int
|
|
294
|
+
Output image dimensions.
|
|
295
|
+
voxel_spacing : float
|
|
296
|
+
Physical voxel size.
|
|
297
|
+
|
|
298
|
+
Returns
|
|
299
|
+
-------
|
|
300
|
+
reco : mx.array
|
|
301
|
+
Shape ``(H, W)``.
|
|
302
|
+
"""
|
|
303
|
+
return _parallel_backward_impl(sinogram, ray_dir, det_origin, det_u_vec,
|
|
304
|
+
detector_spacing, H, W, voxel_spacing)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
@parallel_backward.vjp
|
|
308
|
+
def parallel_backward_vjp(primals, cotangent, _):
|
|
309
|
+
sinogram, ray_dir, det_origin, det_u_vec = primals[:4]
|
|
310
|
+
detector_spacing = primals[4] if len(primals) > 4 else 1.0
|
|
311
|
+
n_ang, n_det = sinogram.shape
|
|
312
|
+
|
|
313
|
+
grad_sinogram = _parallel_forward_impl(
|
|
314
|
+
cotangent, ray_dir, det_origin, det_u_vec,
|
|
315
|
+
n_det, detector_spacing, 1.0
|
|
316
|
+
)
|
|
317
|
+
return (grad_sinogram,
|
|
318
|
+
mx.zeros_like(ray_dir), mx.zeros_like(det_origin), mx.zeros_like(det_u_vec),
|
|
319
|
+
None, None, None, None)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@mx.custom_function
|
|
323
|
+
def parallel_forward_footprint(image, ray_dir, det_origin, det_u_vec,
|
|
324
|
+
num_detectors=128, detector_spacing=1.0, voxel_spacing=1.0):
|
|
325
|
+
"""Matched footprint-style 2D parallel-beam forward projection."""
|
|
326
|
+
return _parallel_forward_footprint_impl(
|
|
327
|
+
image,
|
|
328
|
+
ray_dir,
|
|
329
|
+
det_origin,
|
|
330
|
+
det_u_vec,
|
|
331
|
+
num_detectors,
|
|
332
|
+
detector_spacing,
|
|
333
|
+
voxel_spacing,
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
@parallel_forward_footprint.vjp
|
|
338
|
+
def parallel_forward_footprint_vjp(primals, cotangent, _):
|
|
339
|
+
image, ray_dir, det_origin, det_u_vec = primals[:4]
|
|
340
|
+
detector_spacing = primals[5] if len(primals) > 5 else 1.0
|
|
341
|
+
voxel_spacing = primals[6] if len(primals) > 6 else 1.0
|
|
342
|
+
|
|
343
|
+
Ny, Nx = image.shape
|
|
344
|
+
grad_image = _parallel_backward_footprint_impl(
|
|
345
|
+
cotangent,
|
|
346
|
+
ray_dir,
|
|
347
|
+
det_origin,
|
|
348
|
+
det_u_vec,
|
|
349
|
+
detector_spacing,
|
|
350
|
+
Ny,
|
|
351
|
+
Nx,
|
|
352
|
+
voxel_spacing,
|
|
353
|
+
)
|
|
354
|
+
return (grad_image,
|
|
355
|
+
mx.zeros_like(ray_dir), mx.zeros_like(det_origin), mx.zeros_like(det_u_vec),
|
|
356
|
+
None, None, None)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@mx.custom_function
|
|
360
|
+
def parallel_backward_footprint(sinogram, ray_dir, det_origin, det_u_vec,
|
|
361
|
+
detector_spacing=1.0, H=128, W=128, voxel_spacing=1.0):
|
|
362
|
+
"""Adjoint of the matched footprint-style 2D parallel-beam forward projector."""
|
|
363
|
+
return _parallel_backward_footprint_impl(
|
|
364
|
+
sinogram,
|
|
365
|
+
ray_dir,
|
|
366
|
+
det_origin,
|
|
367
|
+
det_u_vec,
|
|
368
|
+
detector_spacing,
|
|
369
|
+
H,
|
|
370
|
+
W,
|
|
371
|
+
voxel_spacing,
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
@parallel_backward_footprint.vjp
|
|
376
|
+
def parallel_backward_footprint_vjp(primals, cotangent, _):
|
|
377
|
+
sinogram, ray_dir, det_origin, det_u_vec = primals[:4]
|
|
378
|
+
detector_spacing = primals[4] if len(primals) > 4 else 1.0
|
|
379
|
+
n_ang, n_det = sinogram.shape
|
|
380
|
+
voxel_spacing = primals[7] if len(primals) > 7 else 1.0
|
|
381
|
+
|
|
382
|
+
grad_sinogram = _parallel_forward_footprint_impl(
|
|
383
|
+
cotangent,
|
|
384
|
+
ray_dir,
|
|
385
|
+
det_origin,
|
|
386
|
+
det_u_vec,
|
|
387
|
+
n_det,
|
|
388
|
+
detector_spacing,
|
|
389
|
+
voxel_spacing,
|
|
390
|
+
)
|
|
391
|
+
return (grad_sinogram,
|
|
392
|
+
mx.zeros_like(ray_dir), mx.zeros_like(det_origin), mx.zeros_like(det_u_vec),
|
|
393
|
+
None, None, None, None)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
# ============================================================================
|
|
397
|
+
# 2D Fan Beam
|
|
398
|
+
# ============================================================================
|
|
399
|
+
|
|
400
|
+
def _fan_forward_impl(image, src_pos, det_center, det_u_vec,
|
|
401
|
+
num_detectors, detector_spacing=1.0, voxel_spacing=1.0):
|
|
402
|
+
"""Raw fan beam forward projection."""
|
|
403
|
+
image = _as_mx_float_array(image)
|
|
404
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
405
|
+
det_center = _as_mx_float_array(det_center)
|
|
406
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
407
|
+
|
|
408
|
+
Ny, Nx = image.shape
|
|
409
|
+
n_ang = src_pos.shape[0]
|
|
410
|
+
n_det = int(num_detectors)
|
|
411
|
+
|
|
412
|
+
cx = float(Nx * 0.5)
|
|
413
|
+
cy = float(Ny * 0.5)
|
|
414
|
+
|
|
415
|
+
params = mx.array([n_ang, n_det, Nx, Ny], dtype=mx.int32)
|
|
416
|
+
fparams = mx.array([detector_spacing, cx, cy, voxel_spacing], dtype=mx.float32)
|
|
417
|
+
|
|
418
|
+
grid, tg = _grid_2d(n_ang, n_det)
|
|
419
|
+
|
|
420
|
+
outputs = fan_2d_forward_kernel(
|
|
421
|
+
inputs=[image, src_pos, det_center, det_u_vec, params, fparams],
|
|
422
|
+
output_shapes=[(n_ang, n_det)],
|
|
423
|
+
output_dtypes=[_MX_DTYPE],
|
|
424
|
+
grid=grid,
|
|
425
|
+
threadgroup=tg,
|
|
426
|
+
)
|
|
427
|
+
return outputs[0]
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def _fan_backward_impl(sinogram, src_pos, det_center, det_u_vec,
|
|
431
|
+
detector_spacing, H, W, voxel_spacing=1.0):
|
|
432
|
+
"""Raw fan beam backprojection."""
|
|
433
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
434
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
435
|
+
det_center = _as_mx_float_array(det_center)
|
|
436
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
437
|
+
|
|
438
|
+
n_ang, n_det = sinogram.shape
|
|
439
|
+
Ny, Nx = int(H), int(W)
|
|
440
|
+
|
|
441
|
+
cx = float(Nx * 0.5)
|
|
442
|
+
cy = float(Ny * 0.5)
|
|
443
|
+
|
|
444
|
+
params = mx.array([n_ang, n_det, Nx, Ny], dtype=mx.int32)
|
|
445
|
+
fparams = mx.array([detector_spacing, cx, cy, voxel_spacing], dtype=mx.float32)
|
|
446
|
+
|
|
447
|
+
grid, tg = _grid_2d(n_ang, n_det)
|
|
448
|
+
|
|
449
|
+
outputs = fan_2d_backward_kernel(
|
|
450
|
+
inputs=[sinogram, src_pos, det_center, det_u_vec, params, fparams],
|
|
451
|
+
output_shapes=[(Ny, Nx)],
|
|
452
|
+
output_dtypes=[_MX_DTYPE],
|
|
453
|
+
grid=grid,
|
|
454
|
+
threadgroup=tg,
|
|
455
|
+
init_value=0,
|
|
456
|
+
)
|
|
457
|
+
return outputs[0]
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def _fan_forward_footprint_impl(image, src_pos, det_center, det_u_vec,
|
|
461
|
+
num_detectors, detector_spacing=1.0, voxel_spacing=1.0):
|
|
462
|
+
"""Approximate separable-footprint fan-beam forward projector."""
|
|
463
|
+
image = _as_mx_float_array(image)
|
|
464
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
465
|
+
det_center = _as_mx_float_array(det_center)
|
|
466
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
467
|
+
|
|
468
|
+
Ny, Nx = image.shape
|
|
469
|
+
n_ang = src_pos.shape[0]
|
|
470
|
+
n_det = int(num_detectors)
|
|
471
|
+
|
|
472
|
+
cx = float(Nx * 0.5)
|
|
473
|
+
cy = float(Ny * 0.5)
|
|
474
|
+
params, fparams = _projector_params_2d(
|
|
475
|
+
n_ang,
|
|
476
|
+
n_det,
|
|
477
|
+
Nx,
|
|
478
|
+
Ny,
|
|
479
|
+
float(detector_spacing),
|
|
480
|
+
cx,
|
|
481
|
+
cy,
|
|
482
|
+
float(voxel_spacing),
|
|
483
|
+
)
|
|
484
|
+
grid, tg = _grid_2d(n_ang, Ny)
|
|
485
|
+
|
|
486
|
+
outputs = fan_2d_footprint_forward_kernel(
|
|
487
|
+
inputs=[image, src_pos, det_center, det_u_vec, params, fparams],
|
|
488
|
+
output_shapes=[(n_ang, n_det)],
|
|
489
|
+
output_dtypes=[_MX_DTYPE],
|
|
490
|
+
grid=grid,
|
|
491
|
+
threadgroup=tg,
|
|
492
|
+
init_value=0,
|
|
493
|
+
)
|
|
494
|
+
return outputs[0]
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
def _fan_backward_footprint_impl(sinogram, src_pos, det_center, det_u_vec,
|
|
498
|
+
detector_spacing, H, W, voxel_spacing=1.0):
|
|
499
|
+
"""Adjoint of the approximate separable-footprint fan-beam projector."""
|
|
500
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
501
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
502
|
+
det_center = _as_mx_float_array(det_center)
|
|
503
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
504
|
+
|
|
505
|
+
n_ang, n_det = sinogram.shape
|
|
506
|
+
Ny, Nx = int(H), int(W)
|
|
507
|
+
|
|
508
|
+
cx = float(Nx * 0.5)
|
|
509
|
+
cy = float(Ny * 0.5)
|
|
510
|
+
params, fparams = _projector_params_2d(
|
|
511
|
+
n_ang,
|
|
512
|
+
n_det,
|
|
513
|
+
Nx,
|
|
514
|
+
Ny,
|
|
515
|
+
float(detector_spacing),
|
|
516
|
+
cx,
|
|
517
|
+
cy,
|
|
518
|
+
float(voxel_spacing),
|
|
519
|
+
)
|
|
520
|
+
grid, tg = _grid_2d(Nx, Ny)
|
|
521
|
+
|
|
522
|
+
outputs = fan_2d_footprint_backward_kernel(
|
|
523
|
+
inputs=[sinogram, src_pos, det_center, det_u_vec, params, fparams],
|
|
524
|
+
output_shapes=[(Ny, Nx)],
|
|
525
|
+
output_dtypes=[_MX_DTYPE],
|
|
526
|
+
grid=grid,
|
|
527
|
+
threadgroup=tg,
|
|
528
|
+
)
|
|
529
|
+
return outputs[0]
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
@mx.custom_function
|
|
533
|
+
def fan_forward(image, src_pos, det_center, det_u_vec,
|
|
534
|
+
num_detectors=128, detector_spacing=1.0, voxel_spacing=1.0):
|
|
535
|
+
"""Differentiable 2D fan beam forward projection.
|
|
536
|
+
|
|
537
|
+
Parameters
|
|
538
|
+
----------
|
|
539
|
+
image : mx.array
|
|
540
|
+
2D input image, shape ``(H, W)``.
|
|
541
|
+
src_pos : mx.array
|
|
542
|
+
Source positions, shape ``(n_views, 2)``.
|
|
543
|
+
det_center : mx.array
|
|
544
|
+
Detector center positions, shape ``(n_views, 2)``.
|
|
545
|
+
det_u_vec : mx.array
|
|
546
|
+
Detector u-direction unit vectors, shape ``(n_views, 2)``.
|
|
547
|
+
num_detectors : int
|
|
548
|
+
Number of detector elements.
|
|
549
|
+
detector_spacing : float
|
|
550
|
+
Physical spacing between detector elements.
|
|
551
|
+
voxel_spacing : float
|
|
552
|
+
Physical voxel size.
|
|
553
|
+
|
|
554
|
+
Returns
|
|
555
|
+
-------
|
|
556
|
+
sinogram : mx.array
|
|
557
|
+
Shape ``(n_views, num_detectors)``.
|
|
558
|
+
"""
|
|
559
|
+
return _fan_forward_impl(image, src_pos, det_center, det_u_vec,
|
|
560
|
+
num_detectors, detector_spacing, voxel_spacing)
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
@fan_forward.vjp
|
|
564
|
+
def fan_forward_vjp(primals, cotangent, _):
|
|
565
|
+
image, src_pos, det_center, det_u_vec = primals[:4]
|
|
566
|
+
detector_spacing = primals[5] if len(primals) > 5 else 1.0
|
|
567
|
+
voxel_spacing = primals[6] if len(primals) > 6 else 1.0
|
|
568
|
+
|
|
569
|
+
Ny, Nx = image.shape
|
|
570
|
+
grad_image = _fan_backward_impl(
|
|
571
|
+
cotangent, src_pos, det_center, det_u_vec,
|
|
572
|
+
detector_spacing, Ny, Nx, voxel_spacing
|
|
573
|
+
)
|
|
574
|
+
return (grad_image,
|
|
575
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center), mx.zeros_like(det_u_vec),
|
|
576
|
+
None, None, None)
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
@mx.custom_function
|
|
580
|
+
def fan_backward(sinogram, src_pos, det_center, det_u_vec,
|
|
581
|
+
detector_spacing=1.0, H=128, W=128, voxel_spacing=1.0):
|
|
582
|
+
"""Differentiable 2D fan beam backprojection.
|
|
583
|
+
|
|
584
|
+
Parameters
|
|
585
|
+
----------
|
|
586
|
+
sinogram : mx.array
|
|
587
|
+
2D fan beam sinogram, shape ``(n_views, num_detectors)``.
|
|
588
|
+
src_pos : mx.array
|
|
589
|
+
Source positions, shape ``(n_views, 2)``.
|
|
590
|
+
det_center : mx.array
|
|
591
|
+
Detector center positions, shape ``(n_views, 2)``.
|
|
592
|
+
det_u_vec : mx.array
|
|
593
|
+
Detector u-direction unit vectors, shape ``(n_views, 2)``.
|
|
594
|
+
detector_spacing : float
|
|
595
|
+
Physical spacing between detector elements.
|
|
596
|
+
H, W : int
|
|
597
|
+
Output image dimensions.
|
|
598
|
+
voxel_spacing : float
|
|
599
|
+
Physical voxel size.
|
|
600
|
+
|
|
601
|
+
Returns
|
|
602
|
+
-------
|
|
603
|
+
reco : mx.array
|
|
604
|
+
Shape ``(H, W)``.
|
|
605
|
+
"""
|
|
606
|
+
return _fan_backward_impl(sinogram, src_pos, det_center, det_u_vec,
|
|
607
|
+
detector_spacing, H, W, voxel_spacing)
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
@fan_backward.vjp
|
|
611
|
+
def fan_backward_vjp(primals, cotangent, _):
|
|
612
|
+
sinogram, src_pos, det_center, det_u_vec = primals[:4]
|
|
613
|
+
detector_spacing = primals[4] if len(primals) > 4 else 1.0
|
|
614
|
+
n_ang, n_det = sinogram.shape
|
|
615
|
+
|
|
616
|
+
grad_sinogram = _fan_forward_impl(
|
|
617
|
+
cotangent, src_pos, det_center, det_u_vec,
|
|
618
|
+
n_det, detector_spacing, 1.0
|
|
619
|
+
)
|
|
620
|
+
return (grad_sinogram,
|
|
621
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center), mx.zeros_like(det_u_vec),
|
|
622
|
+
None, None, None, None)
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
@mx.custom_function
|
|
626
|
+
def fan_forward_footprint(image, src_pos, det_center, det_u_vec,
|
|
627
|
+
num_detectors=128, detector_spacing=1.0, voxel_spacing=1.0):
|
|
628
|
+
"""Matched footprint-style 2D fan-beam forward projection."""
|
|
629
|
+
return _fan_forward_footprint_impl(
|
|
630
|
+
image,
|
|
631
|
+
src_pos,
|
|
632
|
+
det_center,
|
|
633
|
+
det_u_vec,
|
|
634
|
+
num_detectors,
|
|
635
|
+
detector_spacing,
|
|
636
|
+
voxel_spacing,
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
@fan_forward_footprint.vjp
|
|
641
|
+
def fan_forward_footprint_vjp(primals, cotangent, _):
|
|
642
|
+
image, src_pos, det_center, det_u_vec = primals[:4]
|
|
643
|
+
detector_spacing = primals[5] if len(primals) > 5 else 1.0
|
|
644
|
+
voxel_spacing = primals[6] if len(primals) > 6 else 1.0
|
|
645
|
+
|
|
646
|
+
Ny, Nx = image.shape
|
|
647
|
+
grad_image = _fan_backward_footprint_impl(
|
|
648
|
+
cotangent,
|
|
649
|
+
src_pos,
|
|
650
|
+
det_center,
|
|
651
|
+
det_u_vec,
|
|
652
|
+
detector_spacing,
|
|
653
|
+
Ny,
|
|
654
|
+
Nx,
|
|
655
|
+
voxel_spacing,
|
|
656
|
+
)
|
|
657
|
+
return (grad_image,
|
|
658
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center), mx.zeros_like(det_u_vec),
|
|
659
|
+
None, None, None)
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
@mx.custom_function
|
|
663
|
+
def fan_backward_footprint(sinogram, src_pos, det_center, det_u_vec,
|
|
664
|
+
detector_spacing=1.0, H=128, W=128, voxel_spacing=1.0):
|
|
665
|
+
"""Adjoint of the matched footprint-style 2D fan-beam forward projector."""
|
|
666
|
+
return _fan_backward_footprint_impl(
|
|
667
|
+
sinogram,
|
|
668
|
+
src_pos,
|
|
669
|
+
det_center,
|
|
670
|
+
det_u_vec,
|
|
671
|
+
detector_spacing,
|
|
672
|
+
H,
|
|
673
|
+
W,
|
|
674
|
+
voxel_spacing,
|
|
675
|
+
)
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
@fan_backward_footprint.vjp
|
|
679
|
+
def fan_backward_footprint_vjp(primals, cotangent, _):
|
|
680
|
+
sinogram, src_pos, det_center, det_u_vec = primals[:4]
|
|
681
|
+
detector_spacing = primals[4] if len(primals) > 4 else 1.0
|
|
682
|
+
n_ang, n_det = sinogram.shape
|
|
683
|
+
voxel_spacing = primals[7] if len(primals) > 7 else 1.0
|
|
684
|
+
|
|
685
|
+
grad_sinogram = _fan_forward_footprint_impl(
|
|
686
|
+
cotangent,
|
|
687
|
+
src_pos,
|
|
688
|
+
det_center,
|
|
689
|
+
det_u_vec,
|
|
690
|
+
n_det,
|
|
691
|
+
detector_spacing,
|
|
692
|
+
voxel_spacing,
|
|
693
|
+
)
|
|
694
|
+
return (grad_sinogram,
|
|
695
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center), mx.zeros_like(det_u_vec),
|
|
696
|
+
None, None, None, None)
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
# ============================================================================
|
|
700
|
+
# 3D Cone Beam
|
|
701
|
+
# ============================================================================
|
|
702
|
+
|
|
703
|
+
def _cone_forward_impl(volume, src_pos, det_center, det_u_vec, det_v_vec,
|
|
704
|
+
det_u, det_v, du, dv, voxel_spacing=1.0):
|
|
705
|
+
"""Raw cone beam forward projection."""
|
|
706
|
+
volume = _as_mx_float_array(volume)
|
|
707
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
708
|
+
det_center = _as_mx_float_array(det_center)
|
|
709
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
710
|
+
det_v_vec = _as_mx_float_array(det_v_vec)
|
|
711
|
+
|
|
712
|
+
D, H, W = volume.shape
|
|
713
|
+
n_views = src_pos.shape[0]
|
|
714
|
+
n_u, n_v = int(det_u), int(det_v)
|
|
715
|
+
|
|
716
|
+
# Permute DHW → WHD for kernel (matches CUDA convention)
|
|
717
|
+
volume_perm = mx.transpose(volume, axes=(2, 1, 0))
|
|
718
|
+
# Make contiguous
|
|
719
|
+
volume_perm = mx.array(volume_perm)
|
|
720
|
+
|
|
721
|
+
Nx, Ny, Nz = W, H, D
|
|
722
|
+
cx = float(Nx * 0.5)
|
|
723
|
+
cy = float(Ny * 0.5)
|
|
724
|
+
cz = float(Nz * 0.5)
|
|
725
|
+
|
|
726
|
+
params, fparams = _projector_params_3d(
|
|
727
|
+
n_views,
|
|
728
|
+
n_u,
|
|
729
|
+
n_v,
|
|
730
|
+
Nx,
|
|
731
|
+
Ny,
|
|
732
|
+
Nz,
|
|
733
|
+
float(du),
|
|
734
|
+
float(dv),
|
|
735
|
+
cx,
|
|
736
|
+
cy,
|
|
737
|
+
cz,
|
|
738
|
+
float(voxel_spacing),
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
grid, tg = _grid_3d(n_views, n_u, n_v)
|
|
742
|
+
|
|
743
|
+
outputs = cone_3d_forward_kernel(
|
|
744
|
+
inputs=[volume_perm, src_pos, det_center, det_u_vec, det_v_vec, params, fparams],
|
|
745
|
+
output_shapes=[(n_views, n_u, n_v)],
|
|
746
|
+
output_dtypes=[_MX_DTYPE],
|
|
747
|
+
grid=grid,
|
|
748
|
+
threadgroup=tg,
|
|
749
|
+
)
|
|
750
|
+
return outputs[0]
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def _cone_backward_impl(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
|
|
754
|
+
D, H, W, du, dv, voxel_spacing=1.0):
|
|
755
|
+
"""Raw cone beam backprojection."""
|
|
756
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
757
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
758
|
+
det_center = _as_mx_float_array(det_center)
|
|
759
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
760
|
+
det_v_vec = _as_mx_float_array(det_v_vec)
|
|
761
|
+
|
|
762
|
+
n_views, n_u, n_v = sinogram.shape
|
|
763
|
+
Nx, Ny, Nz = int(W), int(H), int(D)
|
|
764
|
+
|
|
765
|
+
cx = float(Nx * 0.5)
|
|
766
|
+
cy = float(Ny * 0.5)
|
|
767
|
+
cz = float(Nz * 0.5)
|
|
768
|
+
|
|
769
|
+
params, fparams = _projector_params_3d(
|
|
770
|
+
n_views,
|
|
771
|
+
n_u,
|
|
772
|
+
n_v,
|
|
773
|
+
Nx,
|
|
774
|
+
Ny,
|
|
775
|
+
Nz,
|
|
776
|
+
float(du),
|
|
777
|
+
float(dv),
|
|
778
|
+
cx,
|
|
779
|
+
cy,
|
|
780
|
+
cz,
|
|
781
|
+
float(voxel_spacing),
|
|
782
|
+
)
|
|
783
|
+
|
|
784
|
+
grid, tg = _grid_3d(n_views, n_u, n_v)
|
|
785
|
+
|
|
786
|
+
outputs = cone_3d_backward_kernel(
|
|
787
|
+
inputs=[sinogram, src_pos, det_center, det_u_vec, det_v_vec, params, fparams],
|
|
788
|
+
output_shapes=[(Nx, Ny, Nz)], # WHD layout
|
|
789
|
+
output_dtypes=[_MX_DTYPE],
|
|
790
|
+
grid=grid,
|
|
791
|
+
threadgroup=tg,
|
|
792
|
+
init_value=0,
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
# Permute WHD → DHW
|
|
796
|
+
vol = mx.transpose(outputs[0], axes=(2, 1, 0))
|
|
797
|
+
return mx.array(vol)
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
def _cone_forward_footprint_impl(volume, src_pos, det_center, det_u_vec, det_v_vec,
|
|
801
|
+
det_u, det_v, du, dv, voxel_spacing=1.0):
|
|
802
|
+
"""Approximate separable-footprint cone-beam forward projector."""
|
|
803
|
+
volume = _as_mx_float_array(volume)
|
|
804
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
805
|
+
det_center = _as_mx_float_array(det_center)
|
|
806
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
807
|
+
det_v_vec = _as_mx_float_array(det_v_vec)
|
|
808
|
+
|
|
809
|
+
D, H, W = volume.shape
|
|
810
|
+
n_views = src_pos.shape[0]
|
|
811
|
+
n_u, n_v = int(det_u), int(det_v)
|
|
812
|
+
|
|
813
|
+
volume_perm = mx.array(mx.transpose(volume, axes=(2, 1, 0)))
|
|
814
|
+
|
|
815
|
+
Nx, Ny, Nz = W, H, D
|
|
816
|
+
cx = float(Nx * 0.5)
|
|
817
|
+
cy = float(Ny * 0.5)
|
|
818
|
+
cz = float(Nz * 0.5)
|
|
819
|
+
|
|
820
|
+
params, fparams = _projector_params_3d(
|
|
821
|
+
n_views,
|
|
822
|
+
n_u,
|
|
823
|
+
n_v,
|
|
824
|
+
Nx,
|
|
825
|
+
Ny,
|
|
826
|
+
Nz,
|
|
827
|
+
float(du),
|
|
828
|
+
float(dv),
|
|
829
|
+
cx,
|
|
830
|
+
cy,
|
|
831
|
+
cz,
|
|
832
|
+
float(voxel_spacing),
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
grid, tg = _grid_3d(n_views, Ny, Nz)
|
|
836
|
+
|
|
837
|
+
outputs = cone_3d_footprint_forward_kernel(
|
|
838
|
+
inputs=[volume_perm, src_pos, det_center, det_u_vec, det_v_vec, params, fparams],
|
|
839
|
+
output_shapes=[(n_views, n_u, n_v)],
|
|
840
|
+
output_dtypes=[_MX_DTYPE],
|
|
841
|
+
grid=grid,
|
|
842
|
+
threadgroup=tg,
|
|
843
|
+
init_value=0,
|
|
844
|
+
)
|
|
845
|
+
return outputs[0]
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
def _cone_backward_footprint_impl(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
|
|
849
|
+
D, H, W, du, dv, voxel_spacing=1.0):
|
|
850
|
+
"""Adjoint of the approximate separable-footprint cone-beam forward projector."""
|
|
851
|
+
sinogram = _as_mx_float_array(sinogram)
|
|
852
|
+
src_pos = _as_mx_float_array(src_pos)
|
|
853
|
+
det_center = _as_mx_float_array(det_center)
|
|
854
|
+
det_u_vec = _as_mx_float_array(det_u_vec)
|
|
855
|
+
det_v_vec = _as_mx_float_array(det_v_vec)
|
|
856
|
+
|
|
857
|
+
n_views, n_u, n_v = sinogram.shape
|
|
858
|
+
Nx, Ny, Nz = int(W), int(H), int(D)
|
|
859
|
+
|
|
860
|
+
cx = float(Nx * 0.5)
|
|
861
|
+
cy = float(Ny * 0.5)
|
|
862
|
+
cz = float(Nz * 0.5)
|
|
863
|
+
|
|
864
|
+
params, fparams = _projector_params_3d(
|
|
865
|
+
n_views,
|
|
866
|
+
n_u,
|
|
867
|
+
n_v,
|
|
868
|
+
Nx,
|
|
869
|
+
Ny,
|
|
870
|
+
Nz,
|
|
871
|
+
float(du),
|
|
872
|
+
float(dv),
|
|
873
|
+
cx,
|
|
874
|
+
cy,
|
|
875
|
+
cz,
|
|
876
|
+
float(voxel_spacing),
|
|
877
|
+
)
|
|
878
|
+
|
|
879
|
+
grid, tg = _grid_3d(Nx, Ny, Nz)
|
|
880
|
+
|
|
881
|
+
outputs = cone_3d_footprint_backward_kernel(
|
|
882
|
+
inputs=[sinogram, src_pos, det_center, det_u_vec, det_v_vec, params, fparams],
|
|
883
|
+
output_shapes=[(Nx, Ny, Nz)],
|
|
884
|
+
output_dtypes=[_MX_DTYPE],
|
|
885
|
+
grid=grid,
|
|
886
|
+
threadgroup=tg,
|
|
887
|
+
)
|
|
888
|
+
|
|
889
|
+
vol = mx.transpose(outputs[0], axes=(2, 1, 0))
|
|
890
|
+
return mx.array(vol)
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
@mx.custom_function
|
|
894
|
+
def cone_forward(volume, src_pos, det_center, det_u_vec, det_v_vec,
|
|
895
|
+
det_u=128, det_v=128, du=1.0, dv=1.0, voxel_spacing=1.0):
|
|
896
|
+
"""Differentiable 3D cone beam forward projection.
|
|
897
|
+
|
|
898
|
+
Parameters
|
|
899
|
+
----------
|
|
900
|
+
volume : mx.array
|
|
901
|
+
3D input volume, shape ``(D, H, W)``.
|
|
902
|
+
src_pos : mx.array
|
|
903
|
+
Source positions, shape ``(n_views, 3)``.
|
|
904
|
+
det_center : mx.array
|
|
905
|
+
Detector center positions, shape ``(n_views, 3)``.
|
|
906
|
+
det_u_vec : mx.array
|
|
907
|
+
Detector u-direction unit vectors, shape ``(n_views, 3)``.
|
|
908
|
+
det_v_vec : mx.array
|
|
909
|
+
Detector v-direction unit vectors, shape ``(n_views, 3)``.
|
|
910
|
+
det_u, det_v : int
|
|
911
|
+
Number of detector elements along u and v axes.
|
|
912
|
+
du, dv : float
|
|
913
|
+
Physical spacing between detector elements along u and v.
|
|
914
|
+
voxel_spacing : float
|
|
915
|
+
Physical voxel size.
|
|
916
|
+
|
|
917
|
+
Returns
|
|
918
|
+
-------
|
|
919
|
+
sino : mx.array
|
|
920
|
+
Shape ``(n_views, det_u, det_v)``.
|
|
921
|
+
"""
|
|
922
|
+
return _cone_forward_impl(volume, src_pos, det_center, det_u_vec, det_v_vec,
|
|
923
|
+
det_u, det_v, du, dv, voxel_spacing)
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
@cone_forward.vjp
|
|
927
|
+
def cone_forward_vjp(primals, cotangent, _):
|
|
928
|
+
volume, src_pos, det_center, det_u_vec, det_v_vec = primals[:5]
|
|
929
|
+
du = primals[7] if len(primals) > 7 else 1.0
|
|
930
|
+
dv = primals[8] if len(primals) > 8 else 1.0
|
|
931
|
+
voxel_spacing = primals[9] if len(primals) > 9 else 1.0
|
|
932
|
+
|
|
933
|
+
D, H, W = volume.shape
|
|
934
|
+
grad_volume = _cone_backward_impl(
|
|
935
|
+
cotangent, src_pos, det_center, det_u_vec, det_v_vec,
|
|
936
|
+
D, H, W, du, dv, voxel_spacing
|
|
937
|
+
)
|
|
938
|
+
return (grad_volume,
|
|
939
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center),
|
|
940
|
+
mx.zeros_like(det_u_vec), mx.zeros_like(det_v_vec),
|
|
941
|
+
None, None, None, None, None)
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
@mx.custom_function
|
|
945
|
+
def cone_backward(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
|
|
946
|
+
D=128, H=128, W=128, du=1.0, dv=1.0, voxel_spacing=1.0):
|
|
947
|
+
"""Differentiable 3D cone beam backprojection.
|
|
948
|
+
|
|
949
|
+
Parameters
|
|
950
|
+
----------
|
|
951
|
+
sinogram : mx.array
|
|
952
|
+
3D projection data, shape ``(n_views, det_u, det_v)``.
|
|
953
|
+
src_pos : mx.array
|
|
954
|
+
Source positions, shape ``(n_views, 3)``.
|
|
955
|
+
det_center : mx.array
|
|
956
|
+
Detector center positions, shape ``(n_views, 3)``.
|
|
957
|
+
det_u_vec : mx.array
|
|
958
|
+
Detector u-direction unit vectors, shape ``(n_views, 3)``.
|
|
959
|
+
det_v_vec : mx.array
|
|
960
|
+
Detector v-direction unit vectors, shape ``(n_views, 3)``.
|
|
961
|
+
D, H, W : int
|
|
962
|
+
Output volume dimensions (depth, height, width).
|
|
963
|
+
du, dv : float
|
|
964
|
+
Physical spacing between detector elements.
|
|
965
|
+
voxel_spacing : float
|
|
966
|
+
Physical voxel size.
|
|
967
|
+
|
|
968
|
+
Returns
|
|
969
|
+
-------
|
|
970
|
+
vol : mx.array
|
|
971
|
+
Shape ``(D, H, W)``.
|
|
972
|
+
"""
|
|
973
|
+
return _cone_backward_impl(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
|
|
974
|
+
D, H, W, du, dv, voxel_spacing)
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
@cone_backward.vjp
|
|
978
|
+
def cone_backward_vjp(primals, cotangent, _):
|
|
979
|
+
sinogram, src_pos, det_center, det_u_vec, det_v_vec = primals[:5]
|
|
980
|
+
du = primals[8] if len(primals) > 8 else 1.0
|
|
981
|
+
dv = primals[9] if len(primals) > 9 else 1.0
|
|
982
|
+
voxel_spacing = primals[10] if len(primals) > 10 else 1.0
|
|
983
|
+
|
|
984
|
+
n_views, n_u, n_v = sinogram.shape
|
|
985
|
+
|
|
986
|
+
grad_sinogram = _cone_forward_impl(
|
|
987
|
+
cotangent, src_pos, det_center, det_u_vec, det_v_vec,
|
|
988
|
+
n_u, n_v, du, dv, voxel_spacing
|
|
989
|
+
)
|
|
990
|
+
return (grad_sinogram,
|
|
991
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center),
|
|
992
|
+
mx.zeros_like(det_u_vec), mx.zeros_like(det_v_vec),
|
|
993
|
+
None, None, None, None, None, None)
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
@mx.custom_function
|
|
997
|
+
def cone_forward_footprint(volume, src_pos, det_center, det_u_vec, det_v_vec,
|
|
998
|
+
det_u=128, det_v=128, du=1.0, dv=1.0, voxel_spacing=1.0):
|
|
999
|
+
"""Matched voxel-footprint cone-beam forward projection."""
|
|
1000
|
+
return _cone_forward_footprint_impl(
|
|
1001
|
+
volume,
|
|
1002
|
+
src_pos,
|
|
1003
|
+
det_center,
|
|
1004
|
+
det_u_vec,
|
|
1005
|
+
det_v_vec,
|
|
1006
|
+
det_u,
|
|
1007
|
+
det_v,
|
|
1008
|
+
du,
|
|
1009
|
+
dv,
|
|
1010
|
+
voxel_spacing,
|
|
1011
|
+
)
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
@cone_forward_footprint.vjp
|
|
1015
|
+
def cone_forward_footprint_vjp(primals, cotangent, _):
|
|
1016
|
+
volume, src_pos, det_center, det_u_vec, det_v_vec = primals[:5]
|
|
1017
|
+
du = primals[7] if len(primals) > 7 else 1.0
|
|
1018
|
+
dv = primals[8] if len(primals) > 8 else 1.0
|
|
1019
|
+
voxel_spacing = primals[9] if len(primals) > 9 else 1.0
|
|
1020
|
+
|
|
1021
|
+
D, H, W = volume.shape
|
|
1022
|
+
grad_volume = _cone_backward_footprint_impl(
|
|
1023
|
+
cotangent,
|
|
1024
|
+
src_pos,
|
|
1025
|
+
det_center,
|
|
1026
|
+
det_u_vec,
|
|
1027
|
+
det_v_vec,
|
|
1028
|
+
D,
|
|
1029
|
+
H,
|
|
1030
|
+
W,
|
|
1031
|
+
du,
|
|
1032
|
+
dv,
|
|
1033
|
+
voxel_spacing,
|
|
1034
|
+
)
|
|
1035
|
+
return (grad_volume,
|
|
1036
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center),
|
|
1037
|
+
mx.zeros_like(det_u_vec), mx.zeros_like(det_v_vec),
|
|
1038
|
+
None, None, None, None, None)
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
@mx.custom_function
|
|
1042
|
+
def cone_backward_footprint(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
|
|
1043
|
+
D=128, H=128, W=128, du=1.0, dv=1.0, voxel_spacing=1.0):
|
|
1044
|
+
"""Adjoint of the matched voxel-footprint cone-beam forward projector."""
|
|
1045
|
+
return _cone_backward_footprint_impl(
|
|
1046
|
+
sinogram,
|
|
1047
|
+
src_pos,
|
|
1048
|
+
det_center,
|
|
1049
|
+
det_u_vec,
|
|
1050
|
+
det_v_vec,
|
|
1051
|
+
D,
|
|
1052
|
+
H,
|
|
1053
|
+
W,
|
|
1054
|
+
du,
|
|
1055
|
+
dv,
|
|
1056
|
+
voxel_spacing,
|
|
1057
|
+
)
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
@cone_backward_footprint.vjp
|
|
1061
|
+
def cone_backward_footprint_vjp(primals, cotangent, _):
|
|
1062
|
+
sinogram, src_pos, det_center, det_u_vec, det_v_vec = primals[:5]
|
|
1063
|
+
det_u = int(sinogram.shape[1])
|
|
1064
|
+
det_v = int(sinogram.shape[2])
|
|
1065
|
+
du = primals[8] if len(primals) > 8 else 1.0
|
|
1066
|
+
dv = primals[9] if len(primals) > 9 else 1.0
|
|
1067
|
+
voxel_spacing = primals[10] if len(primals) > 10 else 1.0
|
|
1068
|
+
|
|
1069
|
+
grad_sinogram = _cone_forward_footprint_impl(
|
|
1070
|
+
cotangent,
|
|
1071
|
+
src_pos,
|
|
1072
|
+
det_center,
|
|
1073
|
+
det_u_vec,
|
|
1074
|
+
det_v_vec,
|
|
1075
|
+
det_u,
|
|
1076
|
+
det_v,
|
|
1077
|
+
du,
|
|
1078
|
+
dv,
|
|
1079
|
+
voxel_spacing,
|
|
1080
|
+
)
|
|
1081
|
+
return (grad_sinogram,
|
|
1082
|
+
mx.zeros_like(src_pos), mx.zeros_like(det_center),
|
|
1083
|
+
mx.zeros_like(det_u_vec), mx.zeros_like(det_v_vec),
|
|
1084
|
+
None, None, None, None, None, None)
|