python - 如何在 Python 中导入 tensorflow lite 解释器?

标签 python tensorflow raspberry-pi3 pytorch tensorflow-lite

我正在 Raspberry Pi 3b 上使用 TF lite 开发一个 Tensorflow 嵌入式应用程序,运行 Raspbian Stretch。我已将图表转换为 FlatBuffers (精简版)格式,并在 Pi 上原生构建了 TFLite 静态库。到目前为止,一切都很好。但是应用程序是 Python,似乎没有可用的 Python 绑定(bind)。 Tensorflow Lite 开发指南 (https://www.tensorflow.org/mobile/tflite/devguide) 指出“有 Python 绑定(bind)和演示应用程序的计划。”然而/tensorflow/contrib/lite/python/interpreter_wrapper 中有包装器代码,其中包含所有需要的解释器方法。然而,我无法从 Python 中调用它。

我已经生成了一个 SWIG 包装器,但构建步骤失败并出现许多错误。没有描述 interpreter_wrapper 状态的 readme.md。所以,我想知道包装器是否对其他人有用,我应该坚持还是它从根本上被破坏了,我应该去别处看看(PyTorch)?有没有人找到 Pi3 的 TFLite Python 绑定(bind)路径?

最佳答案

关于从 Python 使用 TensorFlow Lite 解释器,下面的示例是从 documentation 复制的.该代码在 TensorFlow GitHubmaster 分支上可用。 .

使用模型文件中的解释器

以下示例展示了在提供 TensorFlow Lite FlatBuffer 文件时如何使用 TensorFlow Lite Python 解释器。该示例还演示了如何对随机输入数据进行推理。在 Python 终端中运行 help(tf.contrib.lite.Interpreter) 以获得有关解释器的详细文档。

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test 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()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

关于python - 如何在 Python 中导入 tensorflow lite 解释器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50902067/

相关文章:

audio - Windows IoT核心版RaspPi Walkie Talkie

python - Kivy USB 键盘 double 字母

java - 从 python 脚本设置环境变量

python - 如何在 Python 中使用索引列表对 np.array 进行索引

python - Pandas 等价于 SQL CROSS JOIN(笛卡尔积)

python - 将相同的字符串附加到 Python 中的字符串列表

python - tf.Estimator 训练后检索张量 (Numpy) 值

python - tf.train.get_global_step() 的值与当前训练步骤之间的差异

python - Tensorflow 2.3.0 未检测到 GPU

在 RPi3 上创建从 ARM 到 VideoCore 的 C 缓冲区