TensorFlow Mobile(一)TensorFlow Mobile Android构建

TensorFlow Mobile是TensorFlow的第一个很对移动端嵌入式这是的支持框架。在早期的TensorFlow发布中加入了对Android、iOS和树莓派的支持,主要采用交叉编译的方式,能够缩短开发周期,并在设备上快速运行,总体缺点是编译后占用空间还是比较大。

TensorFlow Android Camera Demo

https://github.com/tensorflow/tensorflow/tree/r2.1/tensorflow/examples/android

可以根据tensorflow源码打开目录里的Readme进行查看。

This folder contains an example application utilizing TensorFlow for Android devices.

Description

The demos in this folder are designed to give straightforward samples of using TensorFlow in mobile applications.

Inference is done using the TensorFlow Android Inference Interface, which may be built separately if you want a standalone library to drop into your existing application. Object tracking and efficient YUV -> RGB conversion are handled by libtensorflow_demo.so.

A device running Android 5.0 (API 21) or higher is required to run the demo due to the use of the camera2 API, although the native libraries themselves can run on API >= 14 devices.

Current samples:

  1. TF Classify: Uses the Google Inception model to classify camera frames in real-time, displaying the top results in an overlay on the camera image.
  2. TF Detect: Demonstrates an SSD-Mobilenet model trained using the Tensorflow Object Detection API introduced in Speed/accuracy trade-offs for modern convolutional object detectors to localize and track objects (from 80 categories) in the camera preview in real-time.
  3. TF Stylize: Uses a model based on A Learned Representation For Artistic Style to restyle the camera preview image to that of a number of different artists.
  4. TF Speech: Runs a simple speech recognition model built by the audio training tutorial. Listens for a small set of words, and highlights them in the UI when they are recognized.

classify1.jpguploading.4e448015.gif正在上传…重新上传取消stylize1.jpguploading.4e448015.gif正在上传…重新上传取消detect1.jpguploading.4e448015.gif正在上传…重新上传取消

Running the Demo

Once the app is installed it can be started via the "TF Classify", "TF Detect", "TF Stylize", and "TF Speech" icons, which have the orange TensorFlow logo as their icon.

While running the activities, pressing the volume keys on your device will toggle debug visualizations on/off, rendering additional info to the screen that may be useful for development purposes.

(1)直接安装APP

The fastest path to trying the demo is to download the prebuilt demo APK.

(2)Building in Android Studio using the TensorFlow AAR from JCenter

直接选择none或者cmake方式编译运行。

The simplest way to compile the demo app yourself, and try out changes to the project code is to use AndroidStudio. Simply set this android directory as the project root.

Then edit the build.gradle file and change the value of nativeBuildSystem to 'none' so that the project is built in the simplest way possible:

def nativeBuildSystem = 'none'

While this project includes full build integration for TensorFlow, this setting disables it, and uses the TensorFlow Inference Interface package from JCenter.

Note: Currently, in this build mode, YUV -> RGB is done using a less efficient Java implementation, and object tracking is not available in the "TF Detect" activity. Setting the build system to 'cmake' currently only builds libtensorflow_demo.so, which provides fast YUV -> RGB conversion and object tracking, while still acquiring TensorFlow support via the downloaded AAR, so it may be a lightweight way to enable these features.

For any project that does not include custom low level TensorFlow code, this is likely sufficient.

For details on how to include this JCenter package in your own project see tensorflow/tools/android/inference_interface/README.md

(3)Building the Demo with TensorFlow from Source

Pick your preferred approach below. At the moment, we have full support for Bazel, and partial support for gradle, cmake, make, and Android Studio.

As a first step for all build types, clone the TensorFlow repo with:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
git checkout r2.1

Note that --recurse-submodules is necessary to prevent some issues with protobuf compilation.

Bazel

NOTE: Bazel does not currently support building for Android on Windows. Full support for gradle/cmake builds is coming soon, but in the meantime we suggest that Windows users download the prebuilt demo APK instead.

Install Bazel and Android Prerequisites

Bazel is the primary build system for TensorFlow. To build with Bazel, it and the Android NDK and SDK must be installed on your system.

  1. Install the latest version of Bazel as per the instructions on the Bazel website.
  2. The Android NDK is required to build the native (C/C++) TensorFlow code. The current recommended version is 14b, which may be found here.
  3. The Android SDK and build tools may be obtained here, or alternatively as part of Android Studio. Build tools API >= 23 is required to build the TF Android demo (though it will run on API >= 21 devices)

Edit WORKSPACE

NOTE: As long as you have the SDK and NDK installed, the ./configure script will create these rules for you. Answer "Yes" when the script asks to automatically configure the ./WORKSPACE.

The Android entries in <workspace_root>/WORKSPACE must be uncommented with the paths filled in appropriately depending on where you installed the NDK and SDK. Otherwise an error such as: "The external label '//external:android/sdk' is not bound to anything" will be reported.

Also edit the API levels for the SDK in WORKSPACE to the highest level you have installed in your SDK. This must be >= 23 (this is completely independent of the API level of the demo, which is defined in AndroidManifest.xml). The NDK API level may remain at 14.

Install Model Files (optional)

通过bazel或者gradle下载模型文件。

The TensorFlow GraphDefs that contain the model definitions and weights are not packaged in the repo because of their size. They are downloaded automatically and packaged with the APK by Bazel via a new_http_archive defined in WORKSPACE during the build process, and by Gradle via download-models.gradle.

Optional: If you wish to place the models in your assets manually, remove all of the model_files entries from the assets list in tensorflow_demo found in the BUILD file. Then download and extract the archives yourself to the assets directory in the source tree:

