Tensorflow官方Android demo部署到手机

版权声明:转载请注明出处 https://blog.csdn.net/weixin_39653948/article/details/82660026

系统配置:

ubuntu18.04 cuda9.0 cudnn7.0 python2.7 tensorflow-1.10

bazel-0.16 JDK8 SDK28.0.2 NDK12 android-8.0

经过四天多的折腾,终于将tensorflow官方的android demo部署到了手机上,虽然遇到了很多坑,但终究目的还是达到了。由于tensorflow的源码更新速度太快,导致如今很多教程都不适用了,比如WORKSPACE文件夹内容,根本就没有android配置的选项,而自己添加之后,再编译会报一大堆错误,主要错误如图。

这里写图片描述

为了帮助后来遇到相同错误的朋友找到解决思路,也给自己一个警醒,贴出错误开头部分:

/home/chopin/.cache/bazel/_bazel_chopin/c684f6c11184c596bda22514ba46f12a/external/protobuf_archive/BUILD:105:1: C++ compilation of rule ‘@protobuf_archive//:protobuf’ failed (Exit 1)
In file included from external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/initializer_list:36:0,
from external/protobuf_archive/src/google/protobuf/map.h:40,
from external/protobuf_archive/src/google/protobuf/generated_message_table_driven.h:34,
from external/protobuf_archive/src/google/protobuf/generated_message_table_driven.cc:31:
external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

针对错误,我已经尝试过的 错误的 解决思路:
1. 我发现很多教程是修改tensorflow源码下的WORKSPACE,然而我git clone最新的源码如图:
这里写图片描述

于是,我添加了以下内容(路径已经修改,关于完整的配置,下文会讲):

# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
    name = "androidsdk",
    api_level = 28,
    # Ensure that you have the build_tools_version below installed in the
    # SDK manager as it updates periodically.
    build_tools_version = "28.0.2",
    # Replace with path to Android SDK on your system
    path = "/home/chopin/Android/android-sdk-linux",
)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
    name="androidndk",
    path="/home/chopin/Android/android-ndk-r12b",
#    # This needs to be 14 or higher to compile TensorFlow.
#    # Please specify API level to >= 21 to build for 64-bit
#    # archtectures or the Android NDK will automatically select biggest
#    # API level that it supports without notice.
#    # Note that the NDK version is not the API level.
   api_level=24)

bazel编译之后,报错,如文中开头所示。我感觉是不是WORKSPACE内容不完整导致的,因此尝试源码编译tensorflow。依次输入如下指令(主要是configure配置):

git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure

我的选择如下,成功生成whl文件:
Please specify the location of python. [Default is /home/chopin/anaconda3/bin/python]: /home/chopin/anaconda3/bin/python

Found possible Python library paths:
  /home/chopin
  /home/chopin/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/chopin]
/home/chopin/anaconda3/lib/python3.6/site-packages
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: y
jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: y
Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: y
Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon AWS Platform support? [Y/n]: y
Amazon AWS Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: y
Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: n
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with nGraph support? [y/N]: n
No nGraph support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.0

Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0

Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda

Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow.

Please specify the NCCL version you want to use. If NCCL 2.2 is not installed, then you can use version 1.3 that can be fetched automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 1.3 

Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0]: 5.0

Do you want to use clang as CUDA compiler? [y/N]: n
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/x86_64-linux-gnu-gcc-6]: /usr/bin/x86_64-linux-gnu-gcc-6

Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=native

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is /home/chopin/Android/Sdk/ndk-bundle]: /home/chopin/Andriod/android-ndk-r12b

Please specify the home path of the Android SDK to use. [Default is /home/chopin/Android/Sdk]: /home/chopin/Andriod/android-sdk-linux

Please specify the Android SDK API level to use. [Available levels: ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '7', '8', '9']] [Default is 9]: 26

Please specify an Android build tools version to use. [Available versions: ['28.0.2']] [Default is 28.0.2]: 28.0.2

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
    --config=mkl            # Build with MKL support.
    --config=monolithic     # Config for mostly static monolithic build.
Configuration finished

注意Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: 选y! 这是Github官方提到的。
之后依次输入如下指令生成安装包并进行安装(时间较长,大约一个小时):

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

