Slimming down TensorFlow’s impact on your .APK size

By applying selective registration

Yoni Tsafir
Simply
3 min readJun 19, 2017

--

TL;DR I managed to slim down the APK by 10MB by compiling the TensorFlow mobile inference libs with -DSELECTIVE_REGISTRATION

If you’re looking only for the steps to achieve that jump down to Steps to apply selective registration to your libtensorflow_inference.so

Background and motivation

In my previous post I gave a detailed guide on how to build TensorFlow 1.0 for Android deployment:

Since then Google IO 2017 happened and it contained some exciting announcements about mobile deployment of TensorFlow, so stay tuned.

In the meantime, I learned something really useful from the following session which I really recommend watching:

In minute 27:07 of the video, they are talking about compressing the binary with selective registration.

As it happens, after I compiled TensorFlow to include it in Simply Piano for Android, I found the total raw file sizes of the libs was a whopping 52MB, which translated to an additional 16MB in download size.

Here’s a screenshot of the breakdown, taken from Android Studio by using Build -> Analyze APK…

Analyze APK breakdown — before selective registration

So you can see why I was worried about my APK size and something with the title “Compressing the binary with XXX” sounded like something I need.

Steps to apply selective registration to your libtensorflow_inference.so

Selective registration is very easy to apply and as you will immediately see, had a very positive impact on the APK size. What it essentially does is stripping the TensorFlow libs from everything that isn’t needed for the specific model(s) you are going to use in your app.

Here are the steps (run in the main dir of your clone of the tensorflow github repo):

  1. Build the selective registration header generator:
bazel build tensorflow/python/tools/print_selective_registration_header

2. Run the header generator on your model and save the generated header. This will create a .h file that contains only the list of Ops used by your model:

bazel-bin/tensorflow/python/tools/print_selective_registration_header --graphs=model.pb > tensorflow/core/framework/ops_to_register.h

3. Now compile tensorflow for android (explained in my full guide), but add the following flag to your bazel build command

--cxxopt=”-DSELECTIVE_REGISTRATION”

Edit July 2017: After reading the amazing post by

who also mentions this trick, I decided to also add --cxxopt="-Os" to the compilation command, for a few additional 100s of KB shaved from each .so

The Results

The resulting libtensorflow_inference.so files were much slimmer.

The total saving in raw file size was almost 36MB, about 69%. The total saving in download size was 10MB, or 61%. Awesome :)

Here’s the Analyze APK breakdown:

Analyze APK breakdown — after selective registration

And here’s a graph for Download Size change (Raw File Size is similar):

Download Size Before & After Selective Registration

--

--

Yoni Tsafir
Simply
Writer for

Software Engineer (On-Device AI) @ Google. Previously JoyTunes. Mobile Development, AI, Software Craftsmanship. Hands-on dad.