Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow on Android with Python bindings?

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.

like image 883
Zoltan Fedor Avatar asked Mar 17 '17 15:03

Zoltan Fedor


People also ask

Can TensorFlow be used on Android?

TensorFlow Lite lets you run TensorFlow machine learning (ML) models in your Android apps. The TensorFlow Lite system provides prebuilt and customizable execution environments for running models on Android quickly and efficiently, including options for hardware acceleration.

Can you train with TFLite?

TensorFlow Lite is Google's machine learning framework to deploy machine learning models on multiple devices and surfaces such as mobile (iOS and Android), desktops and other edge devices. Recently, we added support to run TensorFlow Lite models in a browser as well.

Can you use TensorFlow with flutter?

Stay organized with collections Save and categorize content based on your preferences. If your app uses custom TensorFlow Lite models, you can use Firebase ML to deploy your models.

Can I train TensorFlow on Raspberry Pi?

You can execute TensorFlow on a Raspberry Pi 4, but don't expect miracles. It can run your models, if not too complex, but it will not be able to train new models. Nor can it perform the so-called transfer learning.


1 Answers

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:

  • Protobuf(C++)
  • OpenSSL

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.

like image 110
Peter Badida Avatar answered Oct 23 '22 14:10

Peter Badida