Detailed explanation of tensorflow lite source code compilation and android side deployment
This article mainly talks about generating libtensorflowlite.jar and libtensorflowlite_jni.so libraries by compiling tensorflow source code
About android deployment attention and code will be introduced in the next blog
1. Download the tensorflow source code
https://github.com/tensorflow/tensorflow
Click clone to download the source zip file and unzip it to your own directory
2. Download the bazel compilation tool (used to compile tensorflow source code)
You can directly refer to the following address to tell you in detail the download and installation process of bazel
https://docs.bazel.build/versions/master/install-ubuntu.html
3. Download android studio
In fact, you can download the android NDK directly, but because we will design the relevant deployment code for android later, android studio
After downloading, all the things needed by android can be downloaded automatically, which is more convenient
The download address is as follows:
https://developer.android.google.cn/studio
After downloading android studio, manually download NDK, CMAKE these things we need in the studio
As follows: check CMake
Check NDK
Then click OK and wait for the download
Remember the NDK directory after the download is complete, you will use it later:
tensorflow lite is fine.
Generally under AndroidSDK/ndk-bundle
3. Compile tensorflow lite source code
The downloaded tensorflow code includes tensorflow, tensorflow lite, and all examples, models, etc., only pay attention to the compilation
tensorflow lite is fine.
Enter the downloaded tensorflow directory and enter the command:
./configure
Then enter your correct python installation directory according to the prompt
You can choose no all the way later, because we only need to compile the java package and so library needed by tensorflow lite, not all tensorflow,
Until you choose to configure the android environment, do the corresponding configuration as follows, select the real android sdk and NDK environment for subsequent successful compilation
The java package and so library of tensorflow lite:
Just select the default android NDK level and SDK level, and finally configure as follows
After the configuration is complete, we can start to compile tensorflow lite related things.
Enter the following command in the tensorflow home directory:
bazel build --cxxopt='--std=c++11' //tensorflow/lite/java:tensorflowlite \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=armeabi-v7a
Explained here, --cxxopt='--std-c++11' means that c++11 is supported, otherwise an error will be reported
--cpu=armeabi-v7a, you can also fill in the following options, you need to decide according to your own cpu, generally it is armeabi-v7a, which can support Huawei, Xiaomi
Qualcomm processors, Kirin processors, and processors based on ARM cores are the vast majority on the market
Compilation is completed as shown below:
The generated JAR package and SO library are placed in the following directories, each person will have a difference:
/home/qinchao/code/tensorflow-master/bazel-bin/tensorflow/lite/java
At this point, the tensorflow lite compilation library has been completed, just take out libtensorflowlite.jar and libtensorflowlite_jni.so!
Original link: https://blog.csdn.net/qinchao315/article/details/88780845