** 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.
'Deep Learning' 카테고리의 다른 글
[TORCS] End-to-end learning for highway assistance driving system (0) | 2017.09.12 |
---|---|
[GTA5/GTAV] End-to-end learning for autonomous driving (0) | 2017.08.10 |
[tensorflow] how to store/save/read float type numpy array as tfrecord (0) | 2017.08.02 |
[tensorflow] How to read a bmp file and feed it into CNN (0) | 2017.07.18 |
[tensorflow] How to load a mini-batch from tfrecord and feed it to CNN (2) | 2017.07.18 |