tensorflow porting to android devices - android

I have a question about tensorflow porting to android devices. When I was in centos, using bazel compiler generated apk file, suggesting the following error:
ERROR: /data/tensorflow-master/WORKSPACE:37:1: syntax error at 'tf_workspace': expected,
ERROR: /data/tensorflow-master/WORKSPACE:44:1: non-keyword arg after keyword arg.
ERROR: error loading package 'external': Failed to parse WORKSPACE file.
INFO: Elapsed time: 0.242s
The following is the 37-44 line of the WORKSPACE file:
Please add all new TensorFlow dependencies in workspace.bzl.
37 tf_workspace ()
38
39 new_http_archive (
40 name = "inception5h",
41 build_file = "models.BUILD",
42 url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip",
43 sha256 = "d13569f6a98159de37e92e9c8ec4dae8f674fbf475f69fe6199b514f756d4364"
44)
I ask you have encountered similar problems? please help me.

I was facing the same problem, this is how I fixed it:
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 24,
# # Ensure that you have the build_tools_version below installed in the
# # SDK manager as it updates periodically.
build_tools_version = "25.0.3",
# # Replace with path to Android SDK on your system
path = "/home/yours/Android/Sdk/",
) #my mistake #) --> )
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
name="androidndk",
path="/home/yours/android-ndk-r12b/",
# # This needs to be 14 or higher to compile TensorFlow.
# # Note that the NDK version is not the API level.
api_level=14)

Related

Build tflite c++ lib for android with ndk 25 > with bazel

