Ubuntu16.04/16.10 of TensorFlow demo on Android

Source: Internet
Author: User
Tags glob zip git clone

This is my first blog, in reference to other people's blog to install the process, for my platform system, encountered a lot of problems, here to write my practice and the problems encountered.
For the reference to the blogger's article, here to express thanks.
For this blog, if there is bad writing or wrong place, because my level is limited, as well as the limitations of the problems encountered, can not be taken into account, please give understanding, and hope to get good suggestions, for good suggestions I will be revised, thank you.
This blog for the author original, reproduced please indicate the source, thank you.

Linux Environment and Android platform
1. Ubuntu 16.04/16.10
2. Vivo Mobile phone
3. Android 5.1
TensorFlow Environment Installation (for TensorFlow demo only on Android platform porting can not be installed)
The following two blogs can be used to install specific processes
Http://www.linuxidc.com/Linux/2016-11/137561.htm
http://blog.csdn.net/zhaoyu106/article/details/52793183/
.. Note that the specific installation process can refer to any of the above two blogs, the Linux system may not be the same, but the installation method is similar. But the most important thing is to downgrade the GCC version mentioned in the main second blog,
because a temporary tensorflow GPU can only be used normally in GCC 5.0 and the following versions.

Android platform porting specific process

1. Install Java 1.8
 2. Install Bazel
 3. Download TensorFlow source
 4. Install android SDK
 5. Install android NDK
 6. Download TensorFlow model
 7. Open the handset development This mode, installs the ADB
 8.bazel compile, download to Android phone
 9. Measured results

1. Install Java 1.8

$ sudo apt-get installsoftware-properties-common
$ sudo add-apt-repositoryppa:webupd8team/java
$ sudo apt-get Update
$ sudo apt-get installoracle-java8-installer

When installed, configure the Java environment variables to add the following content to/etc/environment:

$JAVA _home= "/usr/lib/jvm/java-8-oracle"

It's best to find a network that is stable and fast, and it won't take much time. The author himself in the process of installation is because of a very slow net, under a very long time, and once also because of cotton, the direct termination of the drop load, and then download again, on the report of various errors. Because it stops downloading, there will be records and residues. My method is to clear off the downloaded, uninstall very clean, and then download it again.
The following error is encountered:

Download done.
sha256summismatchjdk-8u131-linux-x64.tar.gz
Oracle JDK 8 IsNOT installed.
DPKG: Error Scavenging:
 child process installed post-installation script returned error number 1
error occurred while processing:
 Oracle-java8-installer

The workaround is as follows:
Terminal input Ps-aux, list process. Find the process containing apt '-get, direct sudo kill PID. Solve.
Force unlock, command

$ sudo rm/var/cache/apt/archives/lock
$ sudo rm/var/lib/dpkg/lock
$ sudo kill 4250
$ sudo lsof/var/cache/ Debconf/config.dat
$ sudo apt-get remove orcacle-java8-installer* #卸除掉所有相关的东西
$ sudo apt-get autoremove$ sudo Apt-getautoclean
$ sudo apt-get clean

The above instructions for the termination of the installation process after the problems encountered should be able to solve. Once resolved, reinstall the java1.8.

2. Install Bazel

$ echo "Deb [arch=amd64]http://storage.googleapis.com/bazel-apt stable jdk1.8" |
  sudo tee/etc/apt/sources.list.d/bazel.list
$ curlhttps://bazel.io/bazel-release.pub.gpg | sudo apt-key add-
$ sudo apt-get update&& sudo apt-get install Bazel
$ sudo apt-get upgrade

For detailed instructions, refer to Bazel's official documentation.

3. Download TensorFlow source

$ cd ~/#进入到用户目录下
$ git clone https://github.com/tensorflow/tensorflow.git #从github上克隆github源码. Users without the Linux environment of git and git   , install them first

4. Install the Android SDK (Software Development Kit)

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
$ tar xvzf android-sdk_r24.4.1-linux.tgz-c ~/ TensorFlow #解压到tensorflow源码的文件下, just easy to manage, other catalogs can also.

5. Install Android NDK (Native Development Kit)

$ wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip
$ unzipandroid-ndk-r12b-linux-x86_64.zip-d ~/tensorflow  #必须保证ndk为至少r12

6. Download the TensorFlow model

$ cd ~/tensorflow
$wgethttps://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip-o/tmp/ Inception5h.zip
$ wgethttp://storage.googleapis.com/download.tensorflow.org/models/mobile_multibox_ V1a.zip-o/tmp/mobile_multibox_vla.zip
$wgethttp://storage.googleapis.com/download.tensorflow.org/models/ Stylize_v1.zip-o/tmp/stylize_v1.zip
$ unzip/tmp/inception5h.zip-dtensorflow/examples/android/assets/
$ unzip/tmp/mobile_multibox_vla.zip-dtensorflow/examples/android/assets/
$ unzip/tmp/stylize_ v1.zip-dtensorflow/examples/android/assets/

