【TensorFlow 精简版】TensorFlow Lite

目录

[一 TensorFlow Lite简介](#一 TensorFlow Lite简介)

[二 开发](#二 开发)

[三 开始使用](#三 开始使用)


一 TensorFlow Lite简介

TensorFlow Lite 是一组工具,可帮助开发者在移动设备、嵌入式设备和 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

bash 复制代码
python3 -m pip install tflite-runtime

③ 使用tflite-runtime进行推理

python 复制代码
import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path=args.model_file)

或者

bash 复制代码
import tflite_runtime.interpreter as tflite

interpreter = tflite.Interpreter(model_path=args.model_file)

模型转换成tflie可参考:模型转换

  • 完整的推理代码
python 复制代码
import numpy as np
import tensorflow as tf
# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="ResNet50_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), dtype=np.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
  • 运行程序的输出
bash 复制代码
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
(1, 1000)
0.00015664824
0.0007171879
相关推荐
葫三生2 分钟前
如何评价《论三生原理》在科技界的地位?
人工智能·算法·机器学习·数学建模·量子计算
m0_751336391 小时前
突破性进展:超短等离子体脉冲实现单电子量子干涉,为飞行量子比特奠定基础
人工智能·深度学习·量子计算·材料科学·光子器件·光子学·无线电电子
美狐美颜sdk4 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
DeepSeek-大模型系统教程5 小时前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习
郭庆汝5 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
小雷FansUnion7 小时前
深入理解MCP架构:智能服务编排、上下文管理与动态路由实战
人工智能·架构·大模型·mcp
资讯分享周7 小时前
扣子空间PPT生产力升级:AI智能生成与多模态创作新时代
人工智能·powerpoint
思则变8 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
叶子爱分享8 小时前
计算机视觉与图像处理的关系
图像处理·人工智能·计算机视觉
鱼摆摆拜拜8 小时前
第 3 章:神经网络如何学习
人工智能·神经网络·学习