def testTotalVariation(): im = scipy.misc.ascent() im = im[:, :, np.newaxis].astype(np.uint8) fn = proximal.norm1(proximal.grad(proximal.Variable(im.shape))) thresh = 6 out = fn.prox(thresh, im)
def solver(f, kernel_img, metric, cnn_func, elemental): """ Solves the deblurring problem for the given input and kernel image. :param f: Corrupted input image :type f: np.ndarray :param kernel_img: Blur kernel :type kernel_img: np.ndarray :param metric: Preinitialized metric :type metric: proximal.utils.metrics :param cnn_func: Preinitialized deployment CNN :type cnn_func: function :param elemental: General experiment configuration parameters :type elemental: Dict :returns: Reconstructed output image :rtype: np.ndarray """ # pylint:disable=no-value-for-parameter options = px.cg_options(tol=1e-4, num_iters=100, verbose=True) u = px.Variable(f.shape) alpha_sumsquare = elemental['alpha_data'] / 2.0 A_u = px.conv(kernel_img, u) prox_fns = px.sum_squares(A_u - f, alpha=alpha_sumsquare) if elemental['alpha_tv'] > 0.0: prox_fns += px.norm1(elemental['alpha_tv'] * px.grad(u)) prox_fns += init_denoising_prior(u, cnn_func, sigma=elemental['sigma'], sigma_scale=elemental['sigma_scale']) prob = init_problem(prox_fns) solve_problem(prob, x0=f.copy(), metric=metric, sigma=elemental['sigma'], lin_solver_options=options) return np.clip(u.value, 0.0, 1.0)
space = odl.uniform_discr([0, 0], [100, 100], [100, 100]) # Create ODL operator for the Laplacian laplacian = odl.Laplacian(space) # Create right hand side phantom = odl.phantom.shepp_logan(space, modified=True) phantom.show('original image') rhs = laplacian(phantom) rhs += odl.phantom.white_noise(space) * np.std(rhs) * 0.1 rhs.show('rhs') # Convert laplacian to ProxImaL operator proximal_lang_laplacian = odl.as_proximal_lang_operator(laplacian) # Convert to array rhs_arr = rhs.asarray() # Set up optimization problem x = proximal.Variable(space.shape) funcs = [10 * proximal.sum_squares(proximal_lang_laplacian(x) - rhs_arr), proximal.norm1(proximal.grad(x))] # Solve the problem using ProxImaL prob = proximal.Problem(funcs) prob.solve(verbose=True) # Convert back to odl and display result result_odl = space.element(x.value) result_odl.show('result from ProxImaL')
# Create ODL operator for the Laplacian laplacian = odl.Laplacian(space) # Create right hand side phantom = odl.phantom.shepp_logan(space, modified=True) phantom.show('original image') rhs = laplacian(phantom) rhs += odl.phantom.white_noise(space) * np.std(rhs) * 0.1 rhs.show('rhs') # Convert laplacian to ProxImaL operator proximal_lang_laplacian = odl.as_proximal_lang_operator(laplacian) # Convert to array rhs_arr = rhs.asarray() # Set up optimization problem x = proximal.Variable(space.shape) funcs = [ 10 * proximal.sum_squares(proximal_lang_laplacian(x) - rhs_arr), proximal.norm1(proximal.grad(x)) ] # Solve the problem using ProxImaL prob = proximal.Problem(funcs) prob.solve(verbose=True) # Convert back to odl and display result result_odl = space.element(x.value) result_odl.show('result from ProxImaL')
def solver(f, x0, metric, cnn_func, elemental): """ Solves the demosaicking problem for the given input. :param f: Corrupted input image :type f: np.ndarray :param x0: Predemosaicked initialization image :type x0: np.ndarray :param metric: Preinitialized metric :type metric: proximal.utils.metrics :param cnn_func: Preinitialized deployment CNN :type cnn_func: function :param elemental: General experiment configuration parameters :type elemental: Dict :returns: Reconstructed output image :rtype: np.ndarray """ # pylint:disable=no-value-for-parameter options = px.cg_options(tol=1e-4, num_iters=100, verbose=True) u = px.Variable(f.shape) A = bayer_mask(f.shape) A_u = px.mul_elemwise(A, u) alpha_sumsquare = elemental['alpha_data'] / 2.0 data = px.sum_squares(A_u - f, alpha=alpha_sumsquare) prox_fns = data if elemental['alpha_tv'] > 0.0: prox_fns += px.norm1(elemental['alpha_tv'] * px.grad(u, dims=2)) if elemental['alpha_cross'] > 0.0: grad_u = px.grad(u, dims=2) grad_x0 = px.grad(x0, dims=2).value x0_stacked = np.array([x0, x0]).reshape(x0.shape + (2, )) u_stacked = px.reshape(px.hstack([u, u]), x0.shape + (2, )) cross_1 = px.vstack([ px.mul_elemwise(np.roll(x0_stacked, 1, 2), grad_u), px.mul_elemwise(np.roll(x0_stacked, 2, 2), grad_u) ]) cross_2 = px.vstack([ px.mul_elemwise(np.roll(grad_x0, 1, 2), u_stacked), px.mul_elemwise(np.roll(grad_x0, 2, 2), u_stacked) ]) prox_fns += px.norm1(0.5 * elemental['alpha_cross'] * (cross_1 - cross_2)) prox_fns += init_denoising_prior(u, cnn_func, sigma=elemental['sigma'], sigma_scale=elemental['sigma_scale']) prob = init_problem(prox_fns) solve_problem(prob, x0=x0, metric=metric, sigma=elemental['sigma'], lin_solver_options=options) return np.clip(u.value, 0.0, 1.0)
# Convert ray transform to proximal language operator proximal_lang_ray_trafo = odl.as_proximal_lang_operator(ray_trafo) # Create sinogram of forward projected phantom with noise phantom = odl.phantom.shepp_logan(reco_space, modified=True) phantom.show('phantom') data = ray_trafo(phantom) data += odl.phantom.white_noise(ray_trafo.range) * np.mean(data) * 0.1 data.show('noisy data') # Convert to array for ProxImaL rhs_arr = data.asarray() # Set up optimization problem # Note that proximal is not aware of the underlying space and only works with # matrices. Hence the norm in proximal does not match the norm in the ODL space # exactly. x = proximal.Variable(reco_space.shape) funcs = [proximal.sum_squares(proximal_lang_ray_trafo(x) - rhs_arr), 0.2 * proximal.norm1(proximal.grad(x)), proximal.nonneg(x), proximal.nonneg(1 - x)] # Solve the problem using ProxImaL prob = proximal.Problem(funcs) prob.solve(verbose=True) # Convert back to odl and display result result_odl = reco_space.element(x.value) result_odl.show('ProxImaL result')