-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #494 from odlgroup/cvx_interface
Add as_cvx_operator and example
- Loading branch information
Showing
12 changed files
with
275 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. _proximal_lang_in_depth: | ||
|
||
####################### | ||
Using ODL with ProxImaL | ||
####################### | ||
|
||
`Proximal | ||
<http://www.proximal-lang.org/en/latest/>`_ is a Python-embedded modeling language for image optimization problems and can be used with ODL to solve typical inverse problems phrased as optimization problems. The package is especially suited for non-differentiable problems such as total variance denoising. | ||
|
||
Here is a minimal example of solving Poisson's equation equation on an interval with a TV type regularizer (:math:`\min_x \ 10||-\Delta x - rhs||_2^2 + ||\nabla x||_1`):: | ||
|
||
>>> space = odl.uniform_discr(0, 1, 5) | ||
>>> op = -odl.Laplacian(space) | ||
>>> proximal_lang_op = odl.as_proximal_lang_operator(op) | ||
>>> rhs = space.element(lambda x: (x>0.4) & (x<0.6)) # indicator function on [0.4, 0.6] | ||
>>> x = proximal.Variable(space.shape) | ||
>>> prob = proximal.Problem([10 * proximal.sum_squares(x - rhs.asarray()), | ||
>>> proximal.norm1(proximal.grad(x))]) | ||
>>> prob.solve() | ||
>>> x.value | ||
array([ 0.02352054, 0.02647946, 0.9 , 0.02647946, 0.02352054]) | ||
|
||
Notable differences between ODL and ProxImaL | ||
============================================ | ||
|
||
It may be tempting to try to convert an arbitrary problem from ODL into ProxImaL, but some differences exist. | ||
|
||
Norms | ||
----- | ||
Norms in ODL are scaled according to the underlying function space. Hence a sequence of statements converging discretizations give rise to a converging norm:: | ||
|
||
>>> for n in range(2, 10000): | ||
... X = odl.uniform_discr(0, 1, n) | ||
... print(X.element(lambda x: x).norm()) | ||
0.559016994375 | ||
0.576628129734 | ||
0.577343052266 | ||
0.577350268468 | ||
>>> 1 / np.sqrt(3) # exact result | ||
0.577350269189 | ||
|
||
this is not the case in ProxImaL, where the norm depends on the number of discretization points. Hence a scaling that is correct for a problem in ODL needs not be correct in proximal. This also changes the definition of things like the operator norm. | ||
|
||
This also has the added effect of changing the definition of derived features, like the spectral norm of operators. | ||
|
||
Spaces | ||
------ | ||
ODL can represent some complicated spaces, like :math:`\mathbb{R}^3 \times \mathbb{C}^2` through the `ProductSpace` class:: | ||
|
||
>>> space = odl.ProductSpace(odl.rn(3), odl.cn(2)) | ||
|
||
This can then be used in solvers and other structures. ProxImaL currently lacks an equivalent structure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2014-2016 The ODL development group | ||
# | ||
# This file is part of ODL. | ||
# | ||
# ODL is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# ODL is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with ODL. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Poisson's problem using the ProxImaL solver. | ||
Solves the optimization problem | ||
min_x 10 ||laplacian(x) - g||_2^2 + || |grad(x)| ||_1 | ||
Where ``laplacian`` is the spatial Laplacian, ``grad`` the spatial | ||
gradient and ``g`` is given noisy data. | ||
""" | ||
|
||
import numpy as np | ||
import odl | ||
import proximal | ||
|
||
# Create space defined on a square from [0, 0] to [100, 100] with (100 x 100) | ||
# points | ||
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2014-2016 The ODL development group | ||
# | ||
# This file is part of ODL. | ||
# | ||
# ODL is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# ODL is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with ODL. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Tomography with TV regularization using the ProxImaL solver. | ||
Solves the optimization problem | ||
min_{0 <= x <= 1} ||A(x) - g||_2^2 + 0.2 || |grad(x)| ||_1 | ||
Where ``A`` is a parallel beam forward projector, ``grad`` the spatial | ||
gradient and ``g`` is given noisy data. | ||
""" | ||
|
||
import numpy as np | ||
import odl | ||
import proximal | ||
|
||
|
||
# --- Set up the forward operator (ray transform) --- # | ||
|
||
|
||
# Discrete reconstruction space: discretized functions on the rectangle | ||
# [-20, 20]^2 with 300 samples per dimension. | ||
reco_space = odl.uniform_discr( | ||
min_corner=[-20, -20], max_corner=[20, 20], nsamples=[300, 300], | ||
dtype='float32') | ||
|
||
# Make a parallel beam geometry with flat detector | ||
# Angles: uniformly spaced, n = 360, min = 0, max = 2 * pi | ||
angle_partition = odl.uniform_partition(0, 2 * np.pi, 360) | ||
# Detector: uniformly sampled, n = 558, min = -30, max = 30 | ||
detector_partition = odl.uniform_partition(-30, 30, 558) | ||
geometry = odl.tomo.Parallel2dGeometry(angle_partition, detector_partition) | ||
|
||
# The implementation of the ray transform to use, options: | ||
# 'scikit' Requires scikit-image (can be installed by | ||
# running ``pip install scikit-image``). | ||
# 'astra_cpu', 'astra_cuda' Requires astra tomography to be installed. | ||
# Astra is much faster than scikit. Webpage: | ||
# https://github.com/astra-toolbox/astra-toolbox | ||
impl = 'astra_cuda' | ||
|
||
# Initialize the ray transform (forward projection). | ||
ray_trafo = odl.tomo.RayTransform(reco_space, geometry, impl=impl) | ||
|
||
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters