Sunday, March 26, 2017

Tensorflow on Android with Python bindings?

Leave a Comment

I am trying to build Tensorflow core with Python bindings for Android - to be used from a Kivy app in Android, but not sure how to get the Python bindings working.

Compiling the Tensorflow core for Android works with the following:

bazel build -c opt \   --crosstool_top=//external:android/crosstool \   --cpu=armeabi-v7a \   --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \   --verbose_failures \   //tensorflow/core:android_tensorflow_lib 

Does anybody know how to add Python bindings to it? Is that even possible?

I have read the documentations, did lots of searching, but no luck finding anything about this. I saw that lot of other people are also interested in getting Tensorflow working on Kivy (both for Android and iOS), but nobody seems to have done it so far. People only got as far as saying that we would need an python-for-android recipe built, but the first step for a recipe is to build for Android with Python bindings, hence the question above.

1 Answers

Answers 1

Note: I have never worked with Tensorflow


Judging from pypi the only dependency problem here might be NumPy, but there's a recipe for it. So, we have NumPy, but there's no recipe for the Tensorflow itself. What now?

The answer is in the documentation on how to make a recipe.

First I'd look at the already working recipes, to actually understand how they work, especially the ones that work with C/C++ and not with Cython as it's a little bit easier to make a recipe for Cython (more even if you made the code).

These are the recipes I consider a basic stone for creating a recipe for Tensorflow:

Each of them has a part that will help you to assemble a working recipe. Now, there's a problem with the bindings. There are manylinux wheels, but those most likely won't help you on Android (like at all) in the state they are. Therefore you'll need to build from source (obviously) and they have a whole folder related to that part.

That leads us to your piece of bazel code. In build_pip_package.sh there's a lot of lines related to the bazel thingy.

So, after you understand how the recipes work there are two possibilities:

  • your piece of code actually does something and builds it for android
  • your code is worthless

If your code actually works, there's a good chance you'll be able to combine the already made setup.py file into a simple-looking recipe hopefully just with another platform check (and using the correct bazel binary I presume). As there already is setup.py file, the package files should be moved to the Python that's compiled for Android.

On the other hand if the piece of code you pasted above is worthless, then you'll probably end up with compiling the code yourself + reinventing their setup.py just for the P4A as a recipe. If you aren't familiar with the compilation steps required for building from source, I really don't envy you the process.


Also it might be a good thing to mention the size of the final APK. You can see Tensorflow on PyPI with just a small wheel of 13MB for Windows. Manylinux wheels, however, are huge (37-42MB) and I think you'll end up with a size inbetween those two for Android if you plan to install anything else than Tensorflow itself. There's always NumPy you'll have to drag with it to the phone as a dependency. So that gives you ~50MB+ APK file, which might not be really desired (depends on you).

This piece of code related to Python in their repo might help you too.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment