SOPT
Sparse OPTimisation
Functions
inpainting.cc File Reference
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include "sopt/logging.h"
#include "sopt/maths.h"
#include "sopt/relative_variation.h"
#include "sopt/sampling.h"
#include "sopt/sdmm.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 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 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 SDMM
29  // Matrix - linear algebra - A * x is a matrix-vector multiplication
30  // type expected by SDMM
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  SOPT_HIGH_LOG("Read input file {}", input);
53  Image const image = sopt::tools::read_standard_tiff(input);
54 
55  SOPT_HIGH_LOG("Initializing sensing operator");
56  sopt::t_uint const nmeasure = 0.33 * image.size();
57  auto const sampling =
58  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
59 
60  SOPT_HIGH_LOG("Initializing wavelets");
61  auto const wavelet = sopt::wavelets::factory("DB4", 4);
62  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
63 
64  SOPT_HIGH_LOG("Computing sdmm parameters");
65  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
66  auto constexpr snr = 30.0;
67  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
68  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
69 
70  SOPT_HIGH_LOG("Create dirty vector");
71  std::normal_distribution<> gaussian_dist(0, sigma);
72  Vector y(y0.size());
73  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
74  // Write dirty imagte to file
75  if (output != "none") {
76  Vector const dirty = sampling.adjoint() * y;
77  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
78  "dirty_" + output + ".tiff");
79  }
80 
81  SOPT_HIGH_LOG("Initializing convergence function");
82  auto relvar = sopt::RelativeVariation<Scalar>(5e-2);
83  auto convergence = [&y, &sampling, &psi, &relvar](sopt::Vector<Scalar> const &x) -> bool {
84  SOPT_MEDIUM_LOG("||x - y||_2: {}", (y - sampling * x).stableNorm());
85  SOPT_MEDIUM_LOG("||Psi^Tx||_1: {}", sopt::l1_norm(psi.adjoint() * x));
86  SOPT_MEDIUM_LOG("||abs(x) - x||_2: {}", (x.array().abs().matrix() - x).stableNorm());
87  return relvar(x);
88  };
89 
90  SOPT_HIGH_LOG("Creating SDMM Functor");
91  auto const sdmm =
93  .itermax(3000)
94  .gamma(0.1)
95  .conjugate_gradient(200, 1e-8)
96  .is_converged(convergence)
97  // Any number of (proximal g_i, L_i) pairs can be added
98  // ||Psi^dagger x||_1
99  .append(sopt::proximal::l1_norm<Scalar>, psi.adjoint(), psi)
100  // ||y - A x|| < epsilon
102  // x in positive quadrant
103  .append(sopt::proximal::positive_quadrant<Scalar>);
104 
105  SOPT_HIGH_LOG("Allocating result vector");
106  Vector result(image.size());
107  SOPT_HIGH_LOG("Starting SDMM");
108  auto const diagnostic = sdmm(result, Vector::Zero(image.size()));
109  SOPT_HIGH_LOG("SDMM returned {}", diagnostic.good);
110 
111  // diagnostic should tell us the function converged
112  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
113  // diagnostic from the last call to the conjugate gradient.
114  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
115 
116  SOPT_HIGH_LOG("SOPT-SDMM converged in {} iterations", diagnostic.niters);
117  if (output != "none")
118  sopt::utilities::write_tiff(Matrix::Map(result.data(), image.rows(), image.cols()),
119  output + ".tiff");
120 
121  return 0;
122 }
sopt::t_real Scalar
An operator that samples a set of measurements.
Definition: sampling.h:17
bool is_converged(t_Vector const &x) const
Forwards to convergence function parameter.
Definition: sdmm.h:172
SDMM< SCALAR > & conjugate_gradient(t_uint itermax, t_real tolerance)
Helps setup conjugate gradient.
Definition: sdmm.h:83
Proximal for indicator function of L2 ball.
Definition: proximal.h:182
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
#define SOPT_HIGH_LOG(...)
High priority message.
Definition: logging.h:223
#define SOPT_MEDIUM_LOG(...)
Medium priority message.
Definition: logging.h:225
Translation< FUNCTION, VECTOR > translate(FUNCTION const &func, VECTOR const &translation)
Translates given proximal by given vector.
Definition: proximal.h:362
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
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

References sopt::algorithm::SDMM< SCALAR >::conjugate_gradient(), sopt::dirty(), sopt::epsilon(), sopt::wavelets::factory(), sopt::algorithm::SDMM< SCALAR >::is_converged(), sopt::l1_norm(), mersenne(), sopt::tools::read_standard_tiff(), sopt::sigma(), SOPT_HIGH_LOG, SOPT_MEDIUM_LOG, sopt::proximal::translate(), and sopt::utilities::write_tiff().