建设企业网站的人员组成,莱芜网站建设费用,台州做优化,天津北京网站建设公司目录
一 TensorFlow Lite简介
二 开发
三 开始使用 一 TensorFlow Lite简介 TensorFlow Lite 是一组工具#xff0c;可帮助开发者在移动设备、嵌入式设备和 loT 设备上运行模型#xff0c;以便实现设备端机器学习。 针对设备端的机器学习进行的优化#xff1a;
① 延时可帮助开发者在移动设备、嵌入式设备和 loT 设备上运行模型以便实现设备端机器学习。 针对设备端的机器学习进行的优化
① 延时数据无需往返服务器
② 隐私没有任何个人数据离开设备
③ 连接性无需连接互联网
④ 大小缩减了模型和二进制文件的大小
⑤ 功耗高效推断且无需网络连接。
支持多种平台涵盖Android 和 IOS设备、嵌入式Linux 和微控制器。支持多种语言包括 Java、Swift、Objective-C、C 和 Python。高性能支持硬件的加速和优化模型。提供多种平台上的常见机器学习任务的端到端范例图像分类、目标检测、姿势估计、问题回答、文本分类等
二 开发
TensorFlow Lite 可在计算和内存资源有限的设备上高效地运行原因是可缩减大小代码占用的空间较小以及提高推断的速度可直接访问数据无需执行额外的解析/解压缩步骤等。
1 创建TensorFlow Lite模型
可以通过以下的方式生成 TensorFlow Lite 模型
① 使用现有的 TensorFlow Lite 模型
模型可能包含元数据也可能不含元数据。
TensorFlow Lite 元数据为模型描述提供了标准。元数据是与模型功能及其输入/输出信息有关的重要信息来源。具有元数据格式的模型如下图所示 ② 创建 TensorFlow Lite 模型
使用 TensorFlow Lite Model Maker利用自定义数据集创建模型。
默认情况下所有模型都包含元数据。
③ 将 TensorFlow 模型转换为 TensorFlow Lite 模型
可以使用 TensorFlow Lite Converter 将 TensorFlow 模型转换为 TensorFlow Lite 模型。
在转换的过程中还可以应用量化等优化措施以缩减模型的大小和缩短延时并最大限度降低或避免准确率的损失。默认情况下所有模型都不含元数据。
2 推理推断
TensorFlow Lite 模型在设备上的执行即预测的过程。
可以通过以下方式运行推断具体取决于模型类型
① 不含元数据的模型
使用 TensorFlow Lite Interpreter API即可。
在多种平台和语言中均受支持。
② 包含元数据的模型
使用 TensorFlow Lite Task 库以利用开箱即用的 API也可以使用 TensorFlow Lite Support 库构建自定义的推断流水线。
三 开始使用
根据目标设备的不同可以参考不同的指南。
① 使用 Python 快速入门基于 Linux 的设备
如果需要使用Python执行TensorFlow Lite模型可以仅安装TensorFlow Lite解释器将这种简化的Python软件包称为tflite_runtime无需安装所有TensorFlow软件包。如果只想执行.tflite模型并且避免因使用大型TensorFlow库而浪费磁盘空间的时候小型软件包tflite_runtime 软件包是首选。
如果是需要访问其他 Python API例如 TensorFlow Lite Converter的时候则必须安装完整的 TensorFlow 软件包。
tflite_runtime 软件包是整个tensorflow软件包的小部分并且包括使用 TensorFlow Lite运行推断所需的最少代码主要是 Interpreter Python类。
② 安装tflite-runtime
python3 -m pip install tflite-runtime
③ 使用tflite-runtime进行推理
import tensorflow as tfinterpreter tf.lite.Interpreter(model_pathargs.model_file)
或者
import tflite_runtime.interpreter as tfliteinterpreter tflite.Interpreter(model_pathargs.model_file)
模型转换成tflie可参考模型转换 完整的推理代码
import numpy as np
import tensorflow as tf
# Load the TFLite model and allocate tensors.
interpreter tf.lite.Interpreter(model_pathResNet50_fp32.tflite)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details interpreter.get_input_details()
output_details interpreter.get_output_details()
# Test the model on random input data.
input_shape input_details[0][shape]
input_data np.array(np.random.random_sample(input_shape), dtypenp.float32)
interpreter.set_tensor(input_details[0][index], input_data)
interpreter.invoke()
# The function get_tensor() returns a copy of the tensor data.
# Use tensor() in order to get a pointer to the tensor.
output_data interpreter.get_tensor(output_details[0][index])
print(output_data.shape)
print(output_data[0][0])
print(output_data[0][999])
pass运行程序的输出
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
(1, 1000)
0.00015664824
0.0007171879