TensorFlow Lite for Android示例
一、TensorFlow Lite
TensorFlow Lite 是用于移动设备和嵌入式设备的轻量级解决方案。TensorFlow Lite 支持 Android、iOS 甚至树莓派等多种平台。
二、tflite格式
TensorFlow 生成的模型是无法直接给移动端使用的,需要离线转换成.tflite文件格式。
tflite 存储格式是 flatbuffers。
FlatBuffers 是由Google开源的一个免费软件库,用于实现序列化格式。它类似于Protocol Buffers、Thrift、Apache Avro。
因此,如果要给移动端使用的话,必须把 TensorFlow 训练好的 protobuf 模型文件转换成 FlatBuffers 格式。官方提供了 toco 来实现模型格式的转换。
三、API
TensorFlow Lite 提供了 C ++ 和 Java 两种类型的 API。无论哪种 API 都需要加载模型和运行模型。
而 TensorFlow Lite 的 Java API 使用了 Interpreter 类(解释器)来完成加载模型和运行模型的任务。后面的例子会看到如何使用 Interpreter。
四、TensorFlow Lite实现手写数字识别
下面的 demo 中已经包含了 mnist.tflite 模型文件。(如果没有的话,需要自己训练保存成pb文件,再转换成tflite 格式)
// The tensorflow lite file
private lateinit var tflite: Interpreter // Input byte buffer
private lateinit var inputBuffer: ByteBuffer // Output array [batch_size, 10]
private lateinit var mnistOutput: Array<FloatArray> init { try {
tflite = Interpreter(loadModelFile(activity)) inputBuffer = ByteBuffer.allocateDirect(
BYTE_SIZE_OF_FLOAT * DIM_BATCH_SIZE * DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE)
inputBuffer.order(ByteOrder.nativeOrder())
mnistOutput = Array(DIM_BATCH_SIZE) { FloatArray(NUMBER_LENGTH) }
Log.d(TAG, "Created a Tensorflow Lite MNIST Classifier.")
} catch (e: IOException) {
Log.e(TAG, "IOException loading the tflite file failed.")
} }
从 asserts 文件中加载 mnist.tflite 模型:
/**
* Load the model file from the assets folder
*/
@Throws(IOException::class)
private fun loadModelFile(activity: Activity): MappedByteBuffer { val fileDescriptor = activity.assets.openFd(MODEL_PATH)
val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
val fileChannel = inputStream.channel
val startOffset = fileDescriptor.startOffset
val declaredLength = fileDescriptor.declaredLength
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
}
真正识别手写数字是在 classify() 方法:
val digit = mnistClassifier.classify(Bitmap.createScaledBitmap(paintView.bitmap, PIXEL_WIDTH, PIXEL_WIDTH, false))
classify() 方法包含了预处理用于初始化 inputBuffer、运行 mnist 模型、识别出数字。
/**
* Classifies the number with the mnist model.
*
* @param bitmap
* @return the identified number
*/
fun classify(bitmap: Bitmap): Int { if (tflite == null) {
Log.e(TAG, "Image classifier has not been initialized; Skipped.")
} preProcess(bitmap)
runModel()
return postProcess()
} /**
* Converts it into the Byte Buffer to feed into the model
*
* @param bitmap
*/
private fun preProcess(bitmap: Bitmap?) { if (bitmap == null || inputBuffer == null) {
return
} // Reset the image data
inputBuffer.rewind() val width = bitmap.width
val height = bitmap.height // The bitmap shape should be 28 x 28
val pixels = IntArray(width * height)
bitmap.getPixels(pixels, 0, width, 0, 0, width, height) for (i in pixels.indices) {
// Set 0 for white and 255 for black pixels
val pixel = pixels[i]
// The color of the input is black so the blue channel will be 0xFF.
val channel = pixel and 0xff
inputBuffer.putFloat((0xff - channel).toFloat())
}
} /**
* Run the TFLite model
*/
private fun runModel() = tflite.run(inputBuffer, mnistOutput) /**
* Go through the output and find the number that was identified.
*
* @return the number that was identified (returns -1 if one wasn't found)
*/
private fun postProcess(): Int { for (i in 0 until mnistOutput[0].size) {
val value = mnistOutput[0][i]
if (value == 1f) {
return i
}
} return -1
}
对于 Android 有一个地方需要注意,必须在 app 模块的 build.gradle 中添加如下的语句,否则无法加载模型。
android {
......
aaptOptions {
noCompress "tflite"
}
}
效果:

