Experimenting with TensorFlow on Android Part 1

Mostafa Gazar
3 min readOct 3, 2016

--

Update: Checkout my new post for more up to date steps https://medium.com/@mgazar/running-a-tensorflow-model-on-ios-and-android-ce89446c8143

Update: Some stuff here might not be up to date, this post was written in the era of TensorFlow version 0.10.0rc

The following assumes good Android knowledge and no TensorFlow experience.

I had a bunch of random goals when I started:

  • Try Bazel vs. Gradle, I came across some articles on how Bazel is faster and that some few big names have switched completely to Bazel or started experimenting with it beside their gradle.build file.
  • Run TensorFlow demo as is and check how accurate it can be in the real world.
  • Modify the demo app so it can pick up images from the device, for quick experimentation.
  • Retrain Inception model on some dataset and replace the attached model in the demo app.

Let us start by installing TensorFlow, I tried a couple methods of installing it and ended up using docker

docker run -it gcr.io/tensorflow/tensorflow:latest-devel

You can also use IPython with TensorFlow and TFLean by running the following

docker run -it -p 8888:8888 -v ~/Work/notebooks:/notebooks tensorflow/tensorflow:0.10.0rc0

Notice that “~/Work/notebooks” is a local directory so I will not lose my changes after closing the running container.

Second setup would be to install bazel, you can use Homebrew, or if you already have it installed, just make sure that you have version 0.3.x

brew install bazel

Third step would be cloning TensorFlow GitHub repo

git clone https://github.com/tensorflow/tensorflow.git

and run

cd tensorflow./configure

Now it is time to prepare your bazel files, you have the WORKSPACE which lives in /tensorflow root directory, you need to uncoment these lines and sdk and ndk directory. And BUILD which lives in /tensorflow/tensorflow/examples/android

We are almost there, bear with me :)

Because the android app does not come with the inception model we will need to download it and copy it across to the assets directory.

curl -L https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -o /tmp/inception5h.zipunzip /tmp/inception5h.zip -d tensorflow/examples/android/assets/

Then run Android Studio and choose “Import project (eclipse, gradle, etc.)” and open tensorflow/tensorflow/examples/android/

You should be able now to run the demo from Android Studio as any other project. You just need to know that bazel is still used to compile the native libs and it has to be installed.

That must of been overwhelming. let me know if you have any questions. You can also have a look at https://github.com/MostafaGazar/tensorflow for the end result.

Part 2 is now released, check it out at https://goo.gl/fnsREt

Getting in touch

I’d love to help, get in touch with me (audit@mostafagazar.com) for a free Android APK Audit or a full Android Audit including your process and sourcecode.

Would you be interested in learning more about this https://leanpub.com/ml-mobile

--

--