I'm building a huge project that requires ndk versions above 25 (I'm using 25.0.8775105 right now). And I would like to use TFLite on my project. Following the docs, to cross-compile it on my x86-64 machine to be suitable for arm64-v8a architectures the instructions goes as follows:
Get tensorflow source
Install bazel (I've tried both 6.0.0 and the 7.0.0-prebuild)
Run ./configure and say y to Would you like to interactively configure ./WORKSPACE for Android builds?, then configure it.
Run bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so
Extract the .so file and get the header files than use like any other library.
When I run ./configure and say yes to configuring ./WORKSPACE, it wants me to provide an ndk directory. However, when I provide it (it being /home/tb/Android/Sdk/ndk/25.0.8775105 on my system, very similar to what's recommended) I get hit with this: WARNING: The NDK version in /home/tb/Android/Sdk/ndk/25.0.8775105 is 25, which is not supported by Bazel (officially supported versions: [19, 20, 21]). Please use another version. Compiling Android targets may result in confusing errors. followed by an error caused by this:
Traceback (most recent call last):
File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 1363, in <module>
main()
File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 1336, in main
create_android_ndk_rule(environ_cp)
File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 653, in create_android_ndk_rule
get_ndk_api_level(environ_cp, android_ndk_home_path))
File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 747, in get_ndk_api_level
api_levels = sorted(os.listdir(platforms))
FileNotFoundError: [Errno 2] No such file or directory: '/home/tb/Android/Sdk/ndk/25.0.8775105/platforms'
I've tried compiling it with bazel build -c opt //tensorflow/lite:libtensorflowlite.so --fat_apk_cpu=arm64-v8a which did in fact built the .so file, but when I try to build the code it says C/C++: ld: error: undefined symbol: tflite::impl::Interpreter::Invoke(), I did run my project on linux and it works fine, android uses a similar if not the same cmake file so I think the .so is broken.
What should I do? Does anyone have a link to, preferably official, pre-built tflite aarch64 / arm64-v8a .so file for c++?
Update: I've opened an issue detailing everything I've tried up to now.

Problems attempting to build oboe c++ library using cmake on windows

I am attempting to build 'oboe' on a windows machine using cmake (version 3.25.0) and a visual studio generator. The end goal is to create a c++ audio library that uses oboe as the back end for android, but as a first step simply geting oboe to build is the plan.
I have managed to succesfully generate build and use a static library with a few simple test functions using the method described below, but I run into errors when attempting to build oboe.
I have also managed to get oboe to build by using the method described in the documentation and
doing add_subdirectory etc. in the CMakeLists file of android studio. However I am trying to generate/ build using cmake outside of android studio.
The oboe repo is found here: https://github.com/google/oboe
The toolchain file I am using is: 'android.toolchain.cmake' included in the NDK at the location: ndk version/build/cmake/android.toolchain.cmake
I use a batch script to automate generating/ building to several different android ABI's, inspired by a similar shell script included with the oboe repo 'build_all_android.sh'. The .bat script I made is shown below:
#echo OFF
set BUILD_DIR=build
set ANDROID_NDK=C:\Microsoft\AndroidNDK\android-ndk-r23c
set GENERATOR="Visual Studio 17 2022"
set CMAKE_GENERATOR=-G %GENERATOR%
set CMAKE_TOOLCHAIN_FILE=-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake
set CMAKE_SYSTEM_NAME=-DCMAKE_SYSTEM_NAME=Android
set EXTRA_CMAKE_ARGS=-DBUILD_SHARED_LIBS=true -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_static
CALL :build_android armeabi-v7a ARM 16
CALL :build_android arm64-v8a ARM64 21
CALL :build_android x86_64 x64 21
CALL :build_android x86 x86 16
EXIT /B %ERRORLEVEL%
:build_android
set ABI_VERSION=%~1
set GENERATOR_PLATFORM=%~2
set MINIMUM_API_LEVEL=%~3
set CMAKE_ANDROID_ARCH_ABI=-DANDROID_ABI=%ABI_VERSION%
set ABI_BUILD_DIR=%BUILD_DIR%\%ABI_VERSION%
set CMAKE_GENERATOR_PLATFORM=-A %GENERATOR_PLATFORM%
set CMAKE_BUILD_DIR=-B %ABI_BUILD_DIR%
set CMAKE_MIN_API=-DANDROID_PLATFORM=android-%MINIMUM_API_LEVEL%
set CMAKE_ARGS=%CMAKE_BUILD_DIR% %CMAKE_ANDROID_ARCH_ABI% %CMAKE_GENERATOR% %CMAKE_GENERATOR_PLATFORM% %CMAKE_SYSTEM_NAME% %CMAKE_TOOLCHAIN_FILE% %CMAKE_MIN_API%
echo building for android ABI: %ABI_VERSION%
echo cmake arguments = %CMAKE_ARGS%
echo:
cmake %CMAKE_ARGS% %EXTRA_CMAKE_ARGS%
echo:
cmake --build %ABI_BUILD_DIR% --target ALL_BUILD
echo:
echo:
EXIT /B 0
When I run this using the ANDROID_NDK variable (line 3) pointing to the root of the up to date ndk that comes with the visual studio 2022 android tools, I get an error:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
The C compiler
"C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/user/Documents/Code Projects/oboe/build/x86/CMakeFiles/CMakeScratch/TryCompile-pp2ibg
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_c21fb.vcxproj /p:Configuration=Debug /p:Platform=x86 /p:VisualStudioVersion=17.0 /v:m && MSBuild version 17.4.1+9a89d02ff for .NET Framework
ANDROID_HOME=C:\\Microsoft\AndroidSDK\25
ANDROID_SDK_ROOT=C:\\Microsoft\AndroidSDK\25
ANT_HOME=
JAVA_HOME=C:\Program Files\Android\jdk\jdk-8.0.302.8-hotspot\jdk8u302-b08
NDK_ROOT=C:\Microsoft\AndroidNDK\android-ndk-r23c
testCCompiler.c
In file included from <built-in>:349:
<command line>(1,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
#define __ANDROID_API__ 1
^
<built-in>(342,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 warning generated.
ld: error: cannot open crtbegin_so.o: No such file or directory
ld: error: unable to find library -llog
ld: error: unable to find library -landroid
ld: error: cannot open crtend_so.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Application Type\Android\3.0\Android.Common.targets(125,5): error MSB6006: "clang.exe" exited with code 1. [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
CMake will not be able to correctly generate this project.
and cmake fails to generate the project. I can actually get this to generate correctly, but only if I set the android NDK version within the cross-compilation options of visual studio to use the NDK bundled with my installation of Android studio, and also set the ANDROID_NDK variable of the batch script to point to the same root.
In this case, though cmake generates the project the build fails with a similar reason to the warning given above:
Building Custom Rule C:/Users/user/Documents/Code Projects/oboe/CMakeLists.txt
AAudioLoader.cpp
In file included from <built-in>:404:
<command line>(1,9): error : '__ANDROID_API__' macro redefined [-Werror,-Wmacro-redefined] [C:\Users\user\Documents\Co
de Projects\oboe\build\x86\oboe.vcxproj]
#define __ANDROID_API__ 16
^
<built-in>(394,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 error generated.
Thanks in advance for any help, it is much appreciated!
Your build fails because you instruct the compiler to view the warning '__ANDROID_API__' macro redefined as an error. And this warning seems to occur because Visual Studio still sets the macro __ANDROID_API__ instead of setting __ANDROID_MIN_SDK_VERSION__. I think that is a Visual Studio bug that was introduced when they started shipping the NDK 23 instead of 21.
EDIT:
This issue is being tracked here.

App construction generates error in toolchain.py by unrecognized argument

Versions
Python: 2.7
OS: Linux Lite (Ubuntu)
Kivy: 1.10.1
Cython: 0.22
Buildozer does not proceed with the creation of the App by an error in toolchain.py and arguments --ndk-api 9.
I switched to all cython versions from 0.21 to the latest, but nothing has changed. I went in the android sdk folder and made several updates, however the problem still persists.
the error is: 'toolchain.py: error: unrecognized arguments: --ndk-api 9'
last part of log:
usage: toolchain.py [-h] [--private PRIVATE] --package PACKAGE --name NAME
[--numeric-version NUMERIC_VERSION] --version VERSION
[--orientation ORIENTATION] [--launcher] [--icon ICON]
[--permission PERMISSIONS [PERMISSIONS ...]]
[--meta-data META_DATA] [--presplash PRESPLASH]
[--presplash-color PRESPLASH_COLOR] [--wakelock]
[--window] [--blacklist BLACKLIST] [--whitelist WHITELIST]
[--add-jar ADD_JAR] [--add-aar ADD_AAR] [--depend DEPENDS]
[--sdk SDK_VERSION] [--minsdk MIN_SDK_VERSION]
[--intent-filters INTENT_FILTERS] [--service SERVICES]
[--add-source EXTRA_SOURCE_DIRS]
[--try-system-python-compile] [--no-compile-pyo] [--sign]
toolchain.py: error: unrecognized arguments: --ndk-api 9
# Command failed: /usr/bin/python -m pythonforandroid.toolchain apk --debug --bootstrap=sdl2 --dist_name myapp --name 'My Application' --version 0.1 --package org.test.myapp --android_api 19 --minsdk 9 --ndk-api 9 --private /home/dev/Modelos/testea/.buildozer/android/app --orientation portrait --window --copy-libs --arch armeabi-v7a --color=always --storage-dir="/home/dev/Modelos/testea/.buildozer/android/platform/build"
#
# Buildozer failed to execute the last command
# The error might be hidden in the log above this error
# Please read the full log, and search for it before
# raising an issue with buildozer itself.
# In case of a bug report, please add a full log with log_level = 2
would anyone know the reason for this error?
for other people who may have this same problem, I put here everything I did to solve the bug.
Ok, first, this was a bug where the buildozer.spec file for some reason did not properly update the p4a files and there was also an error with the version of ndk used.
First I downloaded the r17c version of NDK. I went to the /home/dev/.buildozer/android/platform/ path and extract the downloaded file.
Then in the path /home/dev/buildozer/buildozer/default.spec I changed the branch used in p4a for the master and it looked like this:
# p4a.branch = master
I have also changed the following lines:
# (int) Android API to use
# android.api = 21
# (int) Minimum API required. You will need to set the android.ndk.api to be as low as this value.
# android.minapi = 21
# (int) Android SDK version to use
# android.sdk = 20
# (str) Android NDK version to use
# android.ndk = r17c
# (int) Android NDK API to use (optional). This is the minimum API your app will support.
# android.ndk_api = 19
Well, that initially did not change anything in the way the construction was being done and it continued to make a mistake. So I went on the path
/ home / dev / buildozer / buildozer / targets /
and in the file android.py I changed lines 17, 18 and 20 thus:
ANDROID_API = '21' #line 17
ANDROID_MINAPI = '21' #line 18
ANDROID_SDK_VERSION = '20'
ANDROID_NDK_VERSION = '17c' #line 20
APACHE_ANT_VERSION = '1.9.4'
in this same file, in the TargetAndroid class I changed lines 46 (the old name is python-for-android-new-version) and 47, the class looks like this:
class TargetAndroid (Target):
targetname = 'android_old'
p4a_directory = "python-for-android" #line 46
p4a_branch = 'master' #line 47
p4a_apk_cmd = "python build.py"
Ok, in this same path has a file called android_new.py, in the TargetAndroidNew class I changed lines 16 and 17, the class looks like this:
class TargetAndroidNew (TargetAndroid):
targetname = 'android'
p4a_branch = "master" #line 16
p4a_directory = "python-for-android" #line 17
p4a_apk_cmd = "apk --debug --bootstrap ="
extra_p4a_args = ''
Ok, this should already work, but for some reason there is another path with pretty much the same files, so I made the same changes. No path
/home/dev/buildozer/build/lib.linux-x86_64-2.7/buildozer/
I changed the default.spec file as I had done previously. And in the path
/home/dev/buildozer/build/lib.linux-x86_64-2.7/buildozer/targets/
I've changed the files android.py and android_new.pyin the same way as I previously did.
Now the buildozer performs the proper update of p4a and ndk and works correctly.

Building Tensorflow

I want to build tensorflow for my android smartphone. So I´ve installed bazel and get the sources for tensorflow.
git clone https://github.com/tensorflow/tensorflow
I modify the WORKSPACE file with my paths for the NDK and the SDK
android_sdk_repository(
name = "androidsdk",
api_level = 27,
build_tools_version = "27.0.3",
# Replace with path to Android SDK on your system
path = ".../AppData/Local/Android/Sdk",
)
android_ndk_repository(
name = "androidndk",
path = .../AppData/Local/Android/Sdk/ndk-bundle",
api_level = 27
)
And now I run bazel
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=#bazel_tools//tools/cpp:toolchain \
--cpu=armeabi-v7a
And got this error
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 72,096s, Critical Path: 14,55s
INFO: 213 processes, local.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
How can I compile tensorflow for android with windows (or maybe ubuntu)?

Buildozer Numpy RuntimeError: Broken toolchain: cannot link a simple C program

Writing my first Android app in Python and using Buildozer to package it. Because I will need to use numpy later on in the project, I tried packaging the following test code:
import numpy
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
However, I got the following error:
Traceback (most recent call last):
File "setup.py", line 251, in <module>
setup_package()
File "setup.py", line 243, in setup_package
setup(**metadata)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/core.py", line 169, in setup
return old_setup(**new_attr)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_ext.py", line 59, in run
self.run_command('build_src')
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/python-installs/myapp/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 153, in run
self.build_sources()
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 164, in build_sources
self.build_library_sources(*libname_info)
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 299, in build_library_sources
sources = self.generate_sources(sources, (lib_name, build_info))
File "/home/kivy/Desktop/cam/.buildozer/android/platform/build/build/other_builds/numpy/armeabi-v7a/numpy/numpy/distutils/command/build_src.py", line 386, in generate_sources
source = func(extension, build_dir)
File "numpy/core/setup.py", line 686, in get_mathlib_info
raise RuntimeError("Broken toolchain: cannot link a simple C program")
RuntimeError: Broken toolchain: cannot link a simple C program
STDERR:
# Command failed: /usr/bin/python -m pythonforandroid.toolchain create --dist_name=myapp --bootstrap=sdl2 --requirements=kivy,numpy --arch armeabi-v7a --copy-libs --color=always --storage-dir=/home/kivy/Desktop/cam/.buildozer/android/platform/build
#
# Buildozer failed to execute the last command
# The error might be hidden in the log above this error
# Please read the full log, and search for it before
# raising an issue with buildozer itself.
# In case of a bug report, please add a full log with log_level = 2
Also, here is my buildozer.spec file:
title = My Application
package.name = myapp
package.domain = org.test
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 1.0
requirements = kivy,numpy
orientation = portrait
osx.kivy_version = 1.9.1
fullscreen = 0
android.api = 19
android.sdk = 20
android.ndk = 9c
android.arch = armeabi-v7a
log_level = 2
warn_on_root = 1
Note that when I removed "import numpy" from the python code and removed "numpy" from the requirements list in the buildozer.spec file, my code was packaged perfectly. I am running this on VM Virtual Box which had Buildozer pre-installed.
Also it is not just Numpy giving me this issue- OpenCV is giving me the exact same errors. Will make separate post for that if needed.
This issue is reported in Python for Android (p4a) project here, didn't knew it's actual for stable p4a. Nevertheless, by link you can find PR that fixes issue. I didn't test it, but different people say it works.
You can try to build numpy with this fix, here's what you'll need:
Make sure you clean all left from current building process with command:
buildozer distclean
Clone p4a branch with fix using command:
git clone -b p4a_numpy_fix https://github.com/mahomahomaho/python-for-android fix-numpy
Change your buildozer.spec to use this cloned version of p4a (use your actual path):
p4a.source_dir = /home/ubuntu/p4a_numpy_fix
And run building apk again. If everything will work fine, you'll be able to build apk. If not you'll face another errors, no guarantees here :(

Categories

Resources