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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This demo scripts support the following publication:
"CCPi-Regularisation Toolkit for computed tomographic image reconstruction with
proximal splitting algorithms" by Daniil Kazantsev, Edoardo Pasca, Martin J. Turner,
Philip J. Withers; Software X, 2019
____________________________________________________________________________
* Generates phantom using TomoPhantom software
* Denoise using closely to optimal parameters
____________________________________________________________________________
>>>>> Dependencies: <<<<<
1. TomoPhantom software for phantom and data generation
@author: Daniil Kazantsev, e:mail daniil.kazantsev@diamond.ac.uk
Apache 2.0.
"""
import timeit
import matplotlib.pyplot as plt
# import matplotlib.gridspec as gridspec
import numpy as np
import os
import tomophantom
from tomophantom import TomoP3D
from tomophantom.supp.artifacts import ArtifactsClass
from ccpi.supp.qualitymetrics import QualityTools
from scipy.signal import gaussian
from ccpi.filters.regularisers import ROF_TV, FGP_TV, SB_TV, LLT_ROF, TGV, NDF, Diff4th
#%%
print ("Building 3D phantom using TomoPhantom software")
tic=timeit.default_timer()
model = 16 # select a model number from the library
N_size = 128 # Define phantom dimensions using a scalar value (cubic phantom)
path = os.path.dirname(tomophantom.__file__)
path_library3D = os.path.join(path, "Phantom3DLibrary.dat")
#This will generate a N_size x N_size x N_size phantom (3D)
phantom_tm = TomoP3D.Model(model, N_size, path_library3D)
toc=timeit.default_timer()
Run_time = toc - tic
print("Phantom has been built in {} seconds".format(Run_time))
# adding normally distributed noise
artifacts_add = ArtifactsClass(phantom_tm)
phantom_noise = artifacts_add.noise(sigma=0.1,noisetype='Gaussian')
sliceSel = int(0.5*N_size)
#plt.gray()
plt.figure()
plt.subplot(131)
plt.imshow(phantom_noise[sliceSel,:,:],vmin=0, vmax=1.4)
plt.title('3D Phantom, axial view')
plt.subplot(132)
plt.imshow(phantom_noise[:,sliceSel,:],vmin=0, vmax=1.4)
plt.title('3D Phantom, coronal view')
plt.subplot(133)
plt.imshow(phantom_noise[:,:,sliceSel],vmin=0, vmax=1.4)
plt.title('3D Phantom, sagittal view')
plt.show()
#%%
print ("____________________Applying regularisers_______________________")
print ("________________________________________________________________")
print ("#############ROF TV CPU####################")
# set parameters
pars = {'algorithm': ROF_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06,\
'number_of_iterations': 1000,\
'time_marching_parameter': 0.00025,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(rof_cpu3D, infcpu) = ROF_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'],'cpu')
toc=timeit.default_timer()
Run_time_rof = toc - tic
Qtools = QualityTools(phantom_tm, rof_cpu3D)
RMSE_rof = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, rof_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim_rof = Qtools.ssim(win2d)
print("ROF-TV (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE_rof,ssim_rof[0],Run_time_rof))
#%%
print ("#############ROF TV GPU####################")
# set parameters
pars = {'algorithm': ROF_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06,\
'number_of_iterations': 8330,\
'time_marching_parameter': 0.00025,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(rof_gpu3D, infogpu) = ROF_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'],'gpu')
toc=timeit.default_timer()
Run_time_rof = toc - tic
Qtools = QualityTools(phantom_tm, rof_gpu3D)
RMSE_rof = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, rof_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim_rof = Qtools.ssim(win2d)
print("ROF-TV (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE_rof,ssim_rof[0],Run_time_rof))
#%%
print ("#############FGP TV CPU####################")
# set parameters
pars = {'algorithm' : FGP_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'number_of_iterations' : 930 ,\
'tolerance_constant':0.0,\
'methodTV': 0 ,\
'nonneg': 0}
tic=timeit.default_timer()
(fgp_cpu3D, infoFGP) = FGP_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['tolerance_constant'],
pars['methodTV'],
pars['nonneg'],'cpu')
toc=timeit.default_timer()
Run_time_fgp = toc - tic
Qtools = QualityTools(phantom_tm, fgp_cpu3D)
RMSE_rof = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, fgp_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim_fgp = Qtools.ssim(win2d)
print("FGP-TV (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE_rof,ssim_fgp[0],Run_time_fgp))
#%%
print ("#############FGP TV GPU####################")
# set parameters
pars = {'algorithm' : FGP_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'number_of_iterations' :930 ,\
'tolerance_constant':0.0,\
'methodTV': 0 ,\
'nonneg': 0}
tic=timeit.default_timer()
(fgp_gpu3D,infogpu) = FGP_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['tolerance_constant'],
pars['methodTV'],
pars['nonneg'],'gpu')
toc=timeit.default_timer()
Run_time_fgp = toc - tic
Qtools = QualityTools(phantom_tm, fgp_gpu3D)
RMSE_rof = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, fgp_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim_fgp = Qtools.ssim(win2d)
print("FGP-TV (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE_rof,ssim_fgp[0],Run_time_fgp))
#%%
print ("#############SB TV CPU####################")
# set parameters
pars = {'algorithm' : SB_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'number_of_iterations' :225 ,\
'tolerance_constant':0.0,\
'methodTV': 0}
tic=timeit.default_timer()
(sb_cpu3D, info_vec_cpu) = SB_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['tolerance_constant'],
pars['methodTV'], 'cpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, sb_cpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, sb_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("SB-TV (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############SB TV GPU####################")
# set parameters
pars = {'algorithm' : SB_TV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'number_of_iterations' :225 ,\
'tolerance_constant':0.0,\
'methodTV': 0}
tic=timeit.default_timer()
(sb_gpu3D,info_vec_gpu) = SB_TV(pars['input'],
pars['regularisation_parameter'],
pars['number_of_iterations'],
pars['tolerance_constant'],
pars['methodTV'], 'gpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, sb_gpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, sb_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("SB-TV (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############NDF CPU####################")
# set parameters
pars = {'algorithm' : NDF, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'edge_parameter':0.017,\
'number_of_iterations' :530 ,\
'time_marching_parameter':0.01,\
'penalty_type':1,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(ndf_cpu3D, info_vec_cpu) = NDF(pars['input'],
pars['regularisation_parameter'],
pars['edge_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['penalty_type'],
pars['tolerance_constant'],'cpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, ndf_cpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, ndf_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("NDF (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############NDF GPU####################")
# set parameters
pars = {'algorithm' : NDF, \
'input' : phantom_noise,\
'regularisation_parameter':0.06, \
'edge_parameter':0.017,\
'number_of_iterations' :530 ,\
'time_marching_parameter':0.01,\
'penalty_type':1,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(ndf_gpu3D,info_vec_gpu) = NDF(pars['input'],
pars['regularisation_parameter'],
pars['edge_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['penalty_type'],
pars['tolerance_constant'],'gpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, ndf_gpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, ndf_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("NDF (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############Diff4th CPU####################")
# set parameters
pars = {'algorithm' : Diff4th, \
'input' : phantom_noise,\
'regularisation_parameter':4.5, \
'edge_parameter':0.035,\
'number_of_iterations' :2425 ,\
'time_marching_parameter':0.001,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(diff4th_cpu3D, info_vec_cpu) = Diff4th(pars['input'],
pars['regularisation_parameter'],
pars['edge_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'],'cpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, diff4th_cpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, diff4th_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("Diff4th (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############Diff4th GPU####################")
# set parameters
pars = {'algorithm' : Diff4th, \
'input' : phantom_noise,\
'regularisation_parameter':4.5, \
'edge_parameter':0.035,\
'number_of_iterations' :2425 ,\
'time_marching_parameter':0.001,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(diff4th_gpu3D,info_vec_gpu) = Diff4th(pars['input'],
pars['regularisation_parameter'],
pars['edge_parameter'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'],'gpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, diff4th_gpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, diff4th_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("Diff4th (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############TGV CPU####################")
# set parameters
pars = {'algorithm' : TGV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06,\
'alpha1':1.0,\
'alpha0':2.0,\
'number_of_iterations' :1000,\
'LipshitzConstant' :12,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(tgv_cpu3D, info_vec_cpu) = TGV(pars['input'],
pars['regularisation_parameter'],
pars['alpha1'],
pars['alpha0'],
pars['number_of_iterations'],
pars['LipshitzConstant'],
pars['tolerance_constant'],'cpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, tgv_cpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, tgv_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("TGV (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############TGV GPU####################")
# set parameters
pars = {'algorithm' : TGV, \
'input' : phantom_noise,\
'regularisation_parameter':0.06,\
'alpha1':1.0,\
'alpha0':2.0,\
'number_of_iterations' :7845,\
'LipshitzConstant' :12,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(tgv_gpu3D,info_vec_gpu) = TGV(pars['input'],
pars['regularisation_parameter'],
pars['alpha1'],
pars['alpha0'],
pars['number_of_iterations'],
pars['LipshitzConstant'],
pars['tolerance_constant'],'gpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, tgv_gpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, tgv_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("TGV (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############ROF-LLT CPU####################")
# set parameters
pars = {'algorithm' : LLT_ROF, \
'input' : phantom_noise,\
'regularisation_parameterROF':0.03, \
'regularisation_parameterLLT':0.015, \
'number_of_iterations' : 1000 ,\
'time_marching_parameter' :0.00025 ,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(rofllt_cpu3D, info_vec_cpu) = LLT_ROF(pars['input'],
pars['regularisation_parameterROF'],
pars['regularisation_parameterLLT'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'], 'cpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, rofllt_cpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, rofllt_cpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("ROF-LLT (cpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
#%%
print ("#############ROF-LLT GPU####################")
# set parameters
pars = {'algorithm' : LLT_ROF, \
'input' : phantom_noise,\
'regularisation_parameterROF':0.03, \
'regularisation_parameterLLT':0.015, \
'number_of_iterations' : 8000 ,\
'time_marching_parameter' :0.00025 ,\
'tolerance_constant':0.0}
tic=timeit.default_timer()
(rofllt_gpu3D,info_vec_gpu) = LLT_ROF(pars['input'],
pars['regularisation_parameterROF'],
pars['regularisation_parameterLLT'],
pars['number_of_iterations'],
pars['time_marching_parameter'],
pars['tolerance_constant'], 'gpu')
toc=timeit.default_timer()
Run_time = toc - tic
Qtools = QualityTools(phantom_tm, rofllt_gpu3D)
RMSE = Qtools.rmse()
# SSIM measure
Qtools = QualityTools(phantom_tm[sliceSel,:,:]*255, rofllt_gpu3D[sliceSel,:,:]*235)
win = np.array([gaussian(11, 1.5)])
win2d = win * (win.T)
ssim = Qtools.ssim(win2d)
print("ROF-LLT (gpu) ____ RMSE: {}, MMSIM: {}, run time: {} sec".format(RMSE,ssim[0],Run_time))
|