BASE_URL=https://storage.googleapis.com/download.tensorflow.org/models
for MODEL_ZIP in inception5h.zip ssd_mobilenet_v1_android_export.zip stylize_v1.zip
do
  curl -L ${BASE_URL}/${MODEL_ZIP} -o /tmp/${MODEL_ZIP}
  unzip /tmp/${MODEL_ZIP} -d tensorflow/examples/android/assets/
done

This will extract the models and their associated metadata files to the local assets/ directory.

If you are using Gradle, make sure to remove download-models.gradle reference from build.gradle after your manually download models; otherwise gradle might download models again and overwrite your models.

Build

After editing your WORKSPACE file to update the SDK/NDK configuration, you may build the APK. Run this from your workspace root:

bazel build --cxxopt='--std=c++11' -c opt //tensorflow/examples/android:tensorflow_demo

Install

Make sure that adb debugging is enabled on your Android 5.0 (API 21) or later device, then after building use the following command from your workspace root to install the APK:

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

(4)Android Studio with Bazel

选择bazel方式编译运行。

Android Studio may be used to build the demo in conjunction with Bazel. First, make sure that you can build with Bazel following the above directions. Then, look at build.gradle and make sure that the path to Bazel matches that of your system.

At this point you can add the tensorflow/examples/android directory as a new Android Studio project. Click through installing all the Gradle extensions it requests, and you should be able to have Android Studio build the demo like any other application (it will call out to Bazel to build the native code with the NDK).

 

Android TensorFlow support

https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/contrib/android/README.md

产生云生.so库和Java JAR包支持Android TesnorFlow使用。包括:

  • TensorFlow Java API
  • 类TensorFlowInferenceInterface提供较小的API满足模型的inference运行以及性能。

添加到最顶层WORKSPACE:

android_sdk_repository (                                                                                                                    
    name = "androidsdk",
    api_level = 23,
    build_tools_version = "26.0.2",
    path = "/Users/manda1/Library/Android/sdk/",
)
android_ndk_repository(
    name = "androidndk",
    path = "/Users/manda1/Library/Android/ndk/ndk-bundle/android-ndk-r14b",
    api_level = 14,
)

新的版本里,这个手动的过程被改变了。解决方法是在顶层运行./configure,提示下设置开发者环境变量:

./configure
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.29.1 installed.
Please specify the location of python. [Default is /Users/manda1/Documents/Develope/venv3_build/bin/python]: /usr/bin/python2.7

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

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

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

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

Do you wish to download a fresh release of clang? (Experimental) [y/N]: 
Clang will not be downloaded.

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


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 /Users/manda1/library/Android/Sdk/ndk-bundle]: /Users/manda1/Library/Android/ndk/ndk-bundle/android-ndk-r14b


Please specify the (min) Android NDK API level to use. [Available levels: ['12', '13', '14', '15', '16', '17', '18', '19', '21', '22', '23', '24', '9']] [Default is 21]: 14


Please specify the home path of the Android SDK to use. [Default is /Users/manda1/library/Android/Sdk]: 


Please specify the Android SDK API level to use. [Available levels: ['20', '23', '24', '25', '26', '27', '28']] [Default is 28]: 26


Please specify an Android build tools version to use. [Available versions: ['20.0.0', '23.0.2', '25.0.0', '25.0.3', '26.0.1', '26.0.2', '27.0.3', '28.0.3']] [Default is 28.0.3]: 26.0.2


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

Bazel编译

编译原生TF库:

bazel build -c opt //tensorflow/tools/android/inference_interface:libtensorflow_inference.so \
   --crosstool_top=//external:android/crosstool \
   --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
   --cxxopt=-std=c++11 \
   --cpu=armeabi-v7a

替换armeabi-v7a为其他想要的目标架构。

编译结果位于:

bazel-bin/tensorflow/tools/android/inference_interface/libtensorflow_inference.so

编译Java对应部分:

bazel build //tensorflow/tools/android/inference_interface:android_tensorflow_inference_java

产生的JAR文件位于:

bazel-bin/tensorflow/tools/android/inference_interface/libandroid_tensorflow_inference_java.jar

以上基于r2.0版本实践。r1.11以上版本没有编译成功。随后尝试。

基于r2.1版本无法编译构建,可见未来Tenosrflow Mobile会逐渐放弃。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Create Deep Learning and Reinforcement Learning apps for multiple platforms with TensorFlow Key Features Build TensorFlow-powered AI applications for mobile and embedded devices Learn modern AI topics such as computer vision, NLP, and deep reinforcement learning Get practical insights and exclusive working code not available in the TensorFlow documentation Book Description As a developer, you always need to keep an eye out and be ready for what will be trending soon, while also focusing on what's trending currently. So, what's better than learning about the integration of the best of both worlds, the present and the future? Artificial Intelligence (AI) is widely regarded as the next big thing after mobile, and Google's TensorFlow is the leading open source machine learning framework, the hottest branch of AI. This book covers more than 10 complete iOS, Android, and Raspberry Pi apps powered by TensorFlow and built from scratch, running all kinds of cool TensorFlow models offline on-device: from computer vision, speech and language processing to generative adversarial networks and AlphaZero-like deep reinforcement learning. You'll learn how to use or retrain existing TensorFlow models, build your own models, and develop intelligent mobile apps running those TensorFlow models. You'll learn how to quickly build such apps with step-by-step tutorials and how to avoid many pitfalls in the process with lots of hard-earned troubleshooting tips. What you will learn Classify images with transfer learning Detect objects and their locations Transform pictures with amazing art styles Understand simple speech commands Describe images in natural language Recognize drawing with Convolutional Neural Network and Long Short-Term Memory Predict stock price with Recurrent Neural Network in TensorFlow and Keras Generate and enhance images with generative adversarial networks Build AlphaZero-like mobile game app in TensorFlow and Keras
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值