SOPT
Sparse OPTimisation
Functions
l2_inpainting.cc File Reference
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include "sopt/l2_forward_backward.h"
#include "sopt/logging.h"
#include "sopt/maths.h"
#include "sopt/relative_variation.h"
#include "sopt/sampling.h"
#include "sopt/types.h"
#include "sopt/utilities.h"
#include "sopt/wavelets.h"
#include "tools_for_tests/directories.h"
#include "tools_for_tests/tiffwrappers.h"
+ Include dependency graph for l2_inpainting.cc:

Go to the source code of this file.

Functions

int main (int argc, char const **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char const **  argv 
)

Definition at line 23 of file l2_inpainting.cc.

23  {
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 Forward Backward
29  // Matrix - linear algebra - A * x is a matrix-vector multiplication
30  // type expected by Forward Backward
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  SOPT_HIGH_LOG("Read input file {}", input);
55  Image const image = sopt::tools::read_standard_tiff(input);
56  SOPT_HIGH_LOG("Image size: {} x {} = {}", image.cols(), image.rows(), image.size());
57 
58  SOPT_HIGH_LOG("Initializing sensing operator");
59  sopt::t_uint const nmeasure = std::floor(0.5 * image.size());
60  sopt::LinearTransform<Vector> const sampling =
61  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
62  SOPT_HIGH_LOG("Initializing wavelets");
63  auto const wavelet = sopt::wavelets::factory("DB8", 4);
64 
65  // sopt::wavelets::SARA const wavelet{std::make_tuple("db1", 4u), std::make_tuple("db2", 4u),
66  // std::make_tuple("db3", 4u), std::make_tuple("db4", 4u)};
67 
68  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
69  SOPT_LOW_LOG("Wavelet coefficients: {}", (psi.adjoint() * image).size());
70 
71  SOPT_HIGH_LOG("Computing Forward Backward parameters");
72  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
73  auto constexpr snr = 30.0;
74  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
75  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
76 
77  SOPT_HIGH_LOG("Create dirty vector");
78  std::normal_distribution<> gaussian_dist(0, sigma);
79  Vector y(y0.size());
80  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
81  // Write dirty imagte to file
82  if (output != "none") {
83  Vector const dirty = sampling.adjoint() * y;
84  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
85  "dirty_" + output + ".tiff");
86  }
87  constexpr sopt::t_real x_sigma = 1.;
88  sopt::t_real constexpr regulariser_strength = 1. / (x_sigma * x_sigma * 2);
89  sopt::t_real const step_size = sigma * sigma * 0.5;
90  SOPT_HIGH_LOG("Creating Foward Backward Functor");
92  .itermax(500)
93  .step_size(step_size) // stepsize
94  .sigma(sigma) // sigma
95  .regulariser_strength(regulariser_strength) // regularisation paramater
96  .relative_variation(1e-3)
97  .residual_tolerance(0)
98  .tight_frame(true)
99  .Phi(sampling);
100 
101  SOPT_HIGH_LOG("Starting Forward Backward");
102  // Alternatively, forward-backward can be called with a tuple (x, residual) as argument
103  // Here, we default to (y, Φx/ν - y)
104  Vector const init_map = Vector::Ones(image.size()) * x_sigma;
105  Vector const init_res = y - (sampling * init_map);
106  const std::tuple<Vector, Vector> warm_start = {init_map, init_res};
107  auto const diagnostic = fb();
108  SOPT_HIGH_LOG("Forward backward returned {}", diagnostic.good);
109 
110  if (output != "none")
111  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
112  output + ".tiff");
113  // diagnostic should tell us the function converged
114  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
115  // diagnostic from the last call to the conjugate gradient.
116  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
117 
118  SOPT_HIGH_LOG("SOPT-Forward Backward converged in {} iterations", diagnostic.niters);
119 
120  return 0;
121 }
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
L2ForwardBackward &::type Phi(ARGS &&... args)
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
#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
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

References sopt::LinearTransform< VECTOR >::adjoint(), sopt::dirty(), sopt::epsilon(), sopt::wavelets::factory(), mersenne(), sopt::algorithm::L2ForwardBackward< SCALAR >::Phi(), sopt::tools::read_standard_tiff(), sopt::logging::set_level(), sopt::sigma(), SOPT_HIGH_LOG, SOPT_LOW_LOG, and sopt::utilities::write_tiff().