how to use tensorflow in C++

Published: 27 May 2018 Category: artificial intelligence

Recently, as some of the reason, I have a look at of Mask RCNN which is based on keras. With some research on net, I find some scripts that could export the keras model file(.h5) to tensorflow model file(.pb), and a script that could load the converted tensorflow model and run it in tersoflow framework but this script need a little modification for the Mask RCNN 2.0 Relase. Now I have create a repository with all the script integrated, here it is https://github.com/parai/Mask_RCNN.

But this doesn’t satisfy me, I now know how to load and run the converted Mask RCNN model in python, but how to load and run it in C++? This is what I really what to know as I am an application engineer, I am not a reseacher about what a machine learning model is and how it works. What I want to know is how to retrain a model and deploy it on devices such as PC or Phone.

But first of all, I want to know how to load and run it in C++.

1. Env Prepare

1.1 Build out c++ library

git clone https://github.com/tensorflow/tensorflow
cd tensorflow
./configure
bazel build --config=opt //tensorflow:libtensorflow_cc.so

cd tensorflow/contrib/makefile
chmod +x *.sh
./build_all_linux.sh

# below file is the base tensorflow library for C++ applicaiton
parai@UX303LB:~/workspace/tensorflow/tensorflow/bazel-bin/tensorflow$ ls -l libtensorflow*.so
-r-xr-xr-x 1 parai parai 123880984 5月  27 19:20 libtensorflow_cc.so

1.2 verify that C++ library

1.2.1 build the cc example and run

# build the demo tensorflow/cc/tutorials/example_trainer
bazel build --config=opt //tensorflow/cc:tutorials_example_trainer

Reference

1.loading tensorflow graph with c api

comments powered by Disqus