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

如何在Python中导入tensorflow lite解释器?,python,tensorflow,raspberry-pi3,pytorch,tensorflow-lite,Python,Tensorflow,Raspberry Pi3,Pytorch,Tensorflow Lite,我正在Raspberry Pi 3b上使用TF lite开发一个Tensorflow嵌入式应用程序,运行Raspbian Stretch。我已经将图形转换为flatbuffer(lite)格式,并在Pi上构建了TFLite静态库。到现在为止,一直都还不错。但是应用程序是Python,似乎没有可用的Python绑定。Tensorflow Lite开发指南()声明“有Python绑定和演示应用程序的计划。”但是/Tensorflow/contrib/Lite/Python/解释器包装器中有包装器代码

我正在Raspberry Pi 3b上使用TF lite开发一个Tensorflow嵌入式应用程序,运行Raspbian Stretch。我已经将图形转换为flatbuffer(lite)格式,并在Pi上构建了TFLite静态库。到现在为止,一直都还不错。但是应用程序是Python,似乎没有可用的Python绑定。Tensorflow Lite开发指南()声明“有Python绑定和演示应用程序的计划。”但是/Tensorflow/contrib/Lite/Python/解释器包装器中有包装器代码,包含所有需要的解释器方法。然而,从Python中调用这一点让我难以理解


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

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

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

关于使用Python中的TensorFlow Lite解释器,下面的示例是从。该代码可在的
主机
分支上找到

使用模型文件中的解释器 下面的示例演示了在提供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)

@纳普,谢谢你的意见。问题是Tensorflow绑定没有在Raspbian中定义。没有这些绑定,就不可能“将tensorflow作为tf导入”。解释器已从contrib模块直接移动到tensorflow(
tf.lite.解释器
,而不是
tf.contrib.lite.解释器
)。文档现在位于,我曾尝试在Raspbian上安装夜间版本的TF,但出现错误,“tensorlflow…在此平台上不受支持”。考虑到谷歌在RPi上的测试是在64位Ubuntu()上进行的,以及其他关于Tensorflow没有在32位目标上运行的评论(),我认为TF没有在32位目标上运行。我不认为这是64位对32位的问题。我猜它与当前Raspbian中使用的Python3.5和3.4pip包类似。也许你可以试试。你是对的,大约64位。看起来PyPi链接现在已经更新了,我可以在普通的32位RPi3上使用pip安装所有Tensorflow。问题已解决。@freedom能否提供使用摄影机馈送而不是单个图像进行对象检测的示例代码?我试图使用它,但我得到的值错误:无法设置张量:维度不匹配