SOPT
Sparse OPTimisation
inpainting_joint_map.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <exception>
3 #include <functional>
4 #include <iostream>
5 #include <random>
6 #include <vector>
7 #include <ctime>
8 
11 #include "sopt/joint_map.h"
12 #include "sopt/logging.h"
13 #include "sopt/maths.h"
15 #include "sopt/sampling.h"
16 #include "sopt/types.h"
17 #include "sopt/utilities.h"
18 #include "sopt/wavelets.h"
19 // This header is not part of the installed sopt interface
20 // It is only present in tests
21 #include "tools_for_tests/directories.h"
23 
24 // \min_{x} ||\Psi^Tx||_1 \quad \mbox{s.t.} \quad ||y - Ax||_2 < \epsilon and x \geq 0
25 int main(int argc, char const **argv) {
26  // Some type aliases for simplicity
27  using Scalar = double;
28  // Column vector - linear algebra - A * x is a matrix-vector multiplication
29  // type expected by Forward Backward
31  // Matrix - linear algebra - A * x is a matrix-vector multiplication
32  // type expected by Forward Backward
34  // Image - 2D array - A * x is a coefficient-wise multiplication
35  // Type expected by wavelets and image write/read functions
36  using Image = sopt::Image<Scalar>;
37 
38  std::string const input = argc >= 2 ? argv[1] : "cameraman256";
39  std::string const output = argc == 3 ? argv[2] : "none";
40  if (argc > 3) {
41  std::cout << "Usage:\n"
42  "$ "
43  << argv[0]
44  << " [input [output]]\n\n"
45  "- input: path to the image to clean (or name of standard SOPT image)\n"
46  "- output: filename pattern for output image\n";
47  exit(0);
48  }
49  // Set up random numbers for C and C++
50  auto const seed = std::time(nullptr);
51  std::srand(static_cast<unsigned int>(seed));
52  std::mt19937 mersenne(std::time(nullptr));
53 
54  // See set_level function for levels.
55  sopt::logging::set_level("debug");
56  SOPT_HIGH_LOG("Read input file {}", input);
57  Image const image = sopt::tools::read_standard_tiff(input);
58  SOPT_HIGH_LOG("Image size: {} x {} = {}", image.cols(), image.rows(), image.size());
59 
60  SOPT_HIGH_LOG("Initializing sensing operator");
61  sopt::t_uint const nmeasure = std::floor(0.5 * image.size());
62  sopt::LinearTransform<Vector> const sampling =
63  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
64  SOPT_HIGH_LOG("Initializing wavelets");
65  auto const wavelet = sopt::wavelets::factory("DB8", 4);
66 
67  // sopt::wavelets::SARA const wavelet{std::make_tuple("db1", 4u), std::make_tuple("db2", 4u),
68  // std::make_tuple("db3", 4u), std::make_tuple("db4", 4u)};
69 
70  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
71  SOPT_LOW_LOG("Wavelet coefficients: {}", (psi.adjoint() * image).size());
72 
73  SOPT_HIGH_LOG("Computing Forward Backward parameters");
74  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
75  auto constexpr snr = 30.0;
76  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
77  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
78 
79  SOPT_HIGH_LOG("Create dirty vector");
80  std::normal_distribution<> gaussian_dist(0, sigma);
81  Vector y(y0.size());
82  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
83  // Write dirty imagte to file
84  if (output != "none") {
85  Vector const dirty = sampling.adjoint() * y;
86  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
87  "dirty_" + output + ".tiff");
88  }
89 
90  sopt::t_real constexpr regulariser_strength = 0;
91  sopt::t_real const beta = sigma * sigma * 0.5;
92  SOPT_HIGH_LOG("Creating Foward Backward Functor");
93  auto fb = std::make_shared<sopt::algorithm::ImagingForwardBackward<Scalar>>(y);
94  fb->itermax(500)
95  .step_size(beta) // stepsize
96  .sigma(sigma) // sigma
97  .regulariser_strength(regulariser_strength) // regularisation paramater
98  .relative_variation(1e-3)
99  .residual_tolerance(0)
100  .tight_frame(true)
101  .Phi(sampling);
102 
103  // Create a shared pointer to an instance of the L1GProximal class
104  // and set its properties
105  auto gp = std::make_shared<sopt::algorithm::L1GProximal<Scalar>>(false);
106  gp->l1_proximal_tolerance(1e-4)
107  .l1_proximal_nu(1)
108  .l1_proximal_itermax(50)
109  .l1_proximal_positivity_constraint(true)
110  .l1_proximal_real_constraint(true)
111  .Psi(psi);
112 
113  // Once the properties are set, inject it into the ImagingForwardBackward object
114  fb->g_function(gp);
115 
116  SOPT_HIGH_LOG("Starting Forward Backward");
117  // Alternatively, forward-backward can be called with a tuple (x, residual) as argument
118  // Here, we default to (y, Φx/ν - y)
119  auto l1_norm = [psi](const Vector &x) { return sopt::l1_norm(psi.adjoint() * x); };
120  const sopt::t_uint number_of_wavelet_coefficients = image.size();
122  fb, l1_norm, number_of_wavelet_coefficients)
123  .alpha(1)
124  .beta(1)
125  .k(1)
126  .relative_variation(1e-3)
127  .objective_variation(1e-3);
128  auto diagnostic = joint_map();
129  SOPT_HIGH_LOG("Forward backward returned {}", diagnostic.good);
130  if (output != "none")
131  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
132  output + ".tiff");
133  // diagnostic should tell us the function converged
134  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
135  // diagnostic from the last call to the conjugate gradient.
136  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
137 
138  SOPT_HIGH_LOG("SOPT-Forward Backward converged in {} iterations", diagnostic.niters);
139 
140  return 0;
141 }
sopt::t_real Scalar
Joins together direct and indirect operators.
LinearTransform< VECTOR > adjoint() const
Indirect transform.
An operator that samples a set of measurements.
Definition: sampling.h:17
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
int main(int argc, char const **argv)
#define SOPT_LOW_LOG(...)
Low priority message.
Definition: logging.h:227
#define SOPT_HIGH_LOG(...)
High priority message.
Definition: logging.h:223
void set_level(const std::string &level)
Method to set the logging level of the default Log object.
Definition: logging.h:154
Image read_standard_tiff(std::string const &name)
Reads tiff image from sopt data directory if it exists.
Definition: tiffwrappers.cc:9
void write_tiff(Image<> const &image, std::string const &filename)
Writes a tiff greyscale file.
Definition: utilities.cc:68
Wavelet factory(const std::string &name, t_uint nlevels)
Creates a wavelet transform object.
Definition: wavelets.cc:8
int t_int
Root of the type hierarchy for signed integers.
Definition: types.h:13
Vector< T > dirty(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image, RANDOM &mersenne)
Definition: inpainting.h:25
double t_real
Root of the type hierarchy for real numbers.
Definition: types.h:17
size_t t_uint
Root of the type hierarchy for unsigned integers.
Definition: types.h:15
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Definition: types.h:39
real_type< T >::type epsilon(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:38
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
real_type< T >::type sigma(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:17
real_type< typename T0::Scalar >::type l1_norm(Eigen::ArrayBase< T0 > const &input, Eigen::ArrayBase< T1 > const &weights)
Computes weighted L1 norm.
Definition: maths.h:116
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29
sopt::Vector< Scalar > Vector
Definition: inpainting.cc:28
sopt::Matrix< Scalar > Matrix
Definition: inpainting.cc:29
sopt::Image< Scalar > Image
Definition: inpainting.cc:30