Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using tensorflow on Android NDK side directly (Not using JAVA api)

I am trying to run a neural network on Android in C++. The examples (https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android) show how to use tensorflow using JAVA apis which call C++ using JNI functions. Has anyone tried to use tensorflow directly in C++ on Android? How can the tensorflow library be built and linked for using C++ apis on Android. Can you please guide me on that? I want to use C++ apis on Android in the similar way as done in iOS examples.

like image 447
Ruppesh Nalwaya Avatar asked May 25 '17 08:05

Ruppesh Nalwaya


1 Answers

Here is how I solved this problem. Although there is not much documentation of using c++ apis on android and compiling and linking tensorflow to NDK, the makefile have important comments as well as there are associated scripts. Compilation steps are very similar to that of ios.

  1. Install following dependencies a)autoconf b) automake c)automake. Then run tensorflow/contrib/makefile/download_dependencies.sh; I ran on May 10, 2017 repository for the first time, when it worked perfectly. In the later version around June 1, due to some changes in tensorflow/workspace.bzl, that I do not understand in download_dependencies.sh fails to recognise the tar files download_dependencies is trying to download. I just replaced workspace.bzl from May 10 repo commit.
  2. Step 2 is to run tensorflow/contrib/makefile/compile_android_protobuf.sh like this

    NDK_ROOT=absolute/path/to/ndk/folder ./tensorflow/contrib/makefile/compile_android_protobuf.sh

  3. Run make. But first you may need to make some changes in Makefile. Replace -fPIE flags with -fPIC flags. Also add -fPIC flag to HOST_CXXFLAGS. Then run make like this:

    make -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID NDK_ROOT=absolute/path/to/ndk/folder

    Alternatively one can also run build_all_android.sh which runs all 3 steps in one go, but you may need to do Makefile changes for flags.

This generated tensorflow/contrib/makefile/gen/protobuf/lib/libprotobuf.a and tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a; This can be linked to Android NDK project in Android.mk file under LOCAL_LDLIBS. One should use these Linked flags -Wl,--build-id -Wl,--allow-multiple-definition -Wl,--whole-archive Also -std=c++11 in LOCAL_CFLAGS in Android.mk file and APP_STL := gnustl_shared in Application.mk file.

This should be sufficient to build a shared library of your NDK project.

like image 86
Ruppesh Nalwaya Avatar answered Oct 25 '22 22:10

Ruppesh Nalwaya