CodingYourLife

CodingYourLife

 

Referenzen

 
 

Bazel installieren und Tensorflow Android App kompilieren

Schritt 1: Tensorflow installieren
Schritt 2: am Einfachsten Android Studio installieren
Schritt 3: Bazel installieren (nachfolgend beschrieben)
Schritt 4 *OPTIONAL!?*: Android example mittels bazel kompilieren (nachfolgend beschrieben)
Schritt 5 *NEU*: Mit Android Studio Tensorflow Demo App debuggen

Genutzt wurde Ubuntu 16.04 LTS mit tensorflow v0.8 und bazel 0.2.2b.

Nachdem Tensorflow installiert ist und auch Android Studio, sollten im Android Studio SDK Manager von Android Studio kompatible Versionen angehakt/installiert werden.
SDK-Manager

Dann muss dafür gesorgt werden, dass die richtige Java Version installiert und verwendet wird.

#get and use java 8 (java 9+ not supported with bazel 0.2.2b)
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install oracle-java8-set-default

Abhängigkeiten für bazel installieren (laut offizieller Installationsanleitung).

#required packages (listed on official installation guide)
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

bazel v0.2.2b herunterladen, installieren und testen. Releases sind hier gelistet Bazel Releases

#download and install bazel (important: --user)
wget https://github.com/bazelbuild/bazel/releases/download/0.2.2b/bazel-0.2.2b-installer-linux-x86_64.sh
chmod +x bazel-0.2.2b-installer-linux-x86_64.sh
./bazel-0.2.2b-installer-linux-x86_64.sh --user
export PATH="$PATH:$HOME/bin"

#test bazel (should return v2.2b and path /home/administrator/bin/bazel)
bazel version
which bazel

Kompatible NDK Version installieren

mkdir /home/administrator/Android/Sdk/ndk
cd /home/administrator/Android/Sdk/ndk
wget http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip
unzip android-ndk-r10e-linux-x86_64.zip
rm android-ndk-r10e-linux-x86_64.zip

#put NDK_HOME in PATH
export NDK_HOME="/home/administrator/Android/Sdk/ndk/android-ndk-r10e"
export PATH=$PATH:$NDK_HOME

Tensorflow Projekt inkl. Android Example von Github holen.

sudo apt-get install git -y #install git if you don't have it
cd ~/
git clone https://github.com/tensorflow/tensorflow.git tensorflowGIT
cd tensorflowGIT
wget https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -O /tmp/inception5h.zip
unzip /tmp/inception5h.zip -d tensorflow/examples/android/assets/

WORKSPACE Datei mittels

cd ~/tensorflowGIT/tensorflow/
sudo nano WORKSPACE

anpassen

#remove comment hashtags and insert SDK and NDK paths
android_sdk_repository(
    name = "androidsdk",
    api_level = 23,
    build_tools_version = "23.0.1",
    # Replace with path to Android SDK on your system
    path = "/home/administrator/Android/Sdk",
)
android_ndk_repository(
    name="androidndk",
    path="/home/administrator/Android/Sdk/ndk/android-ndk-r10e",
    api_level=21)

Das Android Projekt kompilieren

#compile
cd ~/tensorflowGIT
bazel build //tensorflow/examples/android:tensorflow_demo --verbose_failures

Das tensorflow_demo.apk wird kompiliert und abgespeichert.
BuildSuccess

ADB für das deployment installieren.

#deploy
sudo apt-get install adb -y

Das Android Smartphone anstecken, Developer Settings aktivieren und deployen.

adb install -r -g bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

Kommentar schreiben

Kommentar

Hi,

I keep getting the error:

alex@alex-VirtualBox:~/tensorflow$ bazel build //tensorflow/examples/android:tensorflow_demo –verbose_failures
ERROR: com.google.devtools.build.lib.packages.BuildFileContainsErrorsException: error loading package “: Extension file not found. Unable to load file ‚//tensorflow:workspace.bzl‘: file doesn’t exist or isn’t a file.
INFO: Elapsed time: 0.095s

any hint?

Hi

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

is not possible, at it already exists..

I changed it to „git clone URL targetDir“ thanks

hi,

tried to run with bazel but:

alex@as:~/tensorflow$ bazel build //tensorflow/examples/android:tensorflow_demo –verbose_failures
………………………..
ERROR: /home/alex/tensorflow/tensorflow/tensorflow.bzl:567:26: Traceback (most recent call last):
File „/home/alex/tensorflow/tensorflow/tensorflow.bzl“, line 561
rule(attrs = {„srcs“: attr.label_list…“), )}, )
File „/home/alex/tensorflow/tensorflow/tensorflow.bzl“, line 567, in rule
attr.label_list(cfg = „data“, allow_files = True)
expected ConfigurationTransition or NoneType for ‚cfg‘ while calling label_list but got string instead: data.
ERROR: com.google.devtools.build.lib.packages.BuildFileContainsErrorsException: error loading package “: Extension file ‚tensorflow/tensorflow.bzl‘ has errors.
INFO: Elapsed time: 1.927s
alex@as:~/tensorflow$

Sorry for the late response. I thought I won’t be able to help you with that but I just ran into the same issue. So if you still have this problem you are lucky :). Just get a newer bazel version from here https://github.com/bazelbuild/bazel/releases (I added the link to the bazel install part too). I used v0.3.2 and that worked together with tensorflow git version from today.