How to build & deploy a TensorFlow app for Android

The following instructions were copied from the Justin Francis post found here. Several additions and changes were made, as needed.

Dev Setup: Download Android SDK & NDK

You can download Android SDK using the terminal and then extract it into your TensorFlow directory.

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
$ tar xvzf android-sdk_r24.4.1-linux.tgz -C ~/tensorflow

Then we need to download some additional build tools for the SDK; I closed the terminal after it started downloading for Android 5.1.1.

$ cd ~/tensorflow/android-sdk-linux
$ tools/android update sdk --no-ui

Next you can download the Android NDK (native development kit) and extract using:

$ wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip
$ unzip android-ndk-r12b-linux-x86_64.zip -d ~/tensorflow

Install the python mock tool:

$ pip install mock

Download the TensorFlow Inception model

“Inception” is google’s Tensorflow model trained using photos found on ImageNet

$ cd ~/tensorflow
$ wget https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -O /tmp/inception5h.zip
$ unzip /tmp/inception5h.zip -d tensorflow/examples/android/assets/

Modify the tensorflow WORKSPACE File

In order to build our app using Android tools, we need to modify the workspace file.

$ gedit ~/tensorflow/WORKSPACE

Copy the code below and overwrite the similar lines found in the WORKSPACE file.

android_sdk_repository(
            name = "androidsdk",
            api_level = 24,
            build_tools_version = "24.0.3",
            path = "android-sdk-linux")

android_ndk_repository(
            name="androidndk",
            path="android-ndk-r12b",
            api_level=21)

Configure tensorflow

$ cd ~/tensorflow
$ ./configure             (this will launch and prompt for entries)

Build the APK for your Android device

$ cd ~/tensorflow
$ bazel build //tensorflow/examples/android:tensorflow_demo

Set up your Android Device

Follow the instructions at this link to enable USB debugging on your android device.

Now install the APK to your Android device

This is the only step I feel like I could not objectively test.  Every android device is different; I suggest upgrading your device to Android 6.0 if you are having any issues. On my friend’s Moto G, I had to remove the -g from this command:

$ cd ~/tensorflow
$ adb install -r -g bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

The command above installs 3 apps on your Android device (TF Classify, TF Detect, and TF Sylize). You can now run TensorFlow and the Inception classifier (TF Classify) on your android device. I find the best part is the humorous classifications it sometimes gets wrong.  Keep in mind the Inception classifier only knows 1,000 images used from the Imagenet challenge.


Create and use a custom classifier

To use a custom graph from our own classifier, we have to optimize the graph file for mobile use and put it into the assets directory.

First we must build the graph optimizer tool:

$ cd ~/tensorflow 
$ bazel build tensorflow/python/tools:optimize_for_inference

Then we run the optimizer tool on the graph:

$ bazel-bin/tensorflow/python/tools/optimize_for_inference \
--input=tf_files/retrained_graph.pb \
--output=tf_files/optimized_graph.pb \
--input_names=Mul \
--output_names=final_result

Test the optimized model

$ cd ~/tensorflow
$ bazel-bin/tensorflow/examples/label_image/label_image \
--output_layer=final_result
--labels=/tf_files/retrained_labels.txt
--image=/tf_files/flower_photos/daisy/9350942387_5b1d043c26_n.jpg
--graph=/tf_files/optimized_graph.pb

This should predict daisy as the highest probability.

Round the graph weights (this produces a smaller model when compressed/packaged)

$ cd ~/tensorflow
$ bazel-bin/tensorflow/tools/quantization/quantize_graph \
--input=tf_files/optimized_graph.pb \
--output=tf_files/rounded_graph.pb \
--output_node_names=final_result \
--mode=weights_rounded

Test the rounded model

$ cd ~/tensorflow
$ bazel-bin/tensorflow/examples/label_image/label_image \
--output_layer=final_result
--labels=/tf_files/retrained_labels.txt
--image=/tf_files/flower_photos/daisy/9350942387_5b1d043c26_n.jpg
--graph=/tf_files/rounded_graph.pb

This should predict daisy as the highest probability.

Copy the graph and labels into the android/assets folder

$ cd ~/tensorflow
$ cp tf_files/optimized_graph.pb   tensorflow/examples/android/assets/
$ cp tf_files/retrained_labels.txt tensorflow/examples/android/assets/

Edit ClassifierActivity.java

$ cd ~/tensorflow
$ subl tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageListener.java
$ subl tensorflow/examples/android/src/org/tensorflow/demo/ClassifierActivity.java

We need to edit this file to use our custom graph: replace the following lines with these variables and save (if you want to revert back to the older file you can find a copy here):

private static final int INPUT_SIZE = 299;
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128;
private static final String INPUT_NAME = "Mul:0";
private static final String OUTPUT_NAME = "final_result:0";

private static final String MODEL_FILE = "file:///android_asset/rounded_graph.pb";
private static final String LABEL_FILE = "file:///android_asset/retrained_labels.txt";

Rebuild the APK for your Android device

$ cd ~/tensorflow
$ bazel build //tensorflow/examples/android:tensorflow_demo

Install the APK to your Android device

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

And there we have it! You can now use your own custom classifier on your Android Device.  There are endless ideas that researchers and hobbyists alike could experiment with using this technology.  One idea I had was to try to learn how to classify nutrient deficiencies or other botanical ailments in plants by classifying images of unhealthy leaves. If you would like to learn how to further compress the graph file and how use a classifier on iPhone, you can follow Pete Warden’s tutorial.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: