From 22f6e22cbe6db04c6bbe8d259ce761e3748d7102 Mon Sep 17 00:00:00 2001 From: algol Date: Thu, 12 Apr 2018 11:56:54 +0100 Subject: dTV some bugs in cython --- Wrappers/Python/ccpi/filters/regularisers.py | 4 +- Wrappers/Python/conda-recipe/run_test.py | 149 --------------------- Wrappers/Python/conda-recipe/run_test.py.in | 148 ++++++++++++++++++++ Wrappers/Python/conda-recipe/testLena.npy | Bin 0 -> 1048656 bytes Wrappers/Python/demos/demo_cpu_regularisers.py | 9 +- .../Python/demos/demo_cpu_vs_gpu_regularisers.py | 2 + Wrappers/Python/demos/demo_gpu_regularisers.py | 8 +- Wrappers/Python/src/cpu_regularisers.pyx | 2 +- Wrappers/Python/src/gpu_regularisers.pyx | 7 +- 9 files changed, 167 insertions(+), 162 deletions(-) delete mode 100644 Wrappers/Python/conda-recipe/run_test.py create mode 100644 Wrappers/Python/conda-recipe/run_test.py.in create mode 100644 Wrappers/Python/conda-recipe/testLena.npy (limited to 'Wrappers/Python') diff --git a/Wrappers/Python/ccpi/filters/regularisers.py b/Wrappers/Python/ccpi/filters/regularisers.py index c6723fa..376cc9c 100644 --- a/Wrappers/Python/ccpi/filters/regularisers.py +++ b/Wrappers/Python/ccpi/filters/regularisers.py @@ -2,8 +2,8 @@ script which assigns a proper device core function based on a flag ('cpu' or 'gpu') """ -from ccpi.filters.cpu_regularisers_cython import TV_ROF_CPU, TV_FGP_CPU dTV_FGP_CPU -from ccpi.filters.gpu_regularisers import TV_ROF_GPU, TV_FGP_GPU dTV_FGP_GPU +from ccpi.filters.cpu_regularisers_cython import TV_ROF_CPU, TV_FGP_CPU, dTV_FGP_CPU +from ccpi.filters.gpu_regularisers import TV_ROF_GPU, TV_FGP_GPU, dTV_FGP_GPU def ROF_TV(inputData, regularisation_parameter, iterations, time_marching_parameter,device='cpu'): diff --git a/Wrappers/Python/conda-recipe/run_test.py b/Wrappers/Python/conda-recipe/run_test.py deleted file mode 100644 index 04bbd40..0000000 --- a/Wrappers/Python/conda-recipe/run_test.py +++ /dev/null @@ -1,149 +0,0 @@ -import unittest -import numpy as np -import os -from ccpi.filters.regularisers import ROF_TV, FGP_TV -import matplotlib.pyplot as plt - -def rmse(im1, im2): - rmse = np.sqrt(np.sum((im1 - im2) ** 2) / float(im1.size)) - return rmse - -class TestRegularisers(unittest.TestCase): - - def setUp(self): - pass - - def test_cpu_regularisers(self): - filename = os.path.join(".." , ".." , ".." , "data" ,"lena_gray_512.tif") - - # read noiseless image - Im = plt.imread(filename) - Im = np.asarray(Im, dtype='float32') - - Im = Im/255 - tolerance = 1e-05 - rms_rof_exp = 0.006812507 #expected value for ROF model - rms_fgp_exp = 0.019152347 #expected value for FGP model - - # set parameters for ROF-TV - pars_rof_tv = {'algorithm': ROF_TV, \ - 'input' : Im,\ - 'regularisation_parameter':0.04,\ - 'number_of_iterations': 50,\ - 'time_marching_parameter': 0.0025 - } - # set parameters for FGP-TV - pars_fgp_tv = {'algorithm' : FGP_TV, \ - 'input' : Im,\ - 'regularisation_parameter':0.04, \ - 'number_of_iterations' :50 ,\ - 'tolerance_constant':1e-08,\ - 'methodTV': 0 ,\ - 'nonneg': 0 ,\ - 'printingOut': 0 - } - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - print ("_________testing ROF-TV (2D, CPU)__________") - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - res = True - rof_cpu = ROF_TV(pars_rof_tv['input'], - pars_rof_tv['regularisation_parameter'], - pars_rof_tv['number_of_iterations'], - pars_rof_tv['time_marching_parameter'],'cpu') - rms_rof = rmse(Im, rof_cpu) - # now compare obtained rms with the expected value - self.assertLess(abs(rms_rof-rms_rof_exp) , tolerance) - """ - if abs(rms_rof-self.rms_rof_exp) > self.tolerance: - raise TypeError('ROF-TV (2D, CPU) test FAILED') - else: - print ("test PASSED") - """ - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - print ("_________testing FGP-TV (2D, CPU)__________") - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - fgp_cpu = FGP_TV(pars_fgp_tv['input'], - pars_fgp_tv['regularisation_parameter'], - pars_fgp_tv['number_of_iterations'], - pars_fgp_tv['tolerance_constant'], - pars_fgp_tv['methodTV'], - pars_fgp_tv['nonneg'], - pars_fgp_tv['printingOut'],'cpu') - rms_fgp = rmse(Im, fgp_cpu) - # now compare obtained rms with the expected value - self.assertLess(abs(rms_fgp-rms_fgp_exp) , tolerance) - """ - if abs(rms_fgp-self.rms_fgp_exp) > self.tolerance: - raise TypeError('FGP-TV (2D, CPU) test FAILED') - else: - print ("test PASSED") - """ - self.assertTrue(res) - def test_gpu_regularisers(self): - filename = os.path.join(".." , ".." , ".." , "data" ,"lena_gray_512.tif") - - # read noiseless image - Im = plt.imread(filename) - Im = np.asarray(Im, dtype='float32') - - Im = Im/255 - tolerance = 1e-05 - rms_rof_exp = 0.006812507 #expected value for ROF model - rms_fgp_exp = 0.019152347 #expected value for FGP model - - # set parameters for ROF-TV - pars_rof_tv = {'algorithm': ROF_TV, \ - 'input' : Im,\ - 'regularisation_parameter':0.04,\ - 'number_of_iterations': 50,\ - 'time_marching_parameter': 0.0025 - } - # set parameters for FGP-TV - pars_fgp_tv = {'algorithm' : FGP_TV, \ - 'input' : Im,\ - 'regularisation_parameter':0.04, \ - 'number_of_iterations' :50 ,\ - 'tolerance_constant':1e-08,\ - 'methodTV': 0 ,\ - 'nonneg': 0 ,\ - 'printingOut': 0 - } - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - print ("_________testing ROF-TV (2D, GPU)__________") - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - res = True - rof_gpu = ROF_TV(pars_rof_tv['input'], - pars_rof_tv['regularisation_parameter'], - pars_rof_tv['number_of_iterations'], - pars_rof_tv['time_marching_parameter'],'gpu') - rms_rof = rmse(Im, rof_gpu) - # now compare obtained rms with the expected value - self.assertLess(abs(rms_rof-rms_rof_exp) , tolerance) - """ - if abs(rms_rof-self.rms_rof_exp) > self.tolerance: - raise TypeError('ROF-TV (2D, GPU) test FAILED') - else: - print ("test PASSED") - """ - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - print ("_________testing FGP-TV (2D, GPU)__________") - print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - fgp_gpu = FGP_TV(pars_fgp_tv['input'], - pars_fgp_tv['regularisation_parameter'], - pars_fgp_tv['number_of_iterations'], - pars_fgp_tv['tolerance_constant'], - pars_fgp_tv['methodTV'], - pars_fgp_tv['nonneg'], - pars_fgp_tv['printingOut'],'gpu') - rms_fgp = rmse(Im, fgp_gpu) - # now compare obtained rms with the expected value - self.assertLess(abs(rms_fgp-rms_fgp_exp) , tolerance) - """ - if abs(rms_fgp-self.rms_fgp_exp) > self.tolerance: - raise TypeError('FGP-TV (2D, GPU) test FAILED') - else: - print ("test PASSED") - """ - self.assertTrue(res) -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/Wrappers/Python/conda-recipe/run_test.py.in b/Wrappers/Python/conda-recipe/run_test.py.in new file mode 100644 index 0000000..9a6f4de --- /dev/null +++ b/Wrappers/Python/conda-recipe/run_test.py.in @@ -0,0 +1,148 @@ +import unittest +import numpy as np +from ccpi.filters.regularisers import ROF_TV, FGP_TV + +def rmse(im1, im2): + rmse = np.sqrt(np.sum((im1 - im2) ** 2) / float(im1.size)) + return rmse + +class TestRegularisers(unittest.TestCase): + + def setUp(self): + pass + + def test_cpu_regularisers(self): + #filename = os.path.join(".." , ".." , ".." , "data" ,"testLena.npy") + + Im = np.load('testLena.npy'); + """ + # read noiseless image + Im = plt.imread(filename) + Im = np.asarray(Im, dtype='float32') + + Im = Im/255 + """ + tolerance = 1e-05 + rms_rof_exp = 0.006812507 #expected value for ROF model + rms_fgp_exp = 0.019152347 #expected value for FGP model + + # set parameters for ROF-TV + pars_rof_tv = {'algorithm': ROF_TV, \ + 'input' : Im,\ + 'regularisation_parameter':0.04,\ + 'number_of_iterations': 50,\ + 'time_marching_parameter': 0.0025 + } + # set parameters for FGP-TV + pars_fgp_tv = {'algorithm' : FGP_TV, \ + 'input' : Im,\ + 'regularisation_parameter':0.04, \ + 'number_of_iterations' :50 ,\ + 'tolerance_constant':1e-08,\ + 'methodTV': 0 ,\ + 'nonneg': 0 ,\ + 'printingOut': 0 + } + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print ("_________testing ROF-TV (2D, CPU)__________") + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + res = True + rof_cpu = ROF_TV(pars_rof_tv['input'], + pars_rof_tv['regularisation_parameter'], + pars_rof_tv['number_of_iterations'], + pars_rof_tv['time_marching_parameter'],'cpu') + rms_rof = rmse(Im, rof_cpu) + # now compare obtained rms with the expected value + self.assertLess(abs(rms_rof-rms_rof_exp) , tolerance) + """ + if abs(rms_rof-self.rms_rof_exp) > self.tolerance: + raise TypeError('ROF-TV (2D, CPU) test FAILED') + else: + print ("test PASSED") + """ + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print ("_________testing FGP-TV (2D, CPU)__________") + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + fgp_cpu = FGP_TV(pars_fgp_tv['input'], + pars_fgp_tv['regularisation_parameter'], + pars_fgp_tv['number_of_iterations'], + pars_fgp_tv['tolerance_constant'], + pars_fgp_tv['methodTV'], + pars_fgp_tv['nonneg'], + pars_fgp_tv['printingOut'],'cpu') + rms_fgp = rmse(Im, fgp_cpu) + # now compare obtained rms with the expected value + self.assertLess(abs(rms_fgp-rms_fgp_exp) , tolerance) + """ + if abs(rms_fgp-self.rms_fgp_exp) > self.tolerance: + raise TypeError('FGP-TV (2D, CPU) test FAILED') + else: + print ("test PASSED") + """ + self.assertTrue(res) + def test_gpu_regularisers(self): + #filename = os.path.join(".." , ".." , ".." , "data" ,"testLena.npy") + + Im = np.load('testLena.npy'); + + #Im = Im/255 + tolerance = 1e-05 + rms_rof_exp = 0.006812507 #expected value for ROF model + rms_fgp_exp = 0.019152347 #expected value for FGP model + + # set parameters for ROF-TV + pars_rof_tv = {'algorithm': ROF_TV, \ + 'input' : Im,\ + 'regularisation_parameter':0.04,\ + 'number_of_iterations': 50,\ + 'time_marching_parameter': 0.0025 + } + # set parameters for FGP-TV + pars_fgp_tv = {'algorithm' : FGP_TV, \ + 'input' : Im,\ + 'regularisation_parameter':0.04, \ + 'number_of_iterations' :50 ,\ + 'tolerance_constant':1e-08,\ + 'methodTV': 0 ,\ + 'nonneg': 0 ,\ + 'printingOut': 0 + } + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print ("_________testing ROF-TV (2D, GPU)__________") + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + res = True + rof_gpu = ROF_TV(pars_rof_tv['input'], + pars_rof_tv['regularisation_parameter'], + pars_rof_tv['number_of_iterations'], + pars_rof_tv['time_marching_parameter'],'gpu') + rms_rof = rmse(Im, rof_gpu) + # now compare obtained rms with the expected value + self.assertLess(abs(rms_rof-rms_rof_exp) , tolerance) + """ + if abs(rms_rof-self.rms_rof_exp) > self.tolerance: + raise TypeError('ROF-TV (2D, GPU) test FAILED') + else: + print ("test PASSED") + """ + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print ("_________testing FGP-TV (2D, GPU)__________") + print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + fgp_gpu = FGP_TV(pars_fgp_tv['input'], + pars_fgp_tv['regularisation_parameter'], + pars_fgp_tv['number_of_iterations'], + pars_fgp_tv['tolerance_constant'], + pars_fgp_tv['methodTV'], + pars_fgp_tv['nonneg'], + pars_fgp_tv['printingOut'],'gpu') + rms_fgp = rmse(Im, fgp_gpu) + # now compare obtained rms with the expected value + self.assertLess(abs(rms_fgp-rms_fgp_exp) , tolerance) + """ + if abs(rms_fgp-self.rms_fgp_exp) > self.tolerance: + raise TypeError('FGP-TV (2D, GPU) test FAILED') + else: + print ("test PASSED") + """ + self.assertTrue(res) +if __name__ == '__main__': + unittest.main() diff --git a/Wrappers/Python/conda-recipe/testLena.npy b/Wrappers/Python/conda-recipe/testLena.npy new file mode 100644 index 0000000..14bc0e3 Binary files /dev/null and b/Wrappers/Python/conda-recipe/testLena.npy differ diff --git a/Wrappers/Python/demos/demo_cpu_regularisers.py b/Wrappers/Python/demos/demo_cpu_regularisers.py index fd3050c..00beb0b 100644 --- a/Wrappers/Python/demos/demo_cpu_regularisers.py +++ b/Wrappers/Python/demos/demo_cpu_regularisers.py @@ -22,6 +22,8 @@ def printParametersToString(pars): txt += "{0} = {1}".format(key, value.__name__) elif key == 'input': txt += "{0} = {1}".format(key, np.shape(value)) + elif key == 'refdata': + txt += "{0} = {1}".format(key, np.shape(value)) else: txt += "{0} = {1}".format(key, value) txt += '\n' @@ -196,7 +198,7 @@ plt.title('{}'.format('CPU results')) # Uncomment to test 3D regularisation performance #%% - +""" N = 512 slices = 20 @@ -318,8 +320,8 @@ a.set_title('Noisy Image') imgplot = plt.imshow(noisyVol[10,:,:],cmap="gray") # set parameters -pars = {'algorithm' : FGP_dTV, \ - 'input' : noisyVol,\ +pars = {'algorithm' : FGP_dTV,\ + 'input' : noisyVol,\ 'refdata' : noisyRef,\ 'regularisation_parameter':0.04, \ 'number_of_iterations' :300 ,\ @@ -358,4 +360,5 @@ a.text(0.15, 0.25, txtstr, transform=a.transAxes, fontsize=14, verticalalignment='top', bbox=props) imgplot = plt.imshow(fgp_dTV_cpu3D[10,:,:], cmap="gray") plt.title('{}'.format('Recovered volume on the CPU using FGP-dTV')) +""" #%% diff --git a/Wrappers/Python/demos/demo_cpu_vs_gpu_regularisers.py b/Wrappers/Python/demos/demo_cpu_vs_gpu_regularisers.py index aa1f865..310cf75 100644 --- a/Wrappers/Python/demos/demo_cpu_vs_gpu_regularisers.py +++ b/Wrappers/Python/demos/demo_cpu_vs_gpu_regularisers.py @@ -22,6 +22,8 @@ def printParametersToString(pars): txt += "{0} = {1}".format(key, value.__name__) elif key == 'input': txt += "{0} = {1}".format(key, np.shape(value)) + elif key == 'refdata': + txt += "{0} = {1}".format(key, np.shape(value)) else: txt += "{0} = {1}".format(key, value) txt += '\n' diff --git a/Wrappers/Python/demos/demo_gpu_regularisers.py b/Wrappers/Python/demos/demo_gpu_regularisers.py index 4759cc3..24a3c88 100644 --- a/Wrappers/Python/demos/demo_gpu_regularisers.py +++ b/Wrappers/Python/demos/demo_gpu_regularisers.py @@ -22,6 +22,8 @@ def printParametersToString(pars): txt += "{0} = {1}".format(key, value.__name__) elif key == 'input': txt += "{0} = {1}".format(key, np.shape(value)) + elif key == 'refdata': + txt += "{0} = {1}".format(key, np.shape(value)) else: txt += "{0} = {1}".format(key, value) txt += '\n' @@ -192,7 +194,7 @@ plt.title('{}'.format('GPU results')) # Uncomment to test 3D regularisation performance #%% - +""" N = 512 slices = 20 @@ -314,7 +316,7 @@ imgplot = plt.imshow(noisyVol[10,:,:],cmap="gray") # set parameters pars = {'algorithm' : FGP_dTV, \ - 'input' : noisyVol,\ + 'input' : noisyVol,\ 'refdata' : noisyRef,\ 'regularisation_parameter':0.04, \ 'number_of_iterations' :300 ,\ @@ -352,5 +354,5 @@ a.text(0.15, 0.25, txtstr, transform=a.transAxes, fontsize=14, verticalalignment='top', bbox=props) imgplot = plt.imshow(fgp_dTV_gpu3D[10,:,:], cmap="gray") plt.title('{}'.format('Recovered volume on the GPU using FGP-dTV')) - +""" #%% diff --git a/Wrappers/Python/src/cpu_regularisers.pyx b/Wrappers/Python/src/cpu_regularisers.pyx index 8f9185a..1661375 100644 --- a/Wrappers/Python/src/cpu_regularisers.pyx +++ b/Wrappers/Python/src/cpu_regularisers.pyx @@ -156,8 +156,8 @@ def dTV_FGP_2D(np.ndarray[np.float32_t, ndim=2, mode="c"] inputData, dTV_FGP_CPU_main(&inputData[0,0], &refdata[0,0], &outputData[0,0], regularisation_parameter, iterationsNumb, tolerance_param, - methodTV, eta_const, + methodTV, nonneg, printM, dims[0], dims[1], 1) diff --git a/Wrappers/Python/src/gpu_regularisers.pyx b/Wrappers/Python/src/gpu_regularisers.pyx index 4a14f69..18efdcd 100644 --- a/Wrappers/Python/src/gpu_regularisers.pyx +++ b/Wrappers/Python/src/gpu_regularisers.pyx @@ -20,7 +20,7 @@ cimport numpy as np cdef extern void TV_ROF_GPU_main(float* Input, float* Output, float lambdaPar, int iter, float tau, int N, int M, int Z); cdef extern void TV_FGP_GPU_main(float *Input, float *Output, float lambdaPar, int iter, float epsil, int methodTV, int nonneg, int printM, int N, int M, int Z); -cdef extern void dTV_FGP_CPU_main(float *Input, float *InputRef, float *Output, float lambdaPar, int iterationsNumb, float epsil, float eta, int methodTV, int nonneg, int printM, int N, int M, int Z); +cdef extern void dTV_FGP_GPU_main(float *Input, float *InputRef, float *Output, float lambdaPar, int iterationsNumb, float epsil, float eta, int methodTV, int nonneg, int printM, int N, int M, int Z); # Total-variation Rudin-Osher-Fatemi (ROF) def TV_ROF_GPU(inputData, @@ -187,8 +187,7 @@ def FGPTV3D(np.ndarray[np.float32_t, ndim=3, mode="c"] inputData, np.zeros([dims[0],dims[1],dims[2]], dtype='float32') # Running CUDA code here - TV_FGP_GPU_main( - &inputData[0,0,0], &outputData[0,0,0], + TV_FGP_GPU_main(&inputData[0,0,0], &outputData[0,0,0], regularisation_parameter , iterations, tolerance_param, @@ -204,7 +203,7 @@ def FGPTV3D(np.ndarray[np.float32_t, ndim=3, mode="c"] inputData, #****************************************************************# #******** Directional TV Fast-Gradient-Projection (FGP)*********# def FGPdTV2D(np.ndarray[np.float32_t, ndim=2, mode="c"] inputData, - np.ndarray[np.float32_t, ndim=3, mode="c"] refdata, + np.ndarray[np.float32_t, ndim=2, mode="c"] refdata, float regularisation_parameter, int iterations, float tolerance_param, -- cgit v1.2.3