tensorflow的android demo环境搭建

1. 安装tensorflow

官方文档地址:https://www.tensorflow.org/install/ 。

2. 安装构建工具bazel

官方文档地址:https://bazel.build/versions/master/docs/install.html 。

安装JDK 8,并添加APT库,然后安装bazel

sudo apt-get install openjdk-8-jdkecho "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -sudo apt-get update && sudo apt-get install bazel

3. 下载源码

使用Git下载代码。

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

4. 配置本地环境

在...\tensorflow\tensorflow\examples\android目录下。

1). 修改WORKSPACE文件

先修改/tensorflow目录下的WORKSPACE文件,找到如下一段代码,对应配置好你的sdk版本以及位置,还有ndk的版本和位置,并且去掉注释:

  # Uncomment and update the paths in these entries to build the Android demo.
  #android_sdk_repository(
  #    name = "androidsdk",
  #    api_level = 23,
  #    # Ensure that you have the build_tools_version below installed in the 
  #    # SDK manager as it updates periodically.
  #    build_tools_version = "25.0.2",
  #    # Replace with path to Android SDK on your system
  #    path = "<PATH_TO_SDK>",
  #)
  #
  # Android NDK r12b is recommended (higher may cause issues with Bazel)
  #android_ndk_repository(
  #    name="androidndk",
  #    path="<PATH_TO_NDK>",
  #    # This needs to be 14 or higher to compile TensorFlow. 
  #    # Note that the NDK version is not the API level.
  #    api_level=14)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

比如:

android_sdk_repository(
    name = "androidsdk",
    # 设置成你安装sdk的最高版本,对应修改manifest.xml以及gradle的target
    api_level = 23,
    # Ensure that you have the build_tools_version below installed in the 
    # SDK manager as it updates periodically.
    build_tools_version = "25.0.3",
    # Replace with path to Android SDK on your system
    path = "/home/cxq/android-sdk-linux",
)

# 推荐使用r12版本,更高的版本可能会造成bazel出错
android_ndk_repository(
    name="androidndk",
    path="/home/cxq/android-sdk-linux/ndk-bundle",
    # This needs to be 14 or higher to compile TensorFlow. 
    # Note that the NDK version is not the API level.
    api_level=14)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注意: 
1. api_level需要设置成你本地sdk最高的版本,推荐23及以上,在android:build.gradle 以及AndroidManifest.xml也对应修改targetSdkVersion 
2. ndk的版本,推荐使用r12,否则bazel编译的时候会报错。

2. 构建

进入到../tensorflow目录下,输入以下命令:

bazel build -c opt //tensorflow/examples/android:tensorflow_demo
 
 
  • 1
  • 1

如果有报protocol buffers的相关错误,运行git submodule update --init命令,并且确保WORKSPACE文件配置对了

3. 安装apk

开启手机的调试模式,运行:

adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
 
 
  • 1
  • 1
4. 使用Android Studio

可以在Android Studio中直接打开tensorflow/examples/android,但是需要配置好你的gradle、sdk、ndk 
1. gradle必须要在3.3以上版本 
2. build_tool_version需要指定在25以上 
3. 配置好你的ndk 
4. 在android:build.gradle 中配置好你的bazel路径,默认的是如下的:

def bazelLocation = '/usr/local/bin/bazel'
 
 
  • 1
  • 1

但是在该路径下并没有bazel,因此会报如下错误:

Error:Execution failed for task ':buildNativeBazel'.
A problem occurred starting process 'command '/usr/local/bin/bazel''
 
 
  • 1
  • 2
  • 1
  • 2

此时只需要把路径改成你的bazel路径就好,比如:

def bazelLocation = '/usr/bin/bazel'
 
 
  • 1
  • 1

这样就能直接在AS中使用run运行demo了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值