五、总结
本文 demo 的 github 地址:https://github.com/fengzhizi715/TFLite-MnistDemo
当然,也可以跑一下官方的例子:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/android/app
虽然准确度都不咋地。。。
更多有趣的TensorFlow Lite示例:https://www.tensorflow.org/lite/examples/
TensorFlow Lite for Android示例的更多相关文章
- TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后
Introduction to TensorFlow Lite TensorFlow Lite is TensorFlow’s lightweight solution for mobile and ...
- android NDK 神经网络API——是给tensorflow lite调用的底层API,应用开发者使用tensorflow lite即可
eural Networks API In this document show more Understanding the Neural Networks API Runtime Neural N ...
- object detection模型转换成TensorFlow Lite,在Android应用
环境 tensorflow = 1.12.0 bazel = 0.18.1 ubuntu = 16.04 python = 3.6.2 安装 bazel (0.18.1) 如果tensorflow是1 ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...
- 移动端目标识别(1)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之TensorFlow Lite简介
平时工作就是做深度学习,但是深度学习没有落地就是比较虚,目前在移动端或嵌入式端应用的比较实际,也了解到目前主要有 caffe2,腾讯ncnn,tensorflow,因为工作用tensorflow比较多 ...
- 在 Flutter 中使用 TensorFlow Lite 插件实现文字分类
如果您希望能有一种简单.高效且灵活的方式把 TensorFlow 模型集成到 Flutter 应用里,那请您一定不要错过我们今天介绍的这个全新插件 tflite_flutter.这个插件的开发者是 G ...
- Tensorflow Lite从入门到精通
TensorFlow Lite 是 TensorFlow 在移动和 IoT 等边缘设备端的解决方案,提供了 Java.Python 和 C++ API 库,可以运行在 Android.iOS 和 Ra ...
- 谷歌发布 TensorFlow Lite [官方网站,文档]
机器学习社区:http://tensorflow123.com/ 简介 TensorFlow Lite TensorFlow Lite 是 TensorFlow 针对移动和嵌入式设备的轻量级解决方案. ...
- 移动端目标识别(2)——使用TENSORFLOW LITE将TENSORFLOW模型部署到移动端(SSD)之TF Lite Developer Guide
TF Lite开发人员指南 目录: 1 选择一个模型 使用一个预训练模型 使用自己的数据集重新训练inception-V3,MovileNet 训练自己的模型 2 转换模型格式 转换tf.GraphD ...
随机推荐
- 用户增长模型AARRR模型
用户增长模型AARRR模型
- Qt Quick 事件处理之鼠标、键盘、定时
一.鼠标事件 MouseArea 对象可以附加到一个 item 上供 item 处理鼠标事件,它本身是一个不可见的 item .在其内部,可以直接引用它所附着的对象的属性和方法.你可以将 MouseA ...
- Maven依赖以及项目创建
目录: 1. Maven依赖.Eclipse中使用Maven.生命周期 1.1 Maven依赖 1.2 Eclipse中使用Maven 2. 依赖排除.通过Maven整合多个Maven 2.1 依赖排 ...
- php 500报错解决方案
php 500报错解决方案 1 先看nginx error.log 指定的错误日记文件路径 找到这个日记文件看 里面信息 2 再看 php-fpm.conf 里面指定的PHP错误日记的路径 具体如下& ...
- 在Mu-kittenbot中使用Robotbit固件
首先,先下载安装支持robotbit扩展板的Mu: http://cdn.kittenbot.cn/mu/mu-kittenbot.exe 标准的3针插口,信号,正电,负电,可接市面的arduino模 ...
- GitHub的高级搜索功能
1. 首先,提供Github高级搜索帮助页面https://help.github.com/categories/search/ 2. 搜索语法https://help.github.com/ ...
- 『You Are Given a Tree 整体分治 树形dp』
You Are Given a Tree Description A tree is an undirected graph with exactly one simple path between ...
- JS操作摄像头
<script src="javascript/jquery-1.9.1.min.js"></script> <fieldset> <le ...
- pom文件语法无误却报红叉(Unknown error)的可能原因
本文链接:https://blog.csdn.net/Little_Stars/article/details/94553090可能原因及解决办法: >> 缓存问题,只要 “Maven - ...
- php-fpm解读-进程管理的三种模式
php-fpm进程管理一共有三种模式:ondemand.static.dynamic,我们可以在同一个fpm的master配置三种模式,看下图1.php-fpm的工作模式和nginx类似,都是一个ma ...