SOPT
Sparse OPTimisation
Functions
reweighted.cc File Reference
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include "sopt/imaging_padmm.h"
#include "sopt/logging.h"
#include "sopt/maths.h"
#include "sopt/positive_quadrant.h"
#include "sopt/relative_variation.h"
#include "sopt/reweighted.h"
#include "sopt/sampling.h"
#include "sopt/types.h"
#include "sopt/utilities.h"
#include "sopt/wavelets.h"
#include "sopt/wavelets/sara.h"
#include "tools_for_tests/directories.h"
#include "tools_for_tests/tiffwrappers.h"
+ Include dependency graph for reweighted.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 26 of file reweighted.cc.

26  {
27  // Some type aliases for simplicity
28  using Scalar = double;
29  // Column vector - linear algebra - A * x is a matrix-vector multiplication
30  // type expected by ProximalADMM
32  // Matrix - linear algebra - A * x is a matrix-vector multiplication
33  // type expected by ProximalADMM
35  // Image - 2D array - A * x is a coefficient-wise multiplication
36  // Type expected by wavelets and image write/read functions
37  using Image = sopt::Image<Scalar>;
38 
39  std::string const input = argc >= 2 ? argv[1] : "cameraman256";
40  std::string const output = argc == 3 ? argv[2] : "none";
41  if (argc > 3) {
42  std::cout << "Usage:\n"
43  "$ "
44  << argv[0]
45  << " [input [output]]\n\n"
46  "- input: path to the image to clean (or name of standard SOPT image)\n"
47  "- output: filename pattern for output image\n";
48  exit(0);
49  }
50  // Set up random numbers for C and C++
51  auto const seed = std::time(nullptr);
52  std::srand(static_cast<unsigned int>(seed));
53  std::mt19937 mersenne(std::time(nullptr));
54 
55  SOPT_MEDIUM_LOG("Read input file {}", input);
56  Image const image = sopt::tools::read_standard_tiff(input);
57 
58  SOPT_MEDIUM_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_MEDIUM_LOG("Initializing wavelets");
64  sopt::wavelets::SARA const sara{std::make_tuple(std::string{"DB3"}, 1u),
65  std::make_tuple(std::string{"DB1"}, 2u),
66  std::make_tuple(std::string{"DB1"}, 3u)};
67  auto const psi = sopt::linear_transform<Scalar>(sara, image.rows(), image.cols());
68 
69  SOPT_MEDIUM_LOG("Computing proximal-ADMM parameters");
70  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
71  auto constexpr snr = 30.0;
72  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
73  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
74 
75  SOPT_MEDIUM_LOG("Create dirty vector");
76  std::normal_distribution<> gaussian_dist(0, sigma);
77  Vector y(y0.size());
78  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
79  // Write dirty imagte to file
80  if (output != "none") {
81  Vector const dirty = sampling.adjoint() * y;
82  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
83  "dirty_" + output + ".tiff");
84  }
85 
86  SOPT_MEDIUM_LOG("Creating proximal-ADMM Functor");
88  .itermax(500)
89  .regulariser_strength(1e-1)
90  .relative_variation(5e-4)
91  .l2ball_proximal_epsilon(epsilon)
92  .tight_frame(false)
93  .l1_proximal_tolerance(1e-2)
94  .l1_proximal_nu(1)
95  .l1_proximal_itermax(50)
96  .l1_proximal_positivity_constraint(true)
97  .l1_proximal_real_constraint(true)
99  .lagrange_update_scale(0.9)
100  .Psi(psi)
101  .Phi(sampling);
102 
103  SOPT_MEDIUM_LOG("Creating the reweighted algorithm");
104  // positive_quadrant projects the result of PADMM on the positive quadrant.
105  // This follows the reweighted algorithm for SDMM
106  auto const min_delta = sigma * std::sqrt(y.size()) / std::sqrt(8 * image.size());
107  auto const reweighted =
108  sopt::algorithm::reweighted(padmm).itermax(5).min_delta(min_delta).is_converged(
110 
111  SOPT_MEDIUM_LOG("Starting proximal-ADMM");
112  // Alternatively, padmm can be called with a tuple (x, residual) as argument
113  // Here, we default to (Φ^Ty/ν, ΦΦ^Ty/ν - y)
114  auto const diagnostic = reweighted();
115  SOPT_MEDIUM_LOG("proximal-ADMM returned {}", diagnostic.good);
116 
117  SOPT_MEDIUM_LOG("SOPT-proximal-ADMM converged in {} iterations", diagnostic.niters);
118  if (output != "none")
119  sopt::utilities::write_tiff(Matrix::Map(diagnostic.algo.x.data(), image.rows(), image.cols()),
120  output + ".tiff");
121 
122  return 0;
123 }
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.
Sparsity Averaging Reweighted Analysis.
Definition: sara.h:20
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
#define SOPT_MEDIUM_LOG(...)
Medium priority message.
Definition: logging.h:225
Reweighted< ALGORITHM > reweighted(ALGORITHM const &algo, typename Reweighted< ALGORITHM >::t_SetWeights const &set_weights, typename Reweighted< ALGORITHM >::t_Reweightee const &reweightee)
Factory function to create an l0-approximation by reweighting an l1 norm.
Definition: reweighted.h:238
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
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

References sopt::dirty(), sopt::epsilon(), mersenne(), sopt::tools::read_standard_tiff(), sopt::algorithm::ImagingProximalADMM< SCALAR >::residual_convergence(), sopt::algorithm::reweighted(), sopt::sigma(), SOPT_MEDIUM_LOG, and sopt::utilities::write_tiff().