Going deeper - Building with Bazel

This the time for doing a concrete example from the beginning till having the .apk files up and running on your device. We are going to follow the Bazel approach for that.

First, you need to clone tensorflow repository, by issuing the following command:

git clone --recurse-submodules   https://github.com/tensorflow/tensorflow.git 
The recurse submodules is needed in order to prevent some issues during compilation.

Next, you need to install Bazel(Which is the underlying build system that TensorFlow uses to build their programs, https://bazel.build/) and Android prerequisites:

  1. Install the latest version of Bazel by following the instructions mentioned in this URL: https://bazel.build/versions/master/docs/install.html
  2. To build the C++ part, Android NDK is required. The recommended version for it is 12b and you can download it from the following URL: https://developer.android.com/ndk/downloads/older_releases.html#ndk-12b-downloads
  3. Also, you need to install the Android SDK and build tools which you can get from this (https://developer.android.com/studio/releases/build-tools.html.) You can also use it as part of Android Studio ( https://developer.android.com/studio/index.html ). Build tools API >= 23 is required to build the TF Android demo (though it will run on API >= 21 devices).

As mentioned above you need to provide information about the SDK and NDK by editing the WORKSPACE (you can find it under the root folder of TensorFlow) files and you need to uncomment the following lines and update the paths accordingly:

# Uncomment and update the paths in these entries to build the Android demo.
#android_sdk_repository(
# name = "androidsdk",
# api_level = 23,
# # Ensure that you have the build_tools_version below installed in the
# # SDK manager as it updates periodically.
# build_tools_version = "25.0.2",
# # Replace with path to Android SDK on your system
# path = "<PATH_TO_SDK>",
#)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
#android_ndk_repository(
# name="androidndk",
# path="<PATH_TO_NDK>",
# # This needs to be 14 or higher to compile TensorFlow.
# # Note that the NDK version is not the API level.
# api_level=14)

If you did not uncomment the previous lines in the workspace an error such as: "The external label //external: android/sdk is not bound to anything" will be reported.

Also, you need to edit the API levels for the SDK in the WORKSPACE file to the highest level installed in your SDK. As mentioned this must be >=23 and the NDK API level may remain at 14.

Then you need to build the APK by issuing the following command from the workspace root:

bazel build -c opt //tensorflow/examples/android:tensorflow_demo

As mentioned by TensorFlow if you get build errors about protocol buffers, run git submodule update--init and make sure that you've modified your workspace file as instructed, then try building again.

Finally you can install the .apk file but first, make sure that adb debugging is enabled on your Android 5.0 (API 21) or later device. Issue the following command from your workspace root in order to install the .apk file:

adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk  

Now you can enjoy running deep learning algorithms on your Android mobile device by using TensorFlow as a platform.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset