SOPT
Sparse OPTimisation
Functions | Variables
sopt::cppflowutils Namespace Reference

Functions

cppflow::tensor convert_image_to_tensor (sopt::Image< double > const &image, int image_rows, int image_cols)
 Converts a sopt::Image to a cppflow::tensor. More...
 
cppflow::tensor convert_image_to_tensor (Image< std::complex< double >> const &image, int image_rows, int image_cols)
 
cppflow::tensor convert_image_to_tensor (sopt::Vector< double > const &image, int image_rows, int image_cols)
 Converts a sopt::Vector to a cppflow::tensor. More...
 
cppflow::tensor convert_image_to_tensor (sopt::Vector< std::complex< double >> const &image, int image_rows, int image_cols)
 
Eigen::Map< Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > > convert_tensor_to_image (std::vector< float > model_output, int image_rows, int image_cols)
 Convert a cppflow:tensor to an Eigen::Array. More...
 

Variables

constexpr double imaginary_threshold = 1e-10
 

Function Documentation

◆ convert_image_to_tensor() [1/4]

cppflow::tensor sopt::cppflowutils::convert_image_to_tensor ( Image< std::complex< double >> const &  image,
int  image_rows,
int  image_cols 
)

Definition at line 33 of file cppflow_utils.cc.

33  {
34  // Convert the Sopt::Image of complex (wrapper for Eigen::Array) to a cppflow::tensor of floats
35  // Only takes the real part for processing, on the assumption that the imaginary part is negligible.
36  // If ratio of imaginary part to real part is greater than the threshold then this will throw an exception.
37  // TODO: Make types template parameters
38  // create a vector of the right shape (model expects extra dimensions on start and end)
39  std::vector<int64_t> input_shape = {1, image_rows, image_cols, 1};
40 
41  std::vector<float> input_values(image_rows*image_cols, 1);
42 
43  for (int i = 0; i < image.rows(); ++i) {
44  for (int j = 0; j < image.cols(); ++j) {
45  if(std::abs(image(i,j).real()) > cppflowutils::imaginary_threshold)
46  {
47  throw std::runtime_error("Cannot convert to tensorflow format: imaginary component of image is non-negligible.");
48  }
49  input_values[j*image_cols+i] = image(i,j).real();
50  }
51  }
52 
53  // create a tensor from vector
54  cppflow::tensor input_tensor(input_values, input_shape);
55  return input_tensor;
56  }
constexpr double imaginary_threshold

References imaginary_threshold.

◆ convert_image_to_tensor() [2/4]

cppflow::tensor sopt::cppflowutils::convert_image_to_tensor ( Image< double > const &  image,
int  image_rows,
int  image_cols 
)

Converts a sopt::Image to a cppflow::tensor.

Definition at line 14 of file cppflow_utils.cc.

14  {
15  // Convert the Sopt::Image of doubles(wrapper for Eigen::Array) to a cppflow::tensor of floats
16  // TODO: Make types template parameters
17  // create a vector of the right shape (model expects extra dimensions on start and end)
18  std::vector<int64_t> input_shape = {1, image_rows, image_cols, 1};
19 
20  std::vector<float> input_values(image_rows*image_cols, 1);
21  for (int i = 0; i < image.rows(); ++i) {
22  for (int j = 0; j < image.cols(); ++j) {
23  input_values[j*image_cols+i] = image(i,j);
24  }
25  }
26 
27  // create a tensor from vector
28  cppflow::tensor input_tensor(input_values, input_shape);
29 
30  return input_tensor;
31  }

◆ convert_image_to_tensor() [3/4]

cppflow::tensor sopt::cppflowutils::convert_image_to_tensor ( sopt::Vector< double > const &  image,
int  image_rows,
int  image_cols 
)

Converts a sopt::Vector to a cppflow::tensor.

Definition at line 59 of file cppflow_utils.cc.

59  {
60 
61  std::vector<float> input_values(&image[0], image.data()+image.size());
62  cppflow::tensor input_tensor(input_values, {1, image_rows, image_cols, 1});
63 
64  return input_tensor;
65 
66  }

◆ convert_image_to_tensor() [4/4]

cppflow::tensor sopt::cppflowutils::convert_image_to_tensor ( sopt::Vector< std::complex< double >> const &  image,
int  image_rows,
int  image_cols 
)

Definition at line 69 of file cppflow_utils.cc.

69  {
70 
71  std::vector<float> input_values(image_rows);
72  for(int i = 0; i < image_rows; i++)
73  {
74  if(std::abs(image(i).real()) > cppflowutils::imaginary_threshold)
75  {
76  throw std::runtime_error("Cannot conver to tensorflow format: imaginary component of image vector is non negligible.");
77  }
78  input_values[i] = image(i).real();
79  }
80  cppflow::tensor input_tensor(input_values, {1, image_rows, image_cols, 1});
81 
82  return input_tensor;
83  }

References imaginary_threshold.

◆ convert_tensor_to_image()

Eigen::Map< Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > > sopt::cppflowutils::convert_tensor_to_image ( std::vector< float >  model_output,
int  image_rows,
int  image_cols 
)

Convert a cppflow:tensor to an Eigen::Array.

Definition at line 86 of file cppflow_utils.cc.

86  {
87  // convert tensor of floats to Eigen::Array of doubles
88 
89  std::vector<double> doubleResults(model_output.begin(), model_output.end());
90  Eigen::Map<Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>> output_image(doubleResults.data(), image_rows, image_cols);
91 
92  return output_image;
93  }

Variable Documentation

◆ imaginary_threshold

constexpr double sopt::cppflowutils::imaginary_threshold = 1e-10
constexpr

Definition at line 12 of file cppflow_utils.cc.

Referenced by convert_image_to_tensor().