Skip to content

Commit

Permalink
MAINT: updates to new ODL
Browse files Browse the repository at this point in the history
  • Loading branch information
adler-j committed Aug 28, 2018
1 parent 72c2dee commit f358cc4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 41 deletions.
3 changes: 2 additions & 1 deletion reference/reference_head_fbp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import odl
from odl.contrib import fom

mu_water = 0.02
photons_per_pixel = 10000.0
Expand Down Expand Up @@ -48,7 +49,7 @@
# Reconstruct using FBP
recon = pseudoinverse(-np.log(epsilon + data) / mu_water)

print('psnr = {}'.format(odl.util.psnr(phantom, recon)))
print('psnr = {}'.format(fom.psnr(phantom, recon)))

# Display results
phantom.show('phantom', clim=[0.8, 1.2])
Expand Down
11 changes: 6 additions & 5 deletions reference/reference_head_tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import odl
from odl.contrib import fom

mu_water = 0.02
photons_per_pixel = 10000.0
Expand All @@ -11,7 +12,7 @@
# Create ODL data structures
size = 512
space = odl.uniform_discr([-128, -128], [128, 128], [size, size],
dtype='float32', weighting='const')
dtype='float32', weighting=1.0)

# Make a fan beam geometry with flat detector
# Angles: uniformly spaced, n = 1000, min = 0, max = 2 * pi
Expand Down Expand Up @@ -56,7 +57,7 @@
op = odl.BroadcastOperator(nonlinear_operator, gradient)

# Do not use the g functional, set it to zero.
g = odl.solvers.ZeroFunctional(op.domain)
f = odl.solvers.ZeroFunctional(op.domain)

# Create functionals for the dual variable

Expand All @@ -67,7 +68,7 @@
l1_norm = 0.0002 * odl.solvers.L1Norm(gradient.range)

# Combine functionals, order must correspond to the operator K
f = odl.solvers.SeparableSum(data_discr, l1_norm)
g = odl.solvers.SeparableSum(data_discr, l1_norm)


# --- Select solver parameters and solve using Chambolle-Pock --- #
Expand All @@ -84,14 +85,14 @@
gamma = 0.01

