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

bju*_*aub 7 python tensorflow raspberry-pi3 pytorch tensorflow-lite

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

我已经生成了SWIG包装器,但是构建步骤失败并出现许多错误。没有readme.md描述解释器包装器的状态。因此,我想知道包装器是否为其他人工作,我应该坚持还是从根本上破坏它,而应该在其他地方使用(PyTorch)?有没有人找到Pi3的TFLite Python绑定的路径?

Nup*_*arg 7

关于使用Python的TensorFlow Lite解释器,以下示例是从文档中复制的。该代码可在TensorFlow GitHubmaster分支上获得

使用模型文件中的解释器

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

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)
Run Code Online (Sandbox Code Playgroud)

  • 解释器已从 contrib 模块直接移至 tensorflow(`tf.lite.Interpreter` 而不是 `tf.contrib.lite.Interpreter`)。该文档现在位于 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/convert/python_api.md (2认同)

小智 6

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

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