How to set input tensor value in C ++?

I am trying to run a sample through a pre-prepared model on ios. session-> Run () takes into account the tensor. I initialized the tensor, but how to set its value? I don't have much experience using C ++.

I have successfully created a test model that accepts a three-dimensional tensor of the form {1, 1, 10}.

I pulled the following line of code from a simple Tensorflow example to create an input tensor.

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ios_examples/simple/RunModelViewController.mm#L189

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,1,10}));

From here I can not figure out how to set input_tensor data. I would like to set the tensor to something like {{.0, .1, .2, .3, .4, .5, .6, .7, .8, .9}}}

+4
source share
2 answers

I had a similar problem and tried to set the input tensor values ​​in C ++ for a model prepared in Python. The model is a simple NN with one hidden layer to learn how to calculate the XOR operation.

First, I created the output graphic file with both the structure of the graph and the model parameters, following the steps 1-4 of this good post: https://medium.com/@hamedmp/exporting-trained-tensorflow-models-to-c- the-right-way-cf24b609d183 # .j4l51ptvb .

Then in C ++ (a simple example of iOS TensorFlow) I used the following code:

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({4,2}));

// input_tensor_mapped is an interface to the data of a tensor and used to copy data into the tensor
auto input_tensor_mapped = input_tensor.tensor<float, 2>();

// set the (4,2) possible input values for XOR
input_tensor_mapped(0, 0) = 0.0;
input_tensor_mapped(0, 1) = 0.0;
input_tensor_mapped(1, 0) = 0.0;
input_tensor_mapped(1, 1) = 1.0;
input_tensor_mapped(2, 0) = 1.0;
input_tensor_mapped(2, 1) = 0.0;
input_tensor_mapped(3, 0) = 1.0;
input_tensor_mapped(3, 1) = 1.0;

tensorflow::Status run_status = session->Run({{input_layer, input_tensor}},
                                             {output_layer}, {}, &outputs);

After that, it GetTopN(output->flat<float>(), kNumResults, kThreshold, &top_results);returns the same 4 values ​​(0.94433498, 0.94425952, 0.06565627, 0.05823805) as in my Python test code for XOR after training the model in top_results.

, {1,1,10}, :

auto input_tensor_mapped = input_tensor.tensor<float, 3>();
input_tensor_mapped(0, 0, 0) = 0.0;
input_tensor_mapped(0, 0, 1) = 0.1;
....
input_tensor_mapped(0, 0, 9) = 0.9;

: Matrix OpenCV Tensorflow ++? .

+3

, , Tensor. flat<T>.

tensor_test

void ExpectClose(const Tensor& x, const Tensor& y, double atol, double rtol) {
  auto Tx = x.flat<T>();
  auto Ty = y.flat<T>();
  for (int i = 0; i < Tx.size(); ++i) {
    if (!IsClose(Tx(i), Ty(i), atol, rtol)) {
      LOG(ERROR) << "x = " << x.DebugString();
      LOG(ERROR) << "y = " << y.DebugString();
      LOG(ERROR) << "atol = " << atol << " rtol = " << rtol
                 << " tol = " << atol + rtol * std::fabs(Tx(i));
      EXPECT_TRUE(false) << i << "-th element is not close " << Tx(i) << " vs. "
                         << Ty(i);
    }
  }
}

,

Tensor(DT_FLOAT, new TensorShape(..))

, Run():

  Status run_status = session->Run({{input_layer, resized_tensor}},
                                   {output_layer}, {}, &outputs);
  if (!run_status.ok()) {
    LOG(ERROR) << "Running model failed: " << run_status;
    return -1;
  }

, Const

tensorflow::ops::Const({input_height, input_width})
+1

Source: https://habr.com/ru/post/1650705/


All Articles