def check_filter_handler(shape, type, orthonormal=False): x = np.random.normal(0, 1, shape) filter_handler = dictionaries.get_filter_handler(x, type) np.testing.assert_array_almost_equal(x.ravel(), filter_handler.dot(filter_handler.inverse_dot(x)).ravel()) np.testing.assert_array_almost_equal(x.ravel(), filter_handler.inverse_transpose_dot(filter_handler.transpose_dot(x)).ravel()) if orthonormal: np.testing.assert_array_almost_equal(x.ravel(), filter_handler.dot( filter_handler.transpose_dot(x)).ravel())
def check_gradient(shape, type, orthonormal=False): x = np.random.rand(*shape) filter_handler = dictionaries.get_filter_handler(x, type) projector = ot_sparse_projection.OtFiltering(filter_handler, GAMMA, K) check_gradient_for_projection(projector, x) projector = ot_sparse_projection.OtSparseProjectionInvertible( filter_handler, GAMMA, LAMB) check_gradient_for_projection(projector, x) if orthonormal: projector = ot_sparse_projection.OtSparseProjectionOrthogonal( filter_handler, GAMMA, LAMB) check_gradient_for_projection(projector, x)
def check_filter_handler_2(shape, type, orthonormal=False): x = np.random.normal(0, 1, shape) filter_handler = dictionaries.get_filter_handler(x, type) n = np.prod(filter_handler._shape_wav) for i in range(n): x = np.zeros((n, 1)) x[i] = 1 np.testing.assert_array_almost_equal(x.ravel(), filter_handler.dot(filter_handler.inverse_dot(x)).ravel()) np.testing.assert_array_almost_equal(x.ravel(), filter_handler.inverse_transpose_dot( filter_handler.transpose_dot(x)).ravel()) if orthonormal: np.testing.assert_array_almost_equal(x.ravel(), filter_handler.dot( filter_handler.transpose_dot(x)).ravel())
def check_filter_handler_3(shape, type, orthonormal=False): x = np.random.normal(0, 1, shape) filter_handler = dictionaries.get_filter_handler(x, type) n = np.prod(filter_handler._shape_wav) m = get_matrix(n, filter_handler.dot) m_inv = get_matrix(n, filter_handler.inverse_dot) m_T = get_matrix(n, filter_handler.transpose_dot) m_inv_T = get_matrix(n, filter_handler.inverse_transpose_dot) np.testing.assert_array_almost_equal(m, m_T.T) np.testing.assert_array_almost_equal(m_inv, m_inv_T.T) np.testing.assert_array_almost_equal(m_inv, np.linalg.inv(m)) np.testing.assert_array_almost_equal(m_T, np.linalg.inv(m_inv_T)) if orthonormal: np.testing.assert_array_almost_equal(m, m_inv_T) np.testing.assert_array_almost_equal(m_inv, m_T)
from ot_sparse_projection import ot_sparse_projection, misc from ot_sparse_projection.dictionaries import get_filter_handler filter_type = 'dct' gamma = .1 n = 256 imName = 'camera' folder = join(pardir, 'img', 'filtering') if not exists(folder): makedirs(folder) k = int(n / 4) im, scaling = misc.get_image(imName, n) filter_handler = get_filter_handler(im, filter_type) Y, Z, obj = ot_sparse_projection.wasserstein_image_low_pass( im, filter_handler, k, gamma) print(Y.sum()) ZZ = Z.copy() recons = filter_handler.dot(filter_handler.reshape_coeffs(ZZ)).reshape( im.shape) sparsity = misc.get_sparsity(ZZ) Y_l2, Z_l2 = filter_handler.low_pass_filter(im, k) Y_l2 = Y_l2.reshape(im.shape) print("l1-norm of coefficients: {}".format(np.abs(Z).sum())) print("sparsity: {}".format(sparsity)) print("l2 sparsity: {}".format(misc.get_sparsity(Z_l2)))
NEW_THRESH = "newThresh" colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] adaptive_values = {"normalShrink": [adaptive_thresholding.NormalShrink, 'd', colors[4]], "sureShrink": [adaptive_thresholding.SureShrink, 'o', colors[5]], "bayesShrink": [adaptive_thresholding.BayesShrink, 'p', colors[6]], "visuShrink": [adaptive_thresholding.VisuShrink, '*', 'r']} plot_values = {W_SOFT: ['-x', 'OT soft thresholding', colors[3]], W_HARD: ['-o', 'OT hard thresholding', colors[1]], SOFT: ['-d', 'Euclidean soft thresholding', colors[0]], HARD: ['-s', 'Euclidean hard thresholding', colors[2]]} original, scaling = misc.get_image(imName, n) filter_handler = get_filter_handler(original, filter_type) mask = filter_handler.inverse_dot(original) mask = np.ones_like(mask) mask = filter_handler.array_to_coeffs(mask) mask[0] = np.zeros_like(mask[0]) mask = filter_handler.coeffs_to_array(mask) noises = {ns.SALT_PEPPER: [.05, .1, .15], ns.GAUSSIAN: [.2, .3, .4 ]} noise_names = {ns.SALT_PEPPER: "Salt \\& pepper", ns.GAUSSIAN: "Gaussian"} SSIM = "SSIM" PSNR = "pSNR" similarities = {SSIM: ns.ssim, PSNR: ns.psnr}
# Choose the type of wavelet of fourier decomposition you want filter_type = 'dct' # Optimal transport regularization strength. Use .1 unless you know better gamma = .1 n = 256 # Used to resize the image to n x n pixels lamb = 2.5 # l1 regularization strength. Higher values increase sparsity imName = 'ascent' # Path to your image, or name of a pre-configured image im, scaling = misc.get_image( imName, n) # get your image, with a rescaling which is usefull to always keep # similar regularization values filter_handler = get_filter_handler( im, filter_type) # handler for Fourier or wavelet transforms Y, Z, obj = ot_sparse_projection.wasserstein_image_filtering_invertible_dictionary( im, filter_handler, gamma, lamb) # this computes the optimal transport coefficient shrinkage sparsity_pattern = np.not_equal(0, Z) _, Z_wasserstein_hard, obj_hard = ot_sparse_projection.OtFilteringSpecificPattern( filter_handler, gamma, sparsity_pattern, ).projection(im) # this computes the optimal transport hard thresholding sparsity = misc.get_sparsity(Z) Y_l2, Z_l2 = l2.sparse_projection( im, filter_handler, sparsity) # Euclidean coefficient shrinkage with same sparsity Y_l2_hard, Z_l2_hard = l2.hard_thresholding(