如何将TensorFlow训练的模型移植到Android手机

现有经验贴汇总:

https://blog.csdn.net/masa_fish/article/details/56049710?winzoom=1

https://blog.csdn.net/masa_fish/article/details/54097796

https://blog.csdn.net/guyuealian/article/details/79672257

https://www.w3cschool.cn/tensorflow/tensorflow-zcim2chz.html

https://blog.csdn.net/cxq234843654/article/details/71171293

https://blog.csdn.net/lq126126/article/details/80776744

什么是TensorFlow Lite

TensorFlow Lite是TensorFlow在移动设备上运行机器学习的跨平台解决方案,具有低延迟、运行时库 (runtime library) 极小等特性,此外还有一系列的工具去转换、调试和优化模型。

采用TensorFlow Lite开发的应用优点之一是反应速度非常快,比如开发照片处理app,不需要将照片传输到云端,可以直接在终端上处理。优点之二是可以离线使用,这在网络条件比较差的地区尤为重要。

Tensorflow Lite具有高度可移植性,已经在如下平台成功移植:

Android、iOS

Raspberry PI、及其它Linux SoCs

微处理器(包括没有操作系统,没有POSIX环境的系统)

也能在PC、Mac运行,调试方便

使用TensorFlow Lite的流程

在这里插入图片描述

TensorFlow Lite的优化

相较于TensorFlow,TensorFlow Lite进行了如下优化:

压缩模型:缩小模型体积

量化 (Quantization):TensorFlow模型中包含大量的矩阵,矩阵数值通常采用32bit的float数据类型,量化就是将这些32bit的浮点数采用8bit的byte表示。

CPU ops融合:比如特别为ARM Neon指令集优化

优化的SIMD运算核心
在这里插入图片描述

此外TensorFlow Lite还紧密整合硬件加速器,支持如下几类硬件加速器:

GPUs

Edge-TPUs

NNAPI支持的硬件加速器

在这里插入图片描述

对GPUs的支持状况:

Android上:基于OpenGL的GPU加速

预定2018年第四季度发布binary

可加速MobileNet以及其它图像模型

关于Google打造的Edge TPUs:
在这里插入图片描述

TensorFlow Lite可以通过参数决定runtime library大小。基本的interpreter 80K,所有内建Ops kernel 750KB,相当小。考虑到不同模型可能用到不同的ops,还可以继续优化,比如可以只注册你需要的Ops,这样其余的Ops就不会编译到runtime library中,体积还可以进一步缩减。

开发者的反馈

TensorFlow Lite得到了众多开发者的使用,得到了很多正面的反馈:

跨平台部署

更快的推断 (inference) 速度

更小的runtime library

硬件加速

也收集到了一些改进意见:

TensorFlow Lite更易于使用

增加支持的Ops数量

增强模型优化工具

更多的文档、范例源码…

后面会进一步说明TensorFlow Lite团队对这些问题的改进。

如何使用TensorFlow Lite

在这里插入图片描述

使用Demo App

下载:从https://www.tensorflow.org/mobile/tflite下载一个demo app(iOS/Android)

编译:在您的机器上简单的编译demo apps

运行:运行demo app,并试着修改看看

参考:

https://www.ctolib.com/topics-135082.html

https://www.cnblogs.com/YouXiangLiThon/p/9382700.html

https://blog.csdn.net/ivy_yijing1115/article/details/79097263

发布了156 篇原创文章 · 获赞 47 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_38769551/article/details/105207296