GithubHelp home page GithubHelp logo

tensorflow_cpp_wrapper's Introduction

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:

  1. Build a shared tensorflow library
  2. Build static wrapper library that links to shared tensorflow library

To build tensorflow shared library:

  1. clone tensorflow: https://github.com/tensorflow/tensorflow
  2. checkout the version you want
  3. cd into tensorflow/ root directory
  4. run "./configure"
  5. run "bazel build tensorflow:libtensorflow.so"

To build static wrapper library:

  1. cd into tensorflow_cpp_wrapper/ root folder
  2. set the "tensorflow_path" variable in the CMakeLists.txt to point to the root directory of tensorflow
  3. mkdir build
  4. cd build
  5. cmake..
  6. 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

  1. run xor_network.py to train a simple MLP that learns the xor function
  2. 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.