The above three models were downloaded.

Download model links with the TensorFlow Android version of the update may be changed, the specific version of the corresponding links please refer to the TensorFlow root directory workspace file, which indicates the current project version of the corresponding model download link.
After the decompression, open the//tensorflow/examples/android/build file, to find the beginning of the android_binary piece of code, the assets = [] In the beginning of the @ three code with the # Note out, The following figure has been annotated with the need to comment out the section below.

Android_binary (
   name = "Tensorflow_demo",
   SRCs = Glob ([
       "Src/**/*.java",
   ]),
    #Package assets From Assets dir as Asall model targets. Remove undesiredmodels
    # (and corresponding activities in source) to reduce APK size.
   #Modified by Hedlen 03/06/2017
   # # #需要注释掉的地方 ########
   assets = [
        "//tensorflow/examples/android/assets: Asset_files ",
      ": External_assets ",
   ],
   assets_dir =" ",
    # # #到这里结束 ############
   custom_ Package = "Org.tensorflow.demo",
   inline_constants = 1,
   manifest = "Androidmanifest.xml",
   manifest_ Merger = "Legacy",
   resource_files =glob (["res/**"]),
   tags = [
       "manual",
       "Notap",
   ],
    Deps = [
       ": Tensorflow_native_libs",
      "//tensorflow/contrib/android:android_tensorflow_inference_java",
   ],
)

.. Note : If you do not comment out this paragraph, even if you manually put the model in the assets file, the model will be downloaded again using Bazel compilation.
Modify WORKSPACE

$ gedit ~/tensorflow/workspace

The contents of the workspace under my installed version are:

# Uncomment and update thepaths in theseentries to build the Android demo.
Android_sdk_repository (
   name = "Androidsdk",
   api_level = num,
    #Ensure that you have thebuild_tools_version Below installed in the
    #SDK Manager as it updates periodically.
   Build_tools_version = "25.0.0",
    #Replace with the path to the Android SDK on yoursystem
   path = "/home/qw/tensorflow/ Android-sdk-linux ",
)
# #
Android NDK r12b isrecommended (Highermay cause with issues Bazel)
android_ Ndk_repository (
   name= "Androidndk",
  path= "/home/qw/tensorflow/android-ndk-r12b",
    #This needs to be or higher to Compiletensorflow.
    #Note the NDK version is not the APILevel.
   #api_level =14
   api_level=24)

Uncomment the Android_sdk_repository and android_ndk_repository sections and replace them with the contents above.
I have already commented on the above content.
Note that. for version issues here, such as the SDK version, you can view the version number in the platforms and Build-tools folders under the ~/android-sdk-linux path.
I installed the Android SDK corresponding to the installation of the Build-tools folder only one version of the 26.0.0, in the subsequent Bael compile process will error error:missing input file. ~/ 26.0.0 file Apksigner.jar missing file. Later, by installing Android Studio, I downloaded the 25.0.0 version (with Apksigner.jar in the file, which should be signed for publication) and added to the Build-tools ~/ Android-sdk-linux the path under the Build-tools folder, and modify the workspace file corresponding to the Build-tools version of the 25.0.0 after the error is resolved.
Attention.. You need to change the contents of the following file to:

Defnativebuildsystem = ' None '
defbazellocation = '/usr/bin/bazel '

The first path is/usr/bin/local/bazel, for my computer is/usr/bin/bazel, for those who do not know Bazel path, you can use

To view the Bazel path of the local computer

7. Open the mobile phone development model, install ADB (Android Debug Bridge)

Install ADB

$ sudoapt-get Install adb

Open the Android phone developer mode and allow debugging of different phone settings, please refer to the phone's open mode.
ADB detects connection Status

$ SUDOADB Devices

A normal connection is shown as follows:

Listof devices attached
xxxx   device

8.bazel build, download to Android phone

$ cd~/tensorflow  #进入tensorflow源码的路径下, compile only, if you do not enter the directory you need to specify the absolute path
$bazel build//tensorflow/examples/android: Tensorflow_demo

After the compilation succeeds, the following results are available:

Bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar
Bazel-bin/tensorflow/examples/android/ tensorflow_demo_unsigned.apk
bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

If there are no more results, or if there is an error in the middle, please check if the files such as workspace are configured correctly.

Download the apk file to the Android phone:

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

During the installation, you will be prompted to install, select OK.
Three apk will be installed on the phone, as shown below

9. Measured results

TF classify

TF Detect

TF stylize

10. Summary

This is my first blog, because it took a lot of time to write, and it wasn't very good. But the future will continue to improve and correct. For this blog application, in their own play for a period of time TensorFlow, is now doing a paper replicate, in GitHub open source. Already done, now want to transplant the model of their own training to Android, look at the actual scene, test the accuracy of their own models.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.