# Pass callback to the solver to display intermediate results
callback = (odl.solvers.CallbackPrint(lambda x: odl.util.psnr(phantom, x)) &
callback = (odl.solvers.CallbackPrint(lambda x: fom.psnr(x, phantom)) &
odl.solvers.CallbackShow(clim=[0.8, 1.2]))

odl.solvers.pdhg(
x, f, g, op, tau=tau, sigma=sigma, niter=niter, gamma=gamma,
callback=callback)

print('psnr = {}'.format(odl.util.psnr(phantom, x)))
print('psnr = {}'.format(fom.psnr(phantom, x)))

# Display images
x.show('head TV', clim=[0.8, 1.2])
3 changes: 2 additions & 1 deletion reference/reference_shepp_fbp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import odl
from odl.contrib import fom


# Create ODL data structures
Expand Down Expand Up @@ -31,7 +32,7 @@

recon = pseudoinverse(data)

print('psnr = {}'.format(odl.util.psnr(phantom, recon)))
print('psnr = {}'.format(fom.psnr(phantom, recon)))

# Display images
data.show('Data')
Expand Down
5 changes: 3 additions & 2 deletions reference/reference_shepp_huber.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import odl
from odl.contrib import fom


# Create ODL data structures
Expand Down Expand Up @@ -61,7 +62,7 @@


# Optionally pass callback to the solver to display intermediate results
callback = (odl.solvers.CallbackPrint(lambda x: odl.util.psnr(phantom, x)) &
callback = (odl.solvers.CallbackPrint(lambda x: fom.psnr(phantom, x)) &
odl.solvers.CallbackShow(clim=[0.1, 0.4]))

# Choose a starting point
Expand All @@ -73,7 +74,7 @@
hessinv_estimate=hessinv_estimate,
callback=callback)

print('psnr = {}'.format(odl.util.psnr(phantom, x)))
print('psnr = {}'.format(fom.psnr(phantom, x)))

# Display images
x.show('Shepp-Logan TV windowed', clim=[0.1, 0.4])
Expand Down
23 changes: 7 additions & 16 deletions reference/reference_shepp_tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import odl
from odl.contrib.fom import psnr
from odl.contrib import fom


# Create ODL data structures
Expand Down Expand Up @@ -41,7 +41,7 @@
op = odl.BroadcastOperator(operator, gradient)

# Do not use the g functional, set it to zero.
g = odl.solvers.ZeroFunctional(op.domain)
f = odl.solvers.ZeroFunctional(op.domain)

# Create functionals for the dual variable

Expand All @@ -52,33 +52,24 @@
l1_norm = 0.3 * odl.solvers.L1Norm(gradient.range)

# Combine functionals, order must correspond to the operator K
f = odl.solvers.SeparableSum(l2_norm, l1_norm)
g = odl.solvers.SeparableSum(l2_norm, l1_norm)


# --- Select solver parameters and solve using Chambolle-Pock --- #


# Estimated operator norm, add 10 percent to ensure ||K||_2^2 * sigma * tau < 1
op_norm = 1.1 * odl.power_method_opnorm(op)

niter = 1000 # Number of iterations
tau = 0.1 # Step size for the primal variable
sigma = 1.0 / (op_norm ** 2 * tau) # Step size for the dual variable
gamma = 0.1

# Optionally pass callback to the solver to display intermediate results
callback = (odl.solvers.CallbackPrint(lambda x: psnr(phantom, x)) &
callback = (odl.solvers.CallbackPrint(lambda x: fom.psnr(x, phantom)) &
odl.solvers.CallbackShow(clim=[0.1, 0.4]))

# Choose a starting point
x = pseudoinverse(data)

# Run the algorithm
odl.solvers.pdhg(
x, f, g, op, tau=tau, sigma=sigma, niter=niter, gamma=gamma,
callback=callback)
x, f, g, op, niter=1000, gamma=0.3,
callback=None)

print('psnr = {}'.format(psnr(phantom, x)))
print('psnr = {}'.format(fom.psnr(x, phantom)))

# Display images
x.show('Shepp-Logan TV windowed', clim=[0.1, 0.4])
24 changes: 8 additions & 16 deletions reference/reference_shepp_tv_isotropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import numpy as np
import odl

from odl.contrib import fom

# Create ODL data structures
size = 128
space = odl.uniform_discr([-64, -64], [64, 64], [size, size],
dtype='float32')

# Creat parallel beam geometry
geometry = odl.tomo.parallel_beam_geometry(space, angles=30)
geometry = odl.tomo.parallel_beam_geometry(space, num_angles=30)

# Create ray transform operator
operator = odl.tomo.RayTransform(space, geometry)
Expand Down Expand Up @@ -41,44 +41,36 @@
op = odl.BroadcastOperator(operator, gradient)

# Do not use the g functional, set it to zero.
g = odl.solvers.ZeroFunctional(op.domain)
f = odl.solvers.ZeroFunctional(op.domain)

# Create functionals for the dual variable

# l2-squared data matching
l2_norm = odl.solvers.L2NormSquared(operator.range).translated(data)

# Isotropic TV-regularization i.e. the l1-norm
l1_norm = 0.3 * odl.solvers.L1Norm(gradient.range)
l1_norm = 0.26 * odl.solvers.GroupL1Norm(gradient.range)

# Combine functionals, order must correspond to the operator K
f = odl.solvers.SeparableSum(l2_norm, l1_norm)
g = odl.solvers.SeparableSum(l2_norm, l1_norm)


# --- Select solver parameters and solve using Chambolle-Pock --- #


# Estimated operator norm, add 10 percent to ensure ||K||_2^2 * sigma * tau < 1
op_norm = 1.1 * odl.power_method_opnorm(op)

niter = 1000 # Number of iterations
tau = 0.1 # Step size for the primal variable
sigma = 1.0 / (op_norm ** 2 * tau) # Step size for the dual variable
gamma = 0.1

# Optionally pass callback to the solver to display intermediate results
callback = (odl.solvers.CallbackPrint(lambda x: odl.util.psnr(phantom, x)) &
callback = (odl.solvers.CallbackPrint(lambda x: fom.psnr(x, phantom)) &
odl.solvers.CallbackShow(clim=[0.1, 0.4]))

# Choose a starting point
x = pseudoinverse(data)

# Run the algorithm
odl.solvers.pdhg(
x, f, g, op, tau=tau, sigma=sigma, niter=niter, gamma=gamma,
x, f, g, op, niter=1000, gamma=0.3,
callback=None)

print('psnr = {}'.format(odl.util.psnr(phantom, x)))
print('psnr = {}'.format(fom.psnr(x, phantom)))

# Display images
x.show('Shepp-Logan TV windowed', clim=[0.1, 0.4])

0 comments on commit f358cc4

Please sign in to comment.