-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDemo_CuPy_3D.py
420 lines (352 loc) · 13.3 KB
/
Demo_CuPy_3D.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GPLv3 license (ASTRA toolbox)
Script that demonstrates the reconstruction of CuPy arrays while keeping
the data on the GPU (device-to-device)
Dependencies:
* astra-toolkit, install conda install -c astra-toolbox astra-toolbox
* TomoPhantom, https://github.com/dkazanc/TomoPhantom
* CuPy package
@author: Daniil Kazantsev
"""
import timeit
import os
import matplotlib.pyplot as plt
import numpy as np
import cupy as cp
import tomophantom
from tomophantom import TomoP3D
from tomophantom.qualitymetrics import QualityTools
from tomobar.methodsDIR_CuPy import RecToolsDIRCuPy
from tomobar.methodsIR_CuPy import RecToolsIRCuPy
print("Building 3D phantom using TomoPhantom software")
tic = timeit.default_timer()
model = 13 # select a model number from the library
N_size = 256 # Define phantom dimensions using a scalar value (cubic phantom)
path = os.path.dirname(tomophantom.__file__)
path_library3D = os.path.join(path, "phantomlib", "Phantom3DLibrary.dat")
phantom_tm = TomoP3D.Model(model, N_size, path_library3D)
# Projection geometry related parameters:
Horiz_det = int(np.sqrt(2) * N_size) # detector column count (horizontal)
Vert_det = N_size # detector row count (vertical) (no reason for it to be > N)
angles_num = int(0.3 * np.pi * N_size) # angles number
angles = np.linspace(0.0, 179.9, angles_num, dtype="float32") # in degrees
angles_rad = angles * (np.pi / 180.0)
print("Generate 3D analytical projection data with TomoPhantom")
projData3D_analyt = TomoP3D.ModelSino(
model, N_size, Horiz_det, Vert_det, angles, path_library3D
)
input_data_labels = ["detY", "angles", "detX"]
# transfering numpy array to CuPy array
projData3D_analyt_cupy = cp.asarray(projData3D_analyt, order="C")
# %%
# It is recommend to re-run twice in order to get the optimal time
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%%Reconstructing with 3D FBP-CuPy method %%%%%%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP = RecToolsDIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
device_projector="gpu",
)
tic = timeit.default_timer()
FBPrec_cupy = RecToolsCP.FBP(
projData3D_analyt_cupy,
recon_mask_radius=0.95,
data_axes_labels_order=input_data_labels,
cutoff_freq=0.7,
)
toc = timeit.default_timer()
Run_time = toc - tic
print(
"FBP 3D reconstruction with FFT filtering using CuPy (GPU) in {} seconds".format(
Run_time
)
)
# bring data from the device to the host
FBPrec_cupy = cp.asnumpy(FBPrec_cupy)
sliceSel = int(0.5 * N_size)
max_val = 1
plt.figure()
plt.subplot(131)
plt.imshow(FBPrec_cupy[sliceSel, :, :], vmin=0, vmax=max_val)
plt.title("3D FBP Reconstruction, axial view")
plt.subplot(132)
plt.imshow(FBPrec_cupy[:, sliceSel, :], vmin=0, vmax=max_val)
plt.title("3D FBP Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(FBPrec_cupy[:, :, sliceSel], vmin=0, vmax=max_val)
plt.title("3D FBP Reconstruction, sagittal view")
plt.show()
#
sliceSel = int(0.5 * N_size)
max_val = 0.3
plt.figure()
plt.subplot(131)
plt.imshow(
abs(FBPrec_cupy[sliceSel, :, :] - phantom_tm[sliceSel, :, :]), vmin=0, vmax=max_val
)
plt.title("3D FBP residual, axial view")
plt.subplot(132)
plt.imshow(
abs(FBPrec_cupy[:, sliceSel, :] - phantom_tm[:, sliceSel, :]), vmin=0, vmax=max_val
)
plt.title("3D FBP residual, coronal view")
plt.subplot(133)
plt.imshow(
abs(FBPrec_cupy[:, :, sliceSel] - phantom_tm[:, :, sliceSel]), vmin=0, vmax=max_val
)
plt.title("3D FBP residual, sagittal view")
plt.show()
print(
"Min {} and Max {} of the volume".format(np.min(FBPrec_cupy), np.max(FBPrec_cupy))
)
# calculate errors
Qtools = QualityTools(phantom_tm, FBPrec_cupy)
RMSE = Qtools.rmse()
print("Root Mean Square Error is {} for FBP".format(RMSE))
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%%Reconstructing with 3D Fourier-CuPy method %%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP = RecToolsDIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
device_projector="gpu",
)
tic = timeit.default_timer()
Fourier_cupy = RecToolsCP.FOURIER_INV(
projData3D_analyt_cupy,
recon_mask_radius=0.95,
data_axes_labels_order=input_data_labels,
)
toc = timeit.default_timer()
Run_time = toc - tic
print("Fourier 3D reconstruction using CuPy (GPU) in {} seconds".format(Run_time))
# bring data from the device to the host
Fourier_cupy = cp.asnumpy(Fourier_cupy)
sliceSel = int(0.5 * N_size)
max_val = 1
plt.figure()
plt.subplot(131)
plt.imshow(Fourier_cupy[sliceSel, :, :], vmin=0, vmax=max_val)
plt.title("3D Fourier Reconstruction, axial view")
plt.subplot(132)
plt.imshow(Fourier_cupy[:, sliceSel, :], vmin=0, vmax=max_val)
plt.title("3D Fourier Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(Fourier_cupy[:, :, sliceSel], vmin=0, vmax=max_val)
plt.title("3D Fourier Reconstruction, sagittal view")
plt.show()
sliceSel = int(0.5 * N_size)
max_val = 0.3
plt.figure()
plt.subplot(131)
plt.imshow(
abs(Fourier_cupy[sliceSel, :, :] - phantom_tm[sliceSel, :, :]), vmin=0, vmax=max_val
)
plt.title("3D Fourier residual, axial view")
plt.subplot(132)
plt.imshow(
abs(Fourier_cupy[:, sliceSel, :] - phantom_tm[:, sliceSel, :]), vmin=0, vmax=max_val
)
plt.title("3D Fourier residual, coronal view")
plt.subplot(133)
plt.imshow(
abs(Fourier_cupy[:, :, sliceSel] - phantom_tm[:, :, sliceSel]), vmin=0, vmax=max_val
)
plt.title("3D Fourier residual, sagittal view")
plt.show()
print(
"Min {} and Max {} of the volume".format(np.min(FBPrec_cupy), np.max(FBPrec_cupy))
)
# calculate errors
Qtools = QualityTools(phantom_tm, Fourier_cupy)
RMSE = Qtools.rmse()
print("Root Mean Square Error is {} for Fourier inversion".format(RMSE))
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%Reconstructing using Landweber algorithm %%%%%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP_iter = RecToolsIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
datafidelity="LS", # Data fidelity, choose from LS, KL, PWLS, SWLS
device_projector="gpu",
)
# prepare dictionaries with parameters:
_data_ = {
"projection_norm_data": projData3D_analyt_cupy,
"data_axes_labels_order": input_data_labels,
} # data dictionary
LWrec_cupy = RecToolsCP_iter.Landweber(_data_)
lwrec = cp.asnumpy(LWrec_cupy)
sliceSel = int(0.5 * N_size)
plt.figure()
plt.subplot(131)
plt.imshow(lwrec[sliceSel, :, :])
plt.title("3D Landweber Reconstruction, axial view")
plt.subplot(132)
plt.imshow(lwrec[:, sliceSel, :])
plt.title("3D Landweber Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(lwrec[:, :, sliceSel])
plt.title("3D Landweber Reconstruction, sagittal view")
plt.show()
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%%% Reconstructing using SIRT algorithm %%%%%%%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP_iter = RecToolsIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
device_projector="gpu",
)
# prepare dictionaries with parameters:
_data_ = {
"projection_norm_data": projData3D_analyt_cupy,
"data_axes_labels_order": input_data_labels,
}
_algorithm_ = {"iterations": 300, "nonnegativity": True}
SIRTrec_cupy = RecToolsCP_iter.SIRT(_data_, _algorithm_)
sirt_rec = cp.asnumpy(SIRTrec_cupy)
sliceSel = int(0.5 * N_size)
plt.figure()
plt.subplot(131)
plt.imshow(sirt_rec[sliceSel, :, :])
plt.title("3D SIRT Reconstruction, axial view")
plt.subplot(132)
plt.imshow(sirt_rec[:, sliceSel, :])
plt.title("3D SIRT Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(sirt_rec[:, :, sliceSel])
plt.title("3D SIRT Reconstruction, sagittal view")
plt.show()
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%%% Reconstructing using CGLS algorithm %%%%%%%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP_iter = RecToolsIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
device_projector="gpu",
)
# prepare dictionaries with parameters:
_data_ = {
"projection_norm_data": projData3D_analyt_cupy,
"data_axes_labels_order": input_data_labels,
}
_algorithm_ = {"iterations": 20, "nonnegativity": True}
CGLSrec_cupy = RecToolsCP_iter.CGLS(_data_, _algorithm_)
cgls_rec = cp.asnumpy(CGLSrec_cupy)
sliceSel = int(0.5 * N_size)
plt.figure()
plt.subplot(131)
plt.imshow(cgls_rec[sliceSel, :, :])
plt.title("3D CGLS Reconstruction, axial view")
plt.subplot(132)
plt.imshow(cgls_rec[:, sliceSel, :])
plt.title("3D CGLS Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(cgls_rec[:, :, sliceSel])
plt.title("3D CGLS Reconstruction, sagittal view")
plt.show()
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%%%%%%% Reconstructing using FISTA algorithm %%%%%%%%%%%%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
RecToolsCP_iter = RecToolsIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
datafidelity="LS",
device_projector=0,
)
# prepare dictionaries with parameters:
_data_ = {
"projection_norm_data": projData3D_analyt_cupy,
"data_axes_labels_order": input_data_labels,
} # data dictionary
lc = RecToolsCP_iter.powermethod(_data_)
_algorithm_ = {"iterations": 300, "lipschitz_const": lc.get()}
start_time = timeit.default_timer()
RecFISTA = RecToolsCP_iter.FISTA(_data_, _algorithm_, _regularisation_={})
txtstr = "%s = %.3fs" % ("elapsed time", timeit.default_timer() - start_time)
print(txtstr)
fista_rec_np = cp.asnumpy(RecFISTA)
sliceSel = int(0.5 * N_size)
plt.figure()
plt.subplot(131)
plt.imshow(fista_rec_np[sliceSel, :, :])
plt.title("3D FISTA Reconstruction, axial view")
plt.subplot(132)
plt.imshow(fista_rec_np[:, sliceSel, :])
plt.title("3D FISTA Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(fista_rec_np[:, :, sliceSel])
plt.title("3D FISTA Reconstruction, sagittal view")
plt.show()
# %%
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("%%%%% Reconstructing using regularised FISTA-OS algorithm %%")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
# NOTE that you'd need to install CuPy modules for the regularisers from the regularisation toolkit
RecToolsCP_iter = RecToolsIRCuPy(
DetectorsDimH=Horiz_det, # Horizontal detector dimension
DetectorsDimV=Vert_det, # Vertical detector dimension (3D case)
CenterRotOffset=0.0, # Center of Rotation scalar or a vector
AnglesVec=angles_rad, # A vector of projection angles in radians
ObjSize=N_size, # Reconstructed object dimensions (scalar)
datafidelity="LS", # Data fidelity, choose from LS, KL, PWLS
device_projector=0,
)
start_time = timeit.default_timer()
# prepare dictionaries with parameters:
_data_ = {
"projection_norm_data": projData3D_analyt_cupy,
"OS_number": 8,
"data_axes_labels_order": input_data_labels,
} # data dictionary
lc = RecToolsCP_iter.powermethod(_data_)
_algorithm_ = {"iterations": 15, "lipschitz_const": lc.get()}
_regularisation_ = {
"method": "PD_TV",
"regul_param": 0.0005,
"iterations": 35,
"device_regulariser": 0,
}
RecFISTA = RecToolsCP_iter.FISTA(_data_, _algorithm_, _regularisation_)
txtstr = "%s = %.3fs" % ("elapsed time", timeit.default_timer() - start_time)
print(txtstr)
fista_rec_np = cp.asnumpy(RecFISTA)
sliceSel = int(0.5 * N_size)
plt.figure()
plt.subplot(131)
plt.imshow(fista_rec_np[sliceSel, :, :])
plt.title("3D FISTA-OS Reconstruction, axial view")
plt.subplot(132)
plt.imshow(fista_rec_np[:, sliceSel, :])
plt.title("3D FISTA-OS Reconstruction, coronal view")
plt.subplot(133)
plt.imshow(fista_rec_np[:, :, sliceSel])
plt.title("3D FISTA-OS Reconstruction, sagittal view")
plt.show()
# %%