安装完之后,发现WORKSPACE内容并没有改动!!!重新进行demo的编译当然还是报错!之后进行第二种尝试。
2. 我仔细看了看报错的原因,大体意思是说不支持 C++ 2011标准,我觉得是不是ubuntu18.04自带的gcc版本太高了,大多数教程也是在ubuntu16.04下移植成功的,因此我借来老师的电脑,进行一系列的配置之后,发现错误还是相同的。所以我卸载了bazel,换用更低版本进行尝试,发现无法编译,bazel版本必须高于0.15.0才可以进行编译。我又将gcc++卸载,安装了4.9.3版本,结果还是报错。快崩溃了,编了删,编了删~~~ 源码也下了N多遍,四天一个错误都没解决,难受啊。
3. 我回到windows10系统,用android studio从头进行JDK、SDK、NDK的配置,之后进行demo的安装,结果还是报错。我又回到ubuntu18.04下用android studio进行配置,好在功夫不负有心人,一次无意之举导致安装成功。下面说说正确的思路,最后总结一下整个流程。

思路就是: 配置完java环境之后,用bazel进行demo的编译。经过一段时间编译之后,文中开头所示我遇到的错误,虽然apk文件没有生成,但是这个demo需要的jar和so文件已经生成,这正是在android studio里我们需要的。因此我们直接用android studio连接手机,进行安装。

部署流程

一.ubuntu下bazel安装

1.安装JDK8

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
# 配置环境变量
$ sudo gedit ~/.bashrc
# 文末添加
 JAVA_HOME = "/usr/lib/jvm/java-8-oracle"

2.安装bazel

(1)更新源安装

# 增加Bazel的发布源
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
# 安装和升级
$ sudo apt-get update && sudo apt-get install bazel
$ sudo apt-get upgrade bazel

(2)下载安装包安装

# 从这里下载源码:
https://github.com/bazelbuild/bazel/releases
# 切换到所在文件夹执行:
$ sudo chmod 777 包名
$ ./包名 --user

二.安装SDK和NDK

1.SDK

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
# 将SDK解压到自己创建的Android文件夹
$ tar xvzf android-sdk_r24.4.1-linux.tgz -C ~/Android
# 更新并安装build-tools
$ cd  ~/Android/android-sdk-linux
$ sudo  tools/android  update  sdk --no-ui

2.NDK

# 下官推荐的NDK-12 并解压到Android文件夹
$ wget https://dl.google.com/android/repositor/android-ndk-r12b-linux-x86_64.zip 
$ unzip android-ndk-r12b-linux-x86_64.zip -d ~/Android

三.用bazel进行编译安装

1.修改WORKSPACE(添加以下内容)

下载tensorflow源码:

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git

我的手机为安卓8.0系统,因此的配置如下。
将sdk path和 ndk path改为安装的SDK和NDK路径。

# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
    name = "androidsdk",
    api_level = 28,
    # Ensure that you have the build_tools_version below installed in the
    # SDK manager as it updates periodically.
    build_tools_version = "28.0.2",
    # Replace with path to Android SDK on your system
    path = "/home/chopin/Android/android-sdk-linux",
)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
    name="androidndk",
    path="/home/chopin/Android/android-ndk-r12b",
#    # This needs to be 14 or higher to compile TensorFlow.
#    # Please specify API level to >= 21 to build for 64-bit
#    # archtectures or the Android NDK will automatically select biggest
#    # API level that it supports without notice.
#    # Note that the NDK version is not the API level.
   api_level=24)

2.进行编译

$ cd ~/tensorflow
$ bazel build //tensorflow/examples/android:tensorflow_demo
# 如果有以下提示证明成功
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
# 安装adb工具
$ sudo  apt-get  install android-tools-adb
# 连接手机,并打开开发者模式,进行app的安装
$ adb install -r -g bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

如果报错跟我相同(文中开头所示)请往下看

上一步会生成所需的jar文件和so文件,查看以下路径下文件是否存在,如果存在则可以忽略错误,继续用android studio完成app的安装。

~/tensorflow/bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so
~/tensorflow/bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar

四.用Android Studio进行安装

AS打开demo时候,选择不更新项目,更新之后会报错

demo所在路径:
/home/chopin/tensorflow/tensorflow/examples/android

1.修改

# 第45行修改为none 则不会重新进行编译
def nativeBuildSystem = 'none'

配置sdk信息
这里写图片描述

2.下载所用模型

因为download-models.grandle中自动下载太慢,我下了一个多小时只下载了两个,所以手动下载之后放到指定文件夹下即可。

# 下载模型地址
https://storage.googleapis.com/download.tensorflow.org/models/inception_v1.zip
https://storage.googleapis.com/download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_android_export.zip
https://storage.googleapis.com/download.tensorflow.org/models/stylize_v1.zip
https://storage.googleapis.com/download.tensorflow.org/models/speech_commands_conv_actions.zip

#解压之后存放到以下地址
/home/chopin/tensorflow/tensorflow/examples/android/gradleBuild/downloads

3.run “android” 完成编译

等待安装apk完成就可以了。

安装完的四个应用:
这里写图片描述
目标检测的测试
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39653948/article/details/82660026