A C++ wrapper for the Tensorflow C API
This will allow you to take a network trained in python and run it in a c++ program. This wraps the Tensorflow C API in a class (which is supposedly more stable?).
To use this, there are two steps:
- Build a shared tensorflow library
- Build static wrapper library that links to shared tensorflow library
To build tensorflow shared library:
- clone tensorflow: https://github.com/tensorflow/tensorflow
- checkout the version you want
- cd into tensorflow/ root directory
- run "./configure"
- run "bazel build tensorflow:libtensorflow.so"
To build static wrapper library:
- cd into tensorflow_cpp_wrapper/ root folder
- set the "tensorflow_path" variable in the CMakeLists.txt to point to the root directory of tensorflow
- mkdir build
- cd build
- cmake..
- make
================================================
Now you can link against the static wrapper library to create c++ code that can load saved binary protobuf tensorflow models. (you may also have to link your c++ code with the shared tensorflow library)
This wrapper only works with tensorflow models that have constant variables (it won't restore a checkpoint). Use tf.graph_util.convert_variables_to_constants() and tf.train.write_graph() to save tensorflow models in python.
See test.cpp for example usage.
You can test if the wrapper is working by going to the main tensorflow_cpp_wrapper/ folder and
- run xor_network.py to train a simple MLP that learns the xor function
- run "build/test_exe"
================================================
some of this code is based off of: https://stackoverflow.com/questions/44305647/segmentation-fault-when-using-tf-sessionrun-to-run-tensorflow-graph-in-c-not-c