安装TensorFlow Lite runtime

本文介绍了如何仅安装TensorFlowLite的解释器来运行Python中的推断,避免完整TensorFlow库的庞大占用。通过安装tflite_runtime包,可以使用tf.lite.Interpreter运行.tflite模型。提供了适用于不同平台和Python版本的安装链接,并给出了修改代码以使用tflite_runtime模块的示例。
摘要由CSDN通过智能技术生成

原文链接:

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.5https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_armv7l.whl
3.6https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_armv7l.whl
3.7https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
3.8https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_armv7l.whl
Linux (ARM 64)3.5https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_aarch64.whl
3.6https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_aarch64.whl
3.7https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_aarch64.whl
3.8https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_aarch64.whl
Linux (x86-64)3.5https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_x86_64.whl
3.6https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl
3.7https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl
3.8https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_x86_64.whl
macOS 10.143.5https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-macosx_10_14_x86_64.whl
3.6https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-macosx_10_14_x86_64.whl
3.7https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
Windows 103.5https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-win_amd64.whl
3.6https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-win_amd64.whl
3.7https://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 转换器

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值