2020年6月20日 星期六

Ubuntu Bazel build tensorflow lite c++ api

由於開始研究如何使用 TensorFlow Lite NNAPI delegate,看了 google 的 sample code 發現好像只有 JAVA 的 sample code 與 demo。

而  c++ 的有 sample code,但所需的 libtensorflowlite.so 似乎需要自己build。
因此,在自己嘗試 build 的過程中順帶記錄了一下。


建置 building 環境

安裝 Openjdk-8

$ sudo apt-get install openjdk-8-jre
查看是否安裝完成 
$ java — version
若顯示如下圖,則表示安裝完成,若與下圖不同,則需跳至下一步驟,設定切換 java 版本



切換 java 版本


下指令後,選取想要的版本。

安裝 Bazel

Step 1: Add Bazel distribution URI as a package source
Note: This is a one-time setup step.
$ sudo apt install curl
Step 2: Install and update Bazel
$ sudo apt update && sudo apt install bazel
Once installed, you can upgrade to a newer version of Bazel as part of your normal system updates:
$ sudo apt update && sudo apt full-upgrade
The bazel package will always install the latest stable version of Bazel. You can install specific, older versions of Bazel in addition to the latest one like this:
$ sudo apt install bazel-1.0.0
This will install Bazel 1.0.0 as /usr/bin/bazel-1.0.0 on your system. This can be useful if you need a specific version of Bazel to build a project, e.g. because it uses a .bazelversion file to explicitly state with which Bazel version it should be built.

下載 Android SDK

(總共要下載3包)
  1. 進入頁面 (https://developer.android.com/studio) 下載如下圖標黃的東西

2. 進入頁面 (https://developer.android.com/studio/releases/platform-tools) 下載如下圖標黃的東西


3. 進入頁面 (https://developer.android.com/studio) 下載如下圖標黃的東西

安裝 Android Sdk

以下檔案請依照實際的壓縮包檔名做修改。
$ tar zxf android-studio-ide-192.6241897-linux.tar.gz
$ unzip platform-tools_r30.0.0-linux.zip
$ unzip sdk-tools-linux-4333796.zip
$ mv platform-tools android-studio
$ mv tools android-studio
$ cd android-studio/tools/bin
$ ./sdkmanager “platform-tools” “platforms;android-26”
$ ./sdkmanager “build-tools;26.0.0”
Note. 可先下 “./sdkmanager — list” 去看想要的 tool 包

下載 與 解壓 Android NDK (linux version)

Git clone tensorflow Repository

Build tensorflow lite C++ API

$ ~/tensorflow$ mv .bazelrc ~
$ ~/tensorflow$ mv .bazelversion ~
NOTE. 若是在虛擬環境建立 python3 環境,要先進入虛擬還境中。
Configure WORKSPACE and .bazelrc
$ cd tensorflow
$ ./configure
照著問題一步一步設定回答,”./WORKSPACE” 那個要選 “y”

全部設置完成後,進入 tensorflow 資料夾
32bit armeabi-v7a:
$ bazel build — config=android_arm //tensorflow/lite:libtensorflowlite.so -c opt
64bit arm64-v8a:
$ bazel build — config=android_arm64 //tensorflow/lite:libtensorflowlite.so -c opt


沒有留言:

張貼留言