SOPT
Sparse OPTimisation
Typedefs | Functions
tf_inpainting.cc File Reference
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include <catch2/catch_all.hpp>
#include "sopt/imaging_forward_backward.h"
#include "sopt/tf_non_diff_function.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 "tools_for_tests/directories.h"
#include "tools_for_tests/tiffwrappers.h"
+ Include dependency graph for tf_inpainting.cc:

Go to the source code of this file.

Typedefs

using Scalar = double
 
using Vector = sopt::Vector< Scalar >
 
using Matrix = sopt::Matrix< Scalar >
 
using Image = sopt::Image< Scalar >
 

Functions

 TEST_CASE ("Inpainting")
 

Typedef Documentation

◆ Image

Definition at line 29 of file tf_inpainting.cc.

◆ Matrix

Definition at line 28 of file tf_inpainting.cc.

◆ Scalar

using Scalar = double

Definition at line 26 of file tf_inpainting.cc.

◆ Vector

Definition at line 27 of file tf_inpainting.cc.

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "Inpainting"  )

Definition at line 31 of file tf_inpainting.cc.

31  {
32  extern std::unique_ptr<std::mt19937_64> mersenne;
33  const std::string input = "cameraman256";
34 
35  const std::string model_path = std::string(sopt::tools::models_directory() + "/snr_15_model_dynamic.onnx");
36 
37  const Image image = sopt::tools::read_standard_tiff(input);
38 
39  sopt::t_uint nmeasure = std::floor(0.5 * image.size());
40  sopt::LinearTransform<Vector> const sampling =
41  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, *mersenne));
42 
43  const Vector y0 = sampling * Vector::Map(image.data(), image.size());
44  constexpr auto snr = 30.0;
45  const auto sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
46  const auto epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
47 
48  std::normal_distribution<> gaussian_dist(0, sigma);
49  Vector y(y0.size());
50  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(*mersenne);
51 
52  Eigen::VectorXd dirty_image = sampling.adjoint() * y;
53 
54  sopt::t_real constexpr regulariser_strength = 100;
55  sopt::t_real const beta = sigma * sigma * 0.5;
56 
58  fb.itermax(500)
59  .step_size(beta) // stepsize
60  .sigma(sigma) // sigma
61  .regulariser_strength(regulariser_strength) // regularisation paramater
62  .fista(false) // switch to use FISTA algorithm in Forward Backward algorithm, should be false if using learned TF model
63  .relative_variation(1e-3)
64  .residual_tolerance(0)
65  .tight_frame(true)
66  .Phi(sampling);
67 
68  // Create a shared pointer to an instance of the TFGProximal class
69  auto gp = std::make_shared<sopt::algorithm::TFGProximal<Scalar>>(model_path);
70 
71  // Inject it into the ImagingForwardBackward object
72  fb.g_function(gp);
73 
74  auto const diagnostic = fb();
75 
76  CHECK(diagnostic.good);
77  CHECK(diagnostic.niters < 500);
78 
79  // compare input image to cleaned output image
80  // calculate mean squared error sum_i ( ( x_true(i) - x_est(i) ) **2 )
81  // check this is less than the number of pixels * 0.01
82  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
83  "tf_reconstruction.tiff");
84  sopt::utilities::write_tiff(Matrix::Map(dirty_image.data(), image.rows(), image.cols()),
85  "tf_dirty.tiff");
86  Eigen::Map<const Eigen::VectorXd> flat_image(image.data(), image.size());
87  auto mse = (flat_image - diagnostic.x).array().square().sum() / image.size();
88  CAPTURE(mse);
89  SOPT_HIGH_LOG("MSE: {}", mse);
90  CHECK(mse < 0.01);
91 }
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))
#define SOPT_HIGH_LOG(...)
High priority message.
Definition: logging.h:223
Image read_standard_tiff(std::string const &name)
Reads tiff image from sopt data directory if it exists.
Definition: tiffwrappers.cc:9
std::string models_directory()
Machine-learning models.
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
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
real_type< T >::type epsilon(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:38
real_type< T >::type sigma(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:17
sopt::Vector< Scalar > Vector
Definition: inpainting.cc:28
sopt::Image< Scalar > Image
Definition: inpainting.cc:30

References sopt::LinearTransform< VECTOR >::adjoint(), sopt::epsilon(), mersenne(), sopt::tools::models_directory(), sopt::tools::read_standard_tiff(), sopt::sigma(), SOPT_HIGH_LOG, and sopt::utilities::write_tiff().