对于基于 Linux 的嵌入式设备(例如 Raspberry Pi 和使用 Edge TPU 的 Coral 设备),非常适合将 TensorFlow Lite 与 Python 结合使用。
本页介绍如何在几分钟内学会开始使用 Python 运行 TensorFlow Lite 模型。您只需要一个已转换为 TensorFlow Lite 的 TensorFlow 模型。(如果还没有转换的模型,您可以使用随下面链接的示例提供的模型进行实验。)
安装 TensorFlow Lite 解释器
要使用 Python 快速运行 TensorFlow Lite 模型,您只需安装 TensorFlow Lite 解释器,而不需要安装所有 TensorFlow 软件包。
只包含解释器的软件包是完整 TensorFlow 软件包的一小部分,其中只包含使用 TensorFlow Lite 运行推断所需要的最少代码——仅包含 tf.lite.Interpreter
Python 类。如果您只想执行 .tflite
模型,而不希望庞大的 TensorFlow 库占用磁盘空间,那么这个小软件包是最理想的选择。
注:如果您需要访问其他 Python API(如 TensorFlow Lite 转换器),则必须安装完整 TensorFlow 软件包。
要安装,请运行 pip3 install
,并向其传递下表中适当的 Python wheel 网址。
例如,如果是运行 Raspbian Buster(具有 Python 3.7)的 Raspberry Pi,请使用以下命令安装 Python wheel:
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
使用 tflite_runtime 运行推理
为了将只包含解释器的软件包与完整 TensorFlow 软件包区分开(如果您愿意,可以同时安装两者), Python 模块在上述 wheel 中提供了命名的 tflite_runtime
。
因此,不要从 tensorflow
模块导入 Interpreter
模块,您需要从 tflite_runtime
导入。
例如,安装上述软件包后,如果复制并运行 label_image.py
文件,(可能)会失败,因为您没有安装 tensorflow
库。要解决此问题,请编辑该文件中的下面一行:
import tensorflow as tf
将其改成:
import tflite_runtime.interpreter as tflite
然后更改下面一行:
interpreter = tf.lite.Interpreter(model_path=args.model_file)
将其改成:
interpreter = tflite.Interpreter(model_path=args.model_file)
现在,重新运行 label_image.py
。就是这样!您现在执行的正是 TensorFlow Lite 模型。
了解详情
有关 Interpreter
API 的更多详细信息,请阅读在 Python 中加载和运行模型。
如果您有 Raspberry Pi,请尝试运行 classify_picamera.py 示例,使用 Pi Camera 和 TensorFlow Lite 执行图像分类。
如果您使用 Coral ML 加速器,请查看 GitHub 上的 Coral 示例。
要将其他 TensorFlow 模型转换为 TensorFlow Lite,请阅读 TensorFlow Lite 转换器。