Using tensor flow on Android NDK side directly (not using JAVA api)

I am trying to run a neural network on Android in C ++. 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 calls C ++ using JNI functions. Has anyone tried using shadoworflow directly in C ++ on Android? How can I build a tensorflow library and link it to use C ++ apis on Android. Could you direct me to this? I want to use C ++ apis on Android in the same way as in iOS examples.

+5
source share
2 answers

This is how I solved this problem. Although there is not much documentation for using C ++ apis for android and compiling and binding shadoworflow to NDK, there are important comments in the make file, as well as related scripts. The compilation steps are very similar to the ios steps.

  • Install the following dependencies: a) autoconf b) automake c) automake. Then run tensorflow / contrib / makefile / download_dependencies.sh; I first launched the repository on May 10, 2017, when it worked perfectly. In a later version, around June 1, due to some changes in tensorflow / workspace.bzl that I donโ€™t understand in download_dependencies.sh, the tar files downloaded by download_dependencies are not recognized. I just replaced workspace.bzl from May 10th. Repo completes.
  • 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

  • Run make. But first, you may need to make some changes to the Makefile. Replace the -fPIE flags with the -fPIC flags. Also add the -fPIC flag to HOST_CXXFLAGS. Then run make as follows:

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

    Alternatively, you can also run build_all_android.sh, which runs all 3 steps at a time, but you may need to make makefile changes for flags.

The generated file tensorflow / contrib / makefile / gen / protobuf / lib / libprotobuf.a and tensorflow / contrib / makefile / gen / lib / libtensorflow-core.a; This can be associated with the Android NDK project in the Android.mk file in LOCAL_LDLIBS. You need to use these Related flags -Wl,--build-id -Wl,--allow-multiple-definition -Wl,--whole-archive Also -std = C ++ 11 in LOCAL_CFLAGS in the Android.mk file and APP_STL: = gnustl_shared in the Application.mk file.

This should be enough to create a shared library for your NDK project.

+2
source

100% possible, with a little caution ...

Most of the Android user interface runs in Java. You can create native activity , but in order to get any output on the screen, you need to either use OpenGL (which does not have all the nice Android UI Views), or you will need to switch to the JNI barrier to display your data on and from your own code to display to the user.

Depending on your experience with OpenGLES, EGL, etc. You can choose to cross the JNI barrier instead of creating native_activity, but with a much smaller cross section.

You can create a Runnable and pass it when the work is done. Use a parallel queue (in Java) to send work and another (parallel) queue to get results. Runnable issues a work queue, calls one JNI / C function to send the job and return a JSON string. Then he sends the work to the finished queue.

0
source

Source: https://habr.com/ru/post/1268218/


All Articles