Google最近发布了Tensorflow Lite,并且提供了demo,虽然该demo可以使用bazel build –cxxopt=’–std=c++11’ //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo命令成功编译出来,但是文档中并没有提及如何纯粹的编译出动态库,参考之前的一篇文章《当 Android 开发者遇见 TensorFlow》,这篇文章就简单介绍一下如何编译动态库

clone 代码

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

修改TensorFlow项目根下的WROKSPACE文件

将以下代码反注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 = "26.0.1",
# Replace with path to Android SDK on your system
path = "/Users/lizhangqu/AndroidSDK",
)
#
android_ndk_repository(
name="androidndk",
path="/Users/lizhangqu/AndroidNDK/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)

然后修改android_sdk_repository中的path为自己电脑中的android sdk目录,修改android_ndk_repository中的path为自己电脑的android ndk目录。

值得注意的是,ndk的版本,官方建议使用大于r14的版本,下载地址android-ndk-r14b-darwin-x86_64.zip

编译

确保必要的工具已经安装,如bazel

1
2
3
4
bazel build --cxxopt='--std=c++11' //tensorflow/contrib/lite/java:tensorflowlite \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=armeabi

编译其他ABI请修改cpu参数,分别为

1
2
3
4
5
6
7
--cpu=armeabi
--cpu=armeabi-v7a
--cpu=arm64-v8a
--cpu=mips
--cpu=mips64
--cpu=x86
--cpu=x86_64

产物位于

1
2
bazel-bin/tensorflow/contrib/lite/java/libtensorflowlite_jni.so
bazel-bin/tensorflow/contrib/lite/java/libtensorflowlitelib.jar

注意编译mips和mips64的时候,需要将构建脚本稍微修改一下,删除一部分代码,否则会报错

找到/tensorflow/contrib/lite/build_def.bzl文件,找到如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def tflite_linkopts_unstripped():
"""Defines linker flags to reduce size of TFLite binary.
These are useful when trying to investigate the relative size of the
symbols in TFLite.
Returns:
a select object with proper linkopts
"""
return select({
"//tensorflow:android": [
"-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj.
"-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export.
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],
"//tensorflow/contrib/lite:mips": [],
"//tensorflow/contrib/lite:mips64": [],
"//conditions:default": [
"-Wl,--icf=all", # Identical code folding.
],
})
def tflite_jni_linkopts_unstripped():
"""Defines linker flags to reduce size of TFLite binary with JNI.
These are useful when trying to investigate the relative size of the
symbols in TFLite.
Returns:
a select object with proper linkopts
"""
return select({
"//tensorflow:android": [
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],
"//tensorflow/contrib/lite:mips": [],
"//tensorflow/contrib/lite:mips64": [],
"//conditions:default": [
"-Wl,--icf=all", # Identical code folding.
],
})

将上面代码中的下面这段删除

1
2
3
4
5
6
"//tensorflow:android": [
"-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj.
"-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export.
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],

以及这一段也删除

1
2
3
4
"//tensorflow:android": [
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],

临时删除后,编译完mips和mips64还原即可,不然会报如下错误

1
2
3
4
5
6
7
8
9
10
ERROR: /Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so:
//tensorflow/contrib/lite:mips
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:
/Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so:
//tensorflow/contrib/lite:mips
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
1
2
3
4
5
6
7
8
9
10
ERROR: /Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so:
//tensorflow/contrib/lite:mips64
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:
/Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so:
//tensorflow/contrib/lite:mips64
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
http://fucknmb.com/2017/11/17/Tensorflow-Lite%E7%BC%96%E8%AF%91/

Tensorflow Lite 编译相关推荐

  1. TensorFlow Lite编译Android so库

    工作中最近用到了Google TensorFlow技术,Android端对相机预览图进行预处理,并加载tflite模型并推测运行结果.期间需要用到lite的so库等相关sdk,并自己实践编译了一下. ...

  2. 在ARM板子上把玩Tensorflow Lite

    前几天Google的IO大会上发布的ML Kit,ML Kit为端上部署深度学习模型提供了一套完整的解决方案,本地运行.云端都支持.里面本地部署用到的就是Tensorflow lite. Tensor ...

  3. 【ARMNN/编译】tensorflow lite版本

    说在前面 PC环境:ubuntu16.04 目标平台:armv8-A 编译方式:交叉编译 参考:为Tensorflow.Tensorflow lite配置Arm NN SDK编译环境.arm deve ...

  4. 如何查看tensorflow lite toco编译时的各个参数

    https://github.com/tensorflow/models/issues/8148#issuecomment-641028423 想生成tflite格式的模型,用于安卓端做推理的时候,不 ...

  5. TensorFlow Lite:TensorFlow在移动设备与嵌入式设备上的轻量级跨平台解决方案 | Google 开发者大会 2018...

    Google 开发者大会 (Google Developer Days,简称 GDD) 是展示 Google 最新开发者产品和平台的全球盛会,旨在帮助你快速开发优质应用,发展和留住活跃用户群,充分利用 ...

  6. Google正式发布TensorFlow Lite预览版,针对移动/嵌入设备的轻量级解决方案

    来源:AI科技评论 概要:日前,谷歌正式发布 TensorFlow Lite 开发者预览版,这是针对移动和嵌入式设备的轻量级解决方案. 日前,谷歌正式发布 TensorFlow Lite 开发者预览版 ...

  7. 移动端目标识别(1)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之TensorFlow Lite简介...

    平时工作就是做深度学习,但是深度学习没有落地就是比较虚,目前在移动端或嵌入式端应用的比较实际,也了解到目前主要有 caffe2,腾讯ncnn,tensorflow,因为工作用tensorflow比较多 ...

  8. [译]GPU加持,TensorFlow Lite更快了

    虽然手机的发展日新月异,我们更换手机的频率也加快,但不可否认的是,手机性能相比PC还是有很大的差距.对于机器学习来说,我们可以在电脑上训练模型,在手机上应用模型,但某些复杂的模型,在手机上进行推断,依 ...

  9. Tensorflow Lite之编译生成tflite文件

    这是tensorflow生成的各种模型文件: GraphDef (.pb) - a protobuf that represents the TensorFlow training and or co ...

最新文章

  1. 如果要存 IP 地址,用什么数据类型比较好?大部人都会答错!
  2. [Spring 深度解析]第6章 Spring的IoC容器系列
  3. 通过checkbox选择以逗号拼接删除字符串
  4. win8: hello gril
  5. 计算机如何搜索相关文字,搜索引擎:“请输入你要搜索的内容”|你是如何使用搜索引擎的呢?...
  6. redis为什么这么火该怎么用
  7. ArcGIS实验教程——实验三十七:基于ArcGIS的太阳辐射分析案例教程
  8. java 导出excel 例子_java导出Excel例子
  9. 前端 页面无刷新方案一
  10. 教你写一个弹幕库,确定不了解一下?
  11. cognos java_cognos与java结合 ?急!急!急!
  12. [NOIP2013]记数问题
  13. Photoshop画小项目原型图设计
  14. mysql 禁用日志_MYSQL禁用生成日志文件mysql
  15. 概率统计Python计算:学生分布分位点计算
  16. 超实用,一口气学会 Centos/Docker/Nginx/Node/Jenkins 等基础操作
  17. android 音频合并
  18. wiredtiger java_mongodb数据库损坏,丢失WiredTIger.wt等meta文件,通过collection*.wt恢复数据...
  19. 【内存】物理内存和虚拟内存
  20. Windows快捷键操作

热门文章

  1. Yii Model中添加默认搜索条件
  2. (转)工作了一个星期各位一定累了吧,那我们一起来表单验证一番吧!
  3. 全新的互动广告牌,待遇男女有别
  4. WPF系列(一)第一个WPF应用程序!
  5. Git bash:初步入门(1)
  6. Linux下的图形界面编程
  7. html传递json中文乱码,解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码...
  8. 【Leetcode】大神总结的链表常见面试问题
  9. [云炬创业基础笔记]第九章企业的法律形态测试1
  10. 科大星云诗社动态20210226