def test_TotalVariation_vs_FGP_TV_gpu(self): # Isotropic TV cil TV_cil_iso = self.alpha * TotalVariation(max_iteration=self.iterations) res_TV_cil_iso = TV_cil_iso.proximal(self.data, tau=1.0) # Anisotropic TV cil TV_cil_aniso = self.alpha * TotalVariation(max_iteration=self.iterations, isotropic=False) res_TV_cil_aniso = TV_cil_aniso.proximal(self.data, tau=1.0) # Isotropic FGP_TV CCPiReg toolkit (gpu) TV_regtoolkit_gpu_iso = self.alpha * FGP_TV(max_iteration=self.iterations, device = 'gpu') res_TV_regtoolkit_gpu_iso = TV_regtoolkit_gpu_iso.proximal(self.data, tau=1.0) # Anisotropic FGP_TV CCPiReg toolkit (gpu) TV_regtoolkit_gpu_aniso = self.alpha * FGP_TV(max_iteration=self.iterations, device = 'gpu', isotropic=False) res_TV_regtoolkit_gpu_aniso = TV_regtoolkit_gpu_aniso.proximal(self.data, tau=1.0) # Anisotropic FGP_TV CCPiReg toolkit (cpu) TV_regtoolkit_cpu_aniso = self.alpha * FGP_TV(max_iteration=self.iterations, device = 'cpu', isotropic=False) res_TV_regtoolkit_cpu_aniso = TV_regtoolkit_cpu_aniso.proximal(self.data, tau=1.0) # Isotropic FGP_TV CCPiReg toolkit (cpu) TV_regtoolkit_cpu_iso = self.alpha * FGP_TV(max_iteration=self.iterations, device = 'cpu') res_TV_regtoolkit_cpu_iso = TV_regtoolkit_cpu_iso.proximal(self.data, tau=1.0) np.testing.assert_array_almost_equal(res_TV_cil_iso.array, res_TV_regtoolkit_gpu_iso.array, decimal=3) np.testing.assert_array_almost_equal(res_TV_regtoolkit_cpu_iso.array, res_TV_regtoolkit_gpu_iso.array, decimal=3) np.testing.assert_array_almost_equal(res_TV_cil_aniso.array, res_TV_regtoolkit_gpu_aniso.array, decimal=3) np.testing.assert_array_almost_equal(res_TV_regtoolkit_cpu_aniso.array, res_TV_regtoolkit_gpu_aniso.array, decimal=3)
def test_FGP_TV_complex(self): data = dataexample.CAMERA.get(size=(256, 256)) datarr = data.as_array() cmpx = np.zeros(data.shape, dtype=np.complex) cmpx.real = datarr[:] cmpx.imag = datarr[:] data.array = cmpx reg = FGP_TV() out = reg.proximal(data, 1) outarr = out.as_array() np.testing.assert_almost_equal(outarr.imag, outarr.real)
def test_functionality_FGP_TV(self): data = dataexample.CAMERA.get(size=(256,256)) datarr = data.as_array() from cil.plugins.ccpi_regularisation.functions import FGP_TV from ccpi.filters import regularisers tau = 1. fcil = FGP_TV() outcil = fcil.proximal(data, tau=tau) # use CIL defaults outrgl, info = regularisers.FGP_TV(datarr, fcil.alpha*tau, fcil.max_iteration, fcil.tolerance, 0, 1, 'cpu' ) np.testing.assert_almost_equal(outrgl, outcil.as_array())
def test_FGP_TV_raise_on_4D_data(self): from cil.plugins.ccpi_regularisation.functions import FGP_TV tau = 1. fcil = FGP_TV() data = ImageGeometry(3,4,5,channels=10).allocate(0) try: outcil = fcil.proximal(data, tau=tau) assert False except ValueError: assert True
t1 = timer() print(t1-t0) plt.figure() plt.imshow(res1.as_array()) plt.colorbar() plt.show() # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-9 r_iso = 0 r_nonneg = 1 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3-t2) plt.figure() plt.imshow(res2.as_array()) plt.colorbar() plt.show() plt.figure() plt.imshow(np.abs(res1.as_array()-res2.as_array())) plt.colorbar() plt.title("Difference CIL_FGP_TV vs CCPi_FGP_TV")
def test_compare_regularisation_toolkit_tomophantom(self): print("Compare CIL_FGP_TV vs CCPiReg_FGP_TV no tolerance (3D)") print("Building 3D phantom using TomoPhantom software") model = 13 # select a model number from the library N_size = 64 # 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) ig = ImageGeometry(N_size, N_size, N_size) data = ig.allocate() data.fill(phantom_tm) noisy_data = noise.gaussian(data, seed=10) alpha = 0.1 iters = 1000 print("Use tau as an array of ones") # CIL_TotalVariation no tolerance g_CIL = alpha * TotalVariation(iters, tolerance=None, info=True) res1 = g_CIL.proximal(noisy_data, ig.allocate(1.)) t0 = timer() res1 = g_CIL.proximal(noisy_data, ig.allocate(1.)) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-9 r_iso = 0 r_nonneg = 0 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t2) np.testing.assert_array_almost_equal(res1.as_array(), res2.as_array(), decimal=3) # CIL_FGP_TV no tolerance #g_CIL = FGP_TV(ig, alpha, iters, tolerance=None, info=True) g_CIL.tolerance = None t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) ################################################################### ################################################################### ################################################################### ################################################################### data = dataexample.PEPPERS.get(size=(256, 256)) ig = data.geometry ag = ig noisy_data = noise.gaussian(data, seed=10) alpha = 0.1 iters = 1000 # CIL_FGP_TV no tolerance g_CIL = alpha * TotalVariation(iters, tolerance=None) t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-8 r_iso = 0 r_nonneg = 0 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t2)
def test_compare_regularisation_toolkit(self): print("Compare CIL_FGP_TV vs CCPiReg_FGP_TV no tolerance (2D)") data = dataexample.SHAPES.get() ig = data.geometry ag = ig # Create noisy data. n1 = np.random.normal(0, 0.1, size=ig.shape) noisy_data = ig.allocate() noisy_data.fill(n1 + data.as_array()) alpha = 0.1 iters = 1000 # CIL_FGP_TV no tolerance g_CIL = alpha * TotalVariation( iters, tolerance=None, lower=0, info=True) t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-9 r_iso = 0 r_nonneg = 1 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t1) np.testing.assert_array_almost_equal(res1.as_array(), res2.as_array(), decimal=4) ################################################################### ################################################################### ################################################################### ################################################################### print("Compare CIL_FGP_TV vs CCPiReg_FGP_TV with iterations.") iters = 408 # CIL_FGP_TV no tolerance g_CIL = alpha * TotalVariation(iters, tolerance=1e-9, lower=0.) t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-9 r_iso = 0 r_nonneg = 1 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t2) print(mae(res1, res2)) np.testing.assert_array_almost_equal(res1.as_array(), res2.as_array(), decimal=3)
fista.run(300) LS_reco = fista.get_output() show2D(LS_reco, slice_list=[('vertical', 204 // bins), ('horizontal_y', 570 // bins)], title="LS reconstruction", fix_range=(-0.02, 0.07)) #%% plt.figure() plt.semilogy(fista.objective) plt.title('FISTA LS criterion') plt.show() #%% Setup total-variation (TV) with a non-negativity (NN) contraint alpha = 1 TV = alpha * FGP_TV(isotropic=True, device='gpu') #%% Setup FISTA to solve for LS with TV+NN fista = FISTA(x_init=ig.allocate(0), f=LS, g=TV, max_iteration=1000) fista.update_objective_interval = 10 #%% Run FISTA fista.run(300) TV_NN_reco_isotropic = fista.get_output() show2D(TV_NN_reco_isotropic, slice_list=[('vertical', 204 // bins), ('horizontal_y', 570 // bins)], title="TV_NN 100it reconstruction", fix_range=(-0.02, 0.07)) #%% plt.figure()
from cil.plugins.astra.operators import ProjectionOperator as A # from cil.plugins.ccpi_regularisation.functions import FGP_TV K = A(ig_cs, ag_shift) # the c parameter is used to remove scaling of L2NormSquared in PDHG # c = 2 f = LeastSquares(K, ldata, c=0.5 * c) if sparse_beads: f.L = 1071.1967 * c else: f.L = 24.4184 * c alpha_rgl = 0.003 alpha = alpha_rgl * ig_cs.voxel_size_x g = c * alpha * TotalVariation(lower=0.) g = FGP_TV(alpha, 100, 1e-5, 1, 1, 0, 'gpu') algo = FISTA(initial=K.domain.allocate(0), f=f, g=g, max_iteration=10000, update_objective_interval=2) #%% import cProfile algo.update_objective_interval = 10 cProfile.run('algo.run(100, verbose=1)') #%% plotter2D(algo.solution, cmap='gist_earth') #%%
def test_FGP_TV_rmul(self): from cil.plugins.ccpi_regularisation.functions import FGP_TV f = FGP_TV() self.rmul_test(f)