Tensorflow Lite Python 绑定 Raspberry Pi 3B+

Tensorflow Lite Python Binding on Raspberry Pi 3B+

我正在尝试使用 Tensorflow Lite Python 解释器在 raspberry pi 3B+ 中像这样

进行对象检测

from tensorflow.contrib.lite.python import interpreter as interpreter_wrapper

但是当我运行这一行interpreter=interpreter_wrapper.Interpreter(model_path="mobilenet.tflite")

我收到这个错误:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
interpreter = interpreter_wrapper.Interpreter(model_path="mobilenet.tflite")
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/lite/python/interpreter.py", line 50, in __init__ 
_interpreter_wrapper.InterpreterWrapper_CreateWrapperCPPFromFile(
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/lazy_loader.py", line 53, in __getattr__
module = self._load()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/lazy_loader.py", line 42, in _load
module = importlib.import_module(self.__name__)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 28, in <module>
_tensorflow_wrap_interpreter_wrapper = swig_import_helper()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 24, in swig_import_helper
_mod = imp.load_module('_tensorflow_wrap_interpreter_wrapper', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 577, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 914, in create_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
ImportError: /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/lite/python/interpreter_wrapper/_tensorflow_wrap_interpreter_wrapper.so: undefined symbol: _ZN6tflite12tensor_utils39NeonMatrixBatchVectorMultiplyAccumulateEPKfiiS2_iPfi

如果有人试过请帮我修正这个错误

谢谢

我找到了一个答案页。 https://github.com/tensorflow/tensorflow/issues/21574 我认为,你最好从源代码构建一个 Python 包(Docker 在 unbunt 或 centos 等上交叉编译)。 https://www.tensorflow.org/install/install_raspbian -> 从源代码交叉编译

这是使用 TensorFlow v1.14 或更低版本到 运行 TensorFlow Lite 时发生的错误。要解决该错误,我们可以卸载常规 TensorFlow,然后改用 Google 提供的 tflite_runtime 包。

首先我描述一下我的环境:

  • Raspberry Pi 型号 3B+。
  • Raspbian OS.
  • 我正在名为“项目”的文件夹中工作。
  • 在此文件夹中,我使用 virtualenv 在名为“Project-env”的文件夹中创建了一个隔离环境。
  • 我在 raspberry pi 中的 python 项目有这个组件“import tensorflow as tf”,这是导致错误的原因。

因为我在 Google Colab 中使用 tensorflow 构建模型,所以这里应该没有必要使用它,至少在我的程序中是这样。

  • 所以首先转到您的工作文件夹:在我的例子中是“cd Project”。
  • 然后激活你的环境:在我的例子中是“source Project-env/bin/activate”。
  • 卸载tensorflow:“pip3卸载tensorflow”。
  • 得到这个轮子:"wget https://dl.google.com/coral/python/tflite_runtime-1.14.0-cp35-cp35m-linux_armv7l.whl" 这适用于Raspberry Pi Model 3B+。
  • 现在安装轮子:“pip3 install tflite_runtime-1.14.0-cp35-cp35m-linux_armv7l.whl”。
  • 最后在您的 python 程序中删除“import tensorflow as tf”这一行并将其替换为“from tflite_runtime.interpreter import Interpreter”。

所以不要使用“interpreter=tf.lite.Interpreter(modelpath)”,而是使用这个“interpreter=Interpreter(modelpath)”。

就是这样。所有帮助我解决这个问题的 EdjeElectronics 功劳。这是它的 YouTube 频道:https://www.youtube.com/watch?v=aimSGOAUI8Y&t=26s