SOPT
Sparse OPTimisation
inpainting.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 
9 #include "sopt/imaging_padmm.h"
10 #include "sopt/logging.h"
11 #include "sopt/maths.h"
13 #include "sopt/sampling.h"
14 #include "sopt/types.h"
15 #include "sopt/utilities.h"
16 #include "sopt/wavelets.h"
17 // This header is not part of the installed sopt interface
18 // It is only present in tests
19 #include "tools_for_tests/directories.h"
21 
22 // \min_{x} ||\Psi^Tx||_1 \quad \mbox{s.t.} \quad ||y - Ax||_2 < \epsilon and x \geq 0
23 int main(int argc, char const **argv) {
24  // Some type aliases for simplicity
25  using Scalar = double;
26  // Column vector - linear algebra - A * x is a matrix-vector multiplication
27  // type expected by ProximalADMM
29  // Matrix - linear algebra - A * x is a matrix-vector multiplication
30  // type expected by ProximalADMM
32  // Image - 2D array - A * x is a coefficient-wise multiplication
33  // Type expected by wavelets and image write/read functions
34  using Image = sopt::Image<Scalar>;
35 
36  std::string const input = argc >= 2 ? argv[1] : "cameraman256";
37  std::string const output = argc == 3 ? argv[2] : "none";
38  if (argc > 3) {
39  std::cout << "Usage:\n"
40  "$ "
41  << argv[0]
42  << " [input [output]]\n\n"
43  "- input: path to the image to clean (or name of standard SOPT image)\n"
44  "- output: filename pattern for output image\n";
45  exit(0);
46  }
47  // Set up random numbers for C and C++
48  auto const seed = std::time(nullptr);
49  std::srand(static_cast<unsigned int>(seed));
50  std::mt19937 mersenne(std::time(nullptr));
51 
52  // See set_level function for levels.
53  sopt::logging::set_level("debug");
54 
55  SOPT_HIGH_LOG("Read input file {}", input);
56  Image const image = sopt::tools::read_standard_tiff(input);
57 
58  SOPT_HIGH_LOG("Initializing sensing operator");
59  sopt::t_uint const nmeasure = 0.33 * image.size();
60  auto const sampling =
61  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
62 
63  SOPT_HIGH_LOG("Initializing wavelets");
64  auto const wavelet = sopt::wavelets::factory("DB4", 4);
65  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
66 
67  SOPT_HIGH_LOG("Computing proximal-ADMM parameters");
68  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
69  auto constexpr snr = 30.0;
70  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
71  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
72 
73  SOPT_HIGH_LOG("Create dirty vector");
74  std::normal_distribution<> gaussian_dist(0, sigma);
75  Vector y(y0.size());
76  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
77  // Write dirty imagte to file
78  if (output != "none") {
79  Vector const dirty = sampling.adjoint() * y;
80  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
81  "dirty_" + output + ".tiff");
82  }
83 
84  SOPT_HIGH_LOG("Creating proximal-ADMM Functor");
86  .itermax(500)
87  .regulariser_strength(1e-1)
88  .relative_variation(5e-4)
89  .l2ball_proximal_epsilon(epsilon)
90  .tight_frame(false)
91  .l1_proximal_tolerance(1e-2)
92  .l1_proximal_nu(1)
93  .l1_proximal_itermax(50)
94  .l1_proximal_positivity_constraint(true)
95  .l1_proximal_real_constraint(true)
97  .lagrange_update_scale(0.9)
98  .Psi(psi)
99  .Phi(sampling);
100 
101  SOPT_HIGH_LOG("Starting proximal-ADMM");
102  // Alternatively, padmm can be called with a tuple (x, residual) as argument
103  // Here, we default to (Φ^Ty/ν, ΦΦ^Ty/ν - y)
104  auto const diagnostic = padmm();
105  SOPT_HIGH_LOG("proximal-ADMM returned {}", diagnostic.good);
106 
107  // diagnostic should tell us the function converged
108  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
109  // diagnostic from the last call to the conjugate gradient.
110  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
111 
112  SOPT_HIGH_LOG("SOPT-proximal-ADMM converged in {} iterations", diagnostic.niters);
113  if (output != "none")
114  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
115  output + ".tiff");
116 
117  return 0;
118 }
sopt::t_real Scalar
An operator that samples a set of measurements.
Definition: sampling.h:17
ImagingProximalADMM< Scalar > & residual_convergence(Real const &tolerance)
Helper function to set-up default residual convergence function.
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
int main(int argc, char const **argv)
Definition: inpainting.cc:24
#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
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
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