diff options
author | Tomas Kulhanek <tomas.kulhanek@stfc.ac.uk> | 2019-02-22 06:44:53 -0500 |
---|---|---|
committer | Tomas Kulhanek <tomas.kulhanek@stfc.ac.uk> | 2019-02-22 06:44:53 -0500 |
commit | 4505a79103e98adb33bfb4c10391319e56ae7031 (patch) | |
tree | 391e8ac544dc152bd9da8295a2446449764db6df /test | |
parent | c8a60f57df5a019b2b7295933dc0299d88f1e35c (diff) | |
download | regularization-4505a79103e98adb33bfb4c10391319e56ae7031.tar.gz regularization-4505a79103e98adb33bfb4c10391319e56ae7031.tar.bz2 regularization-4505a79103e98adb33bfb4c10391319e56ae7031.tar.xz regularization-4505a79103e98adb33bfb4c10391319e56ae7031.zip |
UPDATE: docs -> demos and update paths in m and py demos
Diffstat (limited to 'test')
-rw-r--r-- | test/test_CPU_regularisers.py | 91 | ||||
-rw-r--r-- | test/test_FGP_TV.py | 152 | ||||
-rw-r--r-- | test/test_ROF_TV.py | 3 |
3 files changed, 243 insertions, 3 deletions
diff --git a/test/test_CPU_regularisers.py b/test/test_CPU_regularisers.py new file mode 100644 index 0000000..42e4735 --- /dev/null +++ b/test/test_CPU_regularisers.py @@ -0,0 +1,91 @@ +import unittest +import math +import os +import timeit +from ccpi.filters.regularisers import FGP_TV, SB_TV, TGV, LLT_ROF, FGP_dTV, NDF, Diff4th, ROF_TV +from testroutines import * + +############################################################################### + +class TestRegularisers(unittest.TestCase): + + def getPars(self,alg,noi=1200): + filename = os.path.join("lena_gray_512.tif") + plt = TiffReader() + # read image + Im = plt.imread(filename) + Im = np.asarray(Im, dtype='float32') + Im = Im / 255 + perc = 0.05 + u0 = Im + np.random.normal(loc=0, + scale=perc * Im, + size=np.shape(Im)) + u_ref = Im + np.random.normal(loc=0, + scale=0.01 * Im, + size=np.shape(Im)) + # map the u0 u0->u0>0 + # f = np.frompyfunc(lambda x: 0 if x < 0 else x, 1,1) + u0 = u0.astype('float32') + u_ref = u_ref.astype('float32') + # set parameters + pars = {'algorithm': alg, \ + 'input': u0, \ + 'regularisation_parameter': 0.04, \ + 'number_of_iterations': noi, \ + 'tolerance_constant': 0.00001, \ + 'methodTV': 0, \ + 'nonneg': 0, \ + 'printingOut': 0, \ + 'time_marching_parameter': 0.00002 + } + return Im, pars + + + def test_FGP_TV_CPU(self): + Im, pars = self.getPars(FGP_TV) + + fgp_cpu = FGP_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['tolerance_constant'], + pars['methodTV'], + pars['nonneg'], + pars['printingOut'], 'cpu') + + rms = rmse(Im, fgp_cpu) + pars['rmse'] = rms + self.assertAlmostEqual(rms,0.02,delta=0.01) + + def test_TV_ROF_CPU(self): + # set parameters + Im, pars = self.getPars(ROF_TV) + # call routine + fgp_cpu = ROF_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['time_marching_parameter'], 'cpu') + + rms = rmse(Im, fgp_cpu) + pars['rmse'] = rms + + #txtstr = printParametersToString(pars) + #print(txtstr) + # now test that it generates some expected output + self.assertAlmostEqual(rms,0.02,delta=0.01) + + def test_SB_TV_CPU(self): + # set parameters + Im, pars = self.getPars(SB_TV) + # call routine + fgp_cpu = SB_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['time_marching_parameter'], 'cpu') + + rms = rmse(Im, fgp_cpu) + pars['rmse'] = rms + + #txtstr = printParametersToString(pars) + #print(txtstr) + # now test that it generates some expected output + self.assertAlmostEqual(rms,0.02,delta=0.01) diff --git a/test/test_FGP_TV.py b/test/test_FGP_TV.py new file mode 100644 index 0000000..f0dc540 --- /dev/null +++ b/test/test_FGP_TV.py @@ -0,0 +1,152 @@ +import unittest +import math +import os +import timeit +from ccpi.filters.regularisers import FGP_TV +#, FGP_TV, SB_TV, TGV, LLT_ROF, FGP_dTV, NDF, Diff4th +from testroutines import * + +############################################################################### + +class TestRegularisers(unittest.TestCase): + + def test_FGP_TV_CPU(self): + print(__name__) + filename = os.path.join("lena_gray_512.tif") + plt = TiffReader() + # read image + Im = plt.imread(filename) + Im = np.asarray(Im, dtype='float32') + + Im = Im / 255 + perc = 0.05 + u0 = Im + np.random.normal(loc=0, + scale=perc * Im, + size=np.shape(Im)) + u_ref = Im + np.random.normal(loc=0, + scale=0.01 * Im, + size=np.shape(Im)) + + # map the u0 u0->u0>0 + # f = np.frompyfunc(lambda x: 0 if x < 0 else x, 1,1) + u0 = u0.astype('float32') + u_ref = u_ref.astype('float32') + + print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print("____________FGP-TV bench___________________") + print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + + # set parameters + pars = {'algorithm': FGP_TV, \ + 'input': u0, \ + 'regularisation_parameter': 0.04, \ + 'number_of_iterations': 1200, \ + 'tolerance_constant': 0.00001, \ + 'methodTV': 0, \ + 'nonneg': 0, \ + 'printingOut': 0 + } + + print("#############FGP TV CPU####################") + start_time = timeit.default_timer() + fgp_cpu = FGP_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['tolerance_constant'], + pars['methodTV'], + pars['nonneg'], + pars['printingOut'], 'cpu') + + rms = rmse(Im, fgp_cpu) + pars['rmse'] = rms + + txtstr = printParametersToString(pars) + txtstr += "%s = %.3fs" % ('elapsed time', timeit.default_timer() - start_time) + print(txtstr) + self.assertTrue(math.isclose(rms,0.02,rel_tol=1e-1)) + + def test_FGP_TV_CPU_vs_GPU(self): + print(__name__) + filename = os.path.join("lena_gray_512.tif") + plt = TiffReader() + # read image + Im = plt.imread(filename) + Im = np.asarray(Im, dtype='float32') + + Im = Im / 255 + perc = 0.05 + u0 = Im + np.random.normal(loc=0, + scale=perc * Im, + size=np.shape(Im)) + u_ref = Im + np.random.normal(loc=0, + scale=0.01 * Im, + size=np.shape(Im)) + + # map the u0 u0->u0>0 + # f = np.frompyfunc(lambda x: 0 if x < 0 else x, 1,1) + u0 = u0.astype('float32') + u_ref = u_ref.astype('float32') + + print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + print("____________FGP-TV bench___________________") + print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + + # set parameters + pars = {'algorithm': FGP_TV, \ + 'input': u0, \ + 'regularisation_parameter': 0.04, \ + 'number_of_iterations': 1200, \ + 'tolerance_constant': 0.00001, \ + 'methodTV': 0, \ + 'nonneg': 0, \ + 'printingOut': 0 + } + + print("#############FGP TV CPU####################") + start_time = timeit.default_timer() + fgp_cpu = FGP_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['tolerance_constant'], + pars['methodTV'], + pars['nonneg'], + pars['printingOut'], 'cpu') + + rms = rmse(Im, fgp_cpu) + pars['rmse'] = rms + + txtstr = printParametersToString(pars) + txtstr += "%s = %.3fs" % ('elapsed time', timeit.default_timer() - start_time) + print(txtstr) + + print("##############FGP TV GPU##################") + start_time = timeit.default_timer() + try: + fgp_gpu = FGP_TV(pars['input'], + pars['regularisation_parameter'], + pars['number_of_iterations'], + pars['tolerance_constant'], + pars['methodTV'], + pars['nonneg'], + pars['printingOut'], 'gpu') + + except ValueError as ve: + self.skipTest("Results not comparable. GPU computing error.") + + rms = rmse(Im, fgp_gpu) + pars['rmse'] = rms + pars['algorithm'] = FGP_TV + txtstr = printParametersToString(pars) + txtstr += "%s = %.3fs" % ('elapsed time', timeit.default_timer() - start_time) + print(txtstr) + + print("--------Compare the results--------") + tolerance = 1e-05 + diff_im = np.zeros(np.shape(fgp_cpu)) + diff_im = abs(fgp_cpu - fgp_gpu) + diff_im[diff_im > tolerance] = 1 + + self.assertLessEqual(diff_im.sum(), 1) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_ROF_TV.py b/test/test_ROF_TV.py index dda38b7..fa35680 100644 --- a/test/test_ROF_TV.py +++ b/test/test_ROF_TV.py @@ -53,9 +53,6 @@ class TestRegularisers(unittest.TestCase): def test_ROF_TV_CPU_vs_GPU(self): - # print ("tomas debug test function") - print(__name__) - self.fail("testfail2") filename = os.path.join("lena_gray_512.tif") plt = TiffReader() # read image |