SOPT
Sparse OPTimisation
cppflow_utils.cc
Go to the documentation of this file.
1 #include <Eigen/Core>
2 #include <vector>
3 #include "sopt/types.h"
4 #include <cppflow/cppflow.h>
5 #include "cppflow/ops.h"
6 #include "cppflow/model.h"
7 #include <stdexcept>
8 
9 namespace sopt::cppflowutils {
10 
11  // arbitrary constant for imaginary part of image vectors.
12  constexpr double imaginary_threshold = 1e-10;
13 
14  cppflow::tensor convert_image_to_tensor(Image<double> const &image, int image_rows, int image_cols){
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  }
32 
33  cppflow::tensor convert_image_to_tensor(Image<std::complex<double>> const &image, int image_rows, int image_cols) {
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  }
57 
58  // Convert an image stored in a sopt::Vector<double> to a cppflow::tensor of floats
59  cppflow::tensor convert_image_to_tensor(sopt::Vector<double> const &image, int image_rows, int image_cols) {
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  }
67 
68  // Convert an image stored in a sopt::Vector<double> to a cppflow::tensor of floats
69  cppflow::tensor convert_image_to_tensor(sopt::Vector<std::complex<double>> const &image, int image_rows, int image_cols) {
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  }
84 
85  // Convert model output in a std::vector into a sopt::Image (2D Eigen::Array)
86  Eigen::Map<Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>> convert_tensor_to_image(std::vector<float> model_output, int image_rows, int image_cols){
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  }
94 
95 } // namespace sopt::cppflowutils
constexpr double imaginary_threshold
cppflow::tensor convert_image_to_tensor(Image< double > const &image, int image_rows, int image_cols)
Converts a sopt::Image to a cppflow::tensor.
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.
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Definition: types.h:39
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24