11

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

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

4

2 回答 2

12

关于在 Python 中使用 TensorFlow Lite 解释器,下面的示例是从文档中复制而来的。该代码可在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)
于 2018-06-29T01:32:20.400 回答
6

我能够在运行 Ubuntu 的 x86 和运行 Debian 的 ARM64 板上编写 python 脚本来执行分类1、对象检测(使用 SSD MobilenetV{1,2} 测试)2和图像语义分割3 。

  • 如何为 TF Lite 代码构建 Python 绑定:使用最近的 TensorFlow 主分支构建 pip 并安装它(是的,那些绑定在 TF 1.8 中。但是,我不知道为什么没有安装它们)。有关如何构建和安装 TensorFlow pip 包的信息,请参见4 。
于 2018-06-21T06:26:34.550 回答