Ubuntu TensorFlow 源码 Android Demo的编译运行

时间:2022-02-26 11:32:53

Ubuntu TensorFlow 源码 Android Demo的编译运行

一、 安装 Android 的SDK和NDK

SDK 配置

A:下载

国内下载地址选最新的:

SDK:

https://developer.android.google.cn/studio/index.html

也可以下载一个旧的再update

NDK:

https://developer.android.google.cn/ndk/downloads/index.html

B:解压 配置环境变量

sudo tar -zxvf android-sdk_r26.0.1-linux.tgz

sudo unzip android-ndk-r14b-linux-x86_64.zip

SDK更新(如果是最新的无需更新)

cd ~/sdk/android-sdk-linux

sudo tools/android update sdk --no-ui

C:配置环境变量

打开.bashrc 最后添加一行(主要更改目录)

PATH="/home/dyufei/sdk/android-sdk-linux/tools:/home/dyufei/sdk/android-sdk-linux/platform-tools:/home/dyufei/sdk/android-sdk-linux/build-tools:${PATH}"

D: 使环境变量生效

source .bashrc

二、 TensorFlow 源码下载、配置

源码的下载、编译、安装请参考: http://www.cnblogs.com/dyufei/p/8027517.html

1)下载tensorflow 源码

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

2)配置TensorFlow 的WORKSPACE

在 TensorFlow 的根目录下,找到WORKSPACE,将注释了的SDK和NDK的部分打开,修改成自己的SDK和JDK的目录和对应的版本

#Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 25,
# Ensure that you have the build_tools_version below installed in the
# SDK manager as it updates periodically.
build_tools_version = "27.0.2",
# Replace with path to Android SDK on your system
path = "/home/dyufei/sdk/android-sdk-linux/",
) android_ndk_repository(
name="androidndk",
path="/home/dyufei/sdk/android-ndk-r14b/",
# 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=14)

3)编译Android Demo APK

在 TensorFlow 的根目录下,执行下面命令编译:

bazel build //tensorflow/examples/android:tensorflow_demo

编译完成后生成三个文件:

(在bazel-bin/tensorflow/examples/android/目录下)

Target //tensorflow/examples/android:tensorflow_demo up-to-date:

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: 201.760s, Critical Path: 69.16s

INFO: Build completed successfully, 784 total actions

4)在手机上运行

好了,大功告成!

将tensorflow_demo.apk 安装到手机上,看看效果吧