본문 바로가기

Deep Learning

[tensorflow] About using trained NN in C++

반응형

** tensorflow에서 학습한 network를 C++에서 사용 **


In the previous post (visit 2017/07/18 - [Deep Learning] - [tensorflow] how to load and use CNN in C++), I described how to load the CNN trained by using tensorflow and how to use the CNN in C++. In the example, I assumed that the CNN output is of size (1x1). The following example shows how to access the output of CNN whose size is greater than (1x1). Enjoy.



Define input and output tensor

const int batch_size = 1;
const int data_dim = 5;
Tensor tensor(DataType::DT_FLOAT, TensorShape({batch_size, data_dim}));
std::vector<Tensor> outputs;

Input data is passed to the input tensor

tensor_map(0, 0) = 1.0;
tensor_map(0, 1) = 2.0;
tensor_map(0, 2) = 3.0;
tensor_map(0, 3) = 4.0;
tensor_map(0, 4) = 5.0;

Run NN

status = session->Run({{ "input_node", tensor }}, {"output_node"}, {}, &outputs);

Get output value from 'outputs'

1) when 'outputs' is of size (1,1)

auto value = outputs[0].scalar<float>()();

2) when 'outputs' is of size (1,2)

auto num1 = outputs[0].matrix<float>()(0);
auto num2 = outputs[0].matrix<float>()(1);

Assign the output values to float type variables

float output_speed = num1;
float output_steer = num2;


The outputs[0] means the first element (type of tensor) of outputs (of type vector). Since, in the example, the batch size is 1, the number of elements of outputs is 1.