Running trained tensorflow model in C++

You could use the C++ API as shown in the previous answer, nevertheless, compiling with the TensorFlow C++ API can be a headache. I recommend you using cppflow which a simple and easy to use wrapper to de C API. It allows you to feed data to the network as C++ vectors:

Model m("mymodel.pb");
m.restore("./my_test_model");

auto X = new Tensor(m, "X");
auto y = new Tensor(m, "y");
auto keep_prob = new Tensor(m, "keep_prob");
auto result = new Tensor(m, "prediction");

std::vector<float> xdata, ydata;
// Fill the vectors with data
X->set_data(xdata);
y->set_data(ydata);

m.run({X,y,keep_prob}, result);

std::vector<float> myresult = result->get_data<float>();

You can use this wrapper without installing full TensorFlow, you just need to download the .so of the C API.


Instructions for using a graph in C++ can be found here.

Here is some code to use your image as input:

tensorflow::Tensor keep_prob = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape());
keep_prob.scalar<float>()() = 1.0;

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,height,width,depth}));
auto input_tensor_mapped = input_tensor.tensor<float, 4>();
const float * source_data = (float*) img.data;  // here img is an opencv image, but if it's just a float array this code is very easy to adapt
// copying the image data into the corresponding tensor
for (int y = 0; y < height; ++y) {
    const float* source_row = source_data + (y * width * depth);
    for (int x = 0; x < width; ++x) {
        const float* source_pixel = source_row + (x * depth);
        for (int c = 0; c < depth; ++c) {
            const float* source_value = source_pixel + c;
            input_tensor_mapped(0, y, x, c) = *source_value;
        }
    }
}
std::vector<tensorflow::Tensor> finalOutput;

tensorflow::Status run_status = this->tf_session->Run({{InputName,input_tensor}, 
                                                       {dropoutPlaceHolderName, keep_prob}},
                                                      {OutputName},
                                                      {},
                                                      &finalOutput);

Tags:

C++

Tensorflow