安装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.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 转换器

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 要在树莓派上安装TensorFlow Lite,您需要按照以下步骤进行操作: 1. 首先,您需要在树莓派上安装Python和pip。如果您还没有安装它们,可以使用以下命令进行安装: sudo apt-get update sudo apt-get install python3-dev python3-pip 2. 接下来,您需要安装TensorFlow LitePython包。可以使用以下命令进行安装: pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1..post1-cp37-cp37m-linux_armv7l.whl 3. 安装完成后,您可以使用以下命令测试TensorFlow Lite是否正常工作: python3 -c "import tflite_runtime.interpreter as tflite" 如果没有出现任何错误,则说明TensorFlow Lite已经成功安装并可以正常工作。 希望这些步骤能够帮助您在树莓派上安装TensorFlow Lite。 ### 回答2: 树莓派是一种小型计算机,广泛用于物联网、教育和娱乐等领域。TensorFlow Lite是Google开发的一种轻量级的机器学习框架,专门用于在移动设备和嵌入式设备上部署机器学习模型。在树莓派上安装TensorFlow Lite可以使其更加智能化,可以用于各种应用场景,如图像识别、自然语言处理等。 以下是在树莓派上安装TensorFlow Lite的步骤: 第一步:更新系统 在安装TensorFlow Lite之前,需要保证树莓派系统为最新版本。可以通过以下命令更新系统: sudo apt-get update sudo apt-get upgrade 第二步:安装Python TensorFlow Lite是基于Python语言开发的,因此需要在树莓派上安装Python。可以通过以下命令安装Python: sudo apt-get install python3-dev python3-pip 第三步:安装TensorFlow Lite安装TensorFlow Lite之前,需要先安装TensorFlow框架。可以通过以下命令安装: pip3 install tensorflow==2.3.0 注意,TensorFlow 2.3.0是适用于树莓派运行的最新版本。如果使用其他版本,可能会遇到不兼容的问题。 安装完成TensorFlow之后,可以通过以下命令安装TensorFlow Lite: pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl 第四步:测试TensorFlow Lite 安装完成后,可以用以下代码测试 TensorFlow Lite 是否正常工作: import tflite_runtime.interpreter as tflite interpreter = tflite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() 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() output_data = interpreter.get_tensor(output_details[0]['index']) print(output_data) model.tflite是你要测试的模型的文件名。如果TensorFlow Lite正常工作,将输出模型的预测结果。 总结: 通过以上步骤,在树莓派上安装 Tensorflow Lite 非常简单。需要注意的是,TensorFlow Lite 不像TensorFlow那样跟大多数操作系统集成,需要从Google的官方网站上下载并安装。但还是很方便快捷的,只要按照上述步骤操作,你就可以在树莓派上使用TensorFlow Lite,部署机器学习模型并让树莓派变得更加智能。 ### 回答3: 树莓派是一个非常强大的小型计算机,既能够实现控制硬件,也可以搭载各种软件来实现复杂的功能。在树莓派中安装TensorFlow Lite可以帮助我们实现各种深度学习任务,如目标检测、语音识别、机器翻译和图像分类等。 首先,我们需要设置树莓派。我们需要一张32GB以上的SD卡和一个读卡器,并下载树莓派的操作系统。可以通过官方网站下载最新版本的树莓派操作系统,并将其烧录到SD卡中。 一旦树莓派系统已经设置好,我们需要对其进行基础设置和连接外部硬件(键盘、鼠标和显示器)。接着,我们需要打开终端并更新操作系统: sudo apt-get update sudo apt-get upgrade 接着,我们需要安装Python3以及pip3: sudo apt-get install python3-dev python3-pip 接下来,我们需要安装TensorFlow Lite安装TensorFlow Lite最简单的方法是使用pip: sudo pip3 install tflite-runtime 然后,我们可以测试是否成功安装TensorFlow Lite: # Python3 程序 import tflite_runtime.interpreter as tflite interpreter = tflite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() input_shape = input_details[0]['shape'] print(input_shape) 以上代码将会测试是否已经成功安装TensorFlow Lite,并打印tflite模型中的输入形状。 总之,在树莓派上安装TensorFlow Lite可以帮助我们实现多种深度学习任务。通过上述步骤,您可以在树莓派上安装TensorFlow Lite。更多详细的教程及示例,请访问TensorFlow官网。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值