安装TensorFlow Lite runtime

原文链接:

https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python

如果项目无需安装tensorflow包,而只需要其解释类功能,比如Tensorflow Lite API,则可以只安装tflite_runtime 包即可。

安装 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
如何查看你的Raspberry Pi是ARM32还是64?
运行uname -a命令。
如果出现arm7,则为ARM32,如果出现arm8及以上,则是ARM64。
平台 Python 网址
Linux (ARM 32) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_armv7l.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_armv7l.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
3.8 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_armv7l.whl
Linux (ARM 64) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_aarch64.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_aarch64.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_aarch64.whl
3.8 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_aarch64.whl
Linux (x86-64) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_x86_64.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl
3.8 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_x86_64.whl
macOS 10.14 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-macosx_10_14_x86_64.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-macosx_10_14_x86_64.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
Windows 10 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-win_amd64.whl
3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-win_amd64.whl
3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-win_amd64.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 转换器

猜你喜欢

转载自blog.csdn.net/figosoar/article/details/120973064