Tensorflow 在 Android 平台的移植

Ubuntu 14.04

这里假定 Ubuntu 14.04 系统上还没有 Android 开发环境。

安装 Java 1.8

$ 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

配置 Java 环境变量,将下面的内容添加到 /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

安装 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.io/bazel-release.pub.gpg | sudo apt-key add - $ sudo apt-get update && sudo apt-get install bazel $ sudo apt-get upgrade bazel

详细的说明可以参考 bazel 的官方文档

下载 tensorflow

$ cd ~/
$ git clone https://github.com/tensorflow/tensorflow.git

之后的步骤基本来自 TensorFlow on Android 的翻译:

下载解压 Android SDK

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz $ tar xvzf android-sdk_r24.4.1-linux.tgz -C ~/tensorflow

更新 SDK:

$ cd ~/tensorflow/android-sdk-linux # 如果希望在熟悉的 SDK Manager 中进行操作,可以去掉下面命令中的 --no-ui $ tools/android update sdk --no-ui

下载解压 NDK

$ wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip $ unzip android-ndk-r12b-linux-x86_64.zip -d ~/tensorflow

下载 tensorflow 的 model

$ cd ~/tensorflow
$ 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

$ gedit ~/tensorflow/WORKSPACE

反注释 android_sdk_repository 和 android_ndk_repository 部分,用下面的内容替换:

android_sdk_repository(
    name = "androidsdk", api_level = 24, build_tools_version = "24.0.3", # Replace with path to Android SDK on your system path = "/home/ross/Downloads/android-sdk-linux", ) android_ndk_repository( name="androidndk", path="/home/ross/Downloads/android-ndk-r12b", api_level=24)

编译 tensorflow 的 Android Demo App:

$ 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 INFO: Elapsed time: 109.114s, Critical Path: 37.45s

Android Demo 分析

整个 Demo 的目录结构和使用 Jni 的 Android 工程是相同的,在 ~/tensorflow/tensorflow/examples/android/jni 目录下,放着 native 的代码:

├── imageutils_jni.cc ├── __init__.py ├── rgb2yuv.cc ├── rgb2yuv.h ├── yuv2rgb.cc └── yuv2rgb.h

Java interface 相关的 Java 类在 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android 目录里面,可以考虑将其直接集成到自己的项目中。

Demo 所需的 native 实现在 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android/jni目录里面。

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageListener.java 里面定义了用到的 tensorflow model,protobuf 格式,识别结果的 labels 等:

    private static final Logger LOGGER = new Logger(); private static final boolean SAVE_PREVIEW_BITMAP = false; private static final String MODEL_FILE = "file:///android_asset/tensorflow_inception_graph.pb"; private static final String LABEL_FILE = "file:///android_asset/imagenet_comp_graph_label_strings.txt"; private static final int NUM_CLASSES = 1001; private static final int INPUT_SIZE = 224; private static final int IMAGE_MEAN = 117;

如果想使用自己的模型,使用 tensorflow 解决其他的问题,通过修改上面提到的代码和模块来完成。TensorFlow on Android 文章就提到了具体的步骤。

最后,Tensorflow 也支持移植到 iOS 应用中,可以参考 TalkingData SDK Team 的技术博客文章 iOS 开发迎来机器学习的春天— TensorFlow

 

Ubuntu下安装tensorflow.

   $ sudo apt-get install python-pip python-dev
  1. Ensure proper protobuf dependencies by issuing one of the following commands:

     

    $ sudo pip uninstall tensorflow # for Python 2.7
     $ sudo pip3 uninstall tensorflow # for Python 3.n

     

  2. Install TensorFlow by invoking one of the following commands:

     

    $ pip install tensorflow      # Python 2.7; CPU support (no GPU support)
     $ pip3 install tensorflow     # Python 3.n; CPU support (no GPU support)
     $ pip install tensorflow-gpu  # Python 2.7;  GPU support
     $ pip3 install tensorflow-gpu # Python 3.n; GPU support 
  3. Run a short TensorFlow program

    Invoke python from your shell as follows:

     

    $ python
    

    Then, enter the following short program inside the python interactive shell:

     

    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow!')
    >>> sess = tf.Session()
    >>> print(sess.run(hello))
    

    If the system outputs the following, then you are ready to begin running TensorFlow programs:

     

    Hello, TensorFlow!
    原文作者:tensorflow
    原文地址: https://www.cnblogs.com/jamboo/articles/6498214.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