summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/lena_gray_512.tifbin0 -> 262598 bytes
-rw-r--r--test/test_ROF_TV.py127
-rw-r--r--test/testroutines.py37
3 files changed, 164 insertions, 0 deletions
diff --git a/test/lena_gray_512.tif b/test/lena_gray_512.tif
new file mode 100644
index 0000000..f80cafc
--- /dev/null
+++ b/test/lena_gray_512.tif
Binary files differ
diff --git a/test/test_ROF_TV.py b/test/test_ROF_TV.py
new file mode 100644
index 0000000..dda38b7
--- /dev/null
+++ b/test/test_ROF_TV.py
@@ -0,0 +1,127 @@
+import unittest
+import math
+import os
+import timeit
+from ccpi.filters.regularisers import ROF_TV
+#, FGP_TV, SB_TV, TGV, LLT_ROF, FGP_dTV, NDF, Diff4th
+from testroutines import *
+
+class TestRegularisers(unittest.TestCase):
+
+ def test_ROF_TV_CPU(self):
+ 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': ROF_TV, \
+ 'input': u0, \
+ 'regularisation_parameter': 0.04, \
+ 'number_of_iterations': 2500, \
+ 'time_marching_parameter': 0.00002
+ }
+ print("#############ROF TV CPU####################")
+ start_time = timeit.default_timer()
+ rof_cpu = ROF_TV(pars['input'],
+ pars['regularisation_parameter'],
+ pars['number_of_iterations'],
+ pars['time_marching_parameter'], 'cpu')
+ rms = rmse(Im, rof_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.02067839,rel_tol=1e-2))
+
+
+ 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
+ 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("____________ROF-TV bench___________________")
+ print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
+
+ # set parameters
+ pars = {'algorithm': ROF_TV, \
+ 'input': u0, \
+ 'regularisation_parameter': 0.04, \
+ 'number_of_iterations': 2500, \
+ 'time_marching_parameter': 0.00002
+ }
+ print("##############ROF TV GPU##################")
+ start_time = timeit.default_timer()
+ try:
+ rof_gpu = ROF_TV(pars['input'],
+ pars['regularisation_parameter'],
+ pars['number_of_iterations'],
+ pars['time_marching_parameter'], 'gpu')
+ except ValueError as ve:
+ self.skipTest("Results not comparable. GPU computing error.")
+
+ rms = rmse(Im, rof_gpu)
+ pars['rmse'] = rms
+ pars['algorithm'] = ROF_TV
+ txtstr = printParametersToString(pars)
+ txtstr += "%s = %.3fs" % ('elapsed time', timeit.default_timer() - start_time)
+ print(txtstr)
+
+ print("#############ROF TV CPU####################")
+ start_time = timeit.default_timer()
+ rof_cpu = ROF_TV(pars['input'],
+ pars['regularisation_parameter'],
+ pars['number_of_iterations'],
+ pars['time_marching_parameter'], 'cpu')
+ rms = rmse(Im, rof_cpu)
+ pars['rmse'] = rms
+
+ txtstr = printParametersToString(pars)
+ txtstr += "%s = %.3fs" % ('elapsed time', timeit.default_timer() - start_time)
+ print(txtstr)
+ print("--------Compare the results--------")
+ tolerance = 1e-04
+ diff_im = np.zeros(np.shape(rof_cpu))
+ diff_im = abs(rof_cpu - rof_gpu)
+ diff_im[diff_im > tolerance] = 1
+ self.assertLessEqual(diff_im.sum(), 1)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/test/testroutines.py b/test/testroutines.py
new file mode 100644
index 0000000..8da5c5e
--- /dev/null
+++ b/test/testroutines.py
@@ -0,0 +1,37 @@
+import numpy as np
+from PIL import Image
+
+class TiffReader(object):
+ def imread(self, filename):
+ return np.asarray(Image.open(filename))
+
+
+###############################################################################
+def printParametersToString(pars):
+ txt = r''
+ for key, value in pars.items():
+ if key == 'algorithm':
+ 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'
+ return txt
+
+
+def nrmse(im1, im2):
+ rmse = np.sqrt(np.sum((im2 - im1) ** 2) / float(im1.size))
+ max_val = max(np.max(im1), np.max(im2))
+ min_val = min(np.min(im1), np.min(im2))
+ return 1 - (rmse / (max_val - min_val))
+
+
+def rmse(im1, im2):
+ rmse = np.sqrt(np.sum((im1 - im2) ** 2) / float(im1.size))
+ return rmse
+
+
+###############################################################################