python - 如何使用适用于 x86_64 系统的精选 TensorFlow ops 构建 TensorFlow lite?

标签 python tensorflow bazel tensorflow-lite

为了能够运行支持原生 TensorFlow 操作的 TensorFlow 精简模型,必须重新构建 libtensorflow-lite 静态库编译。在 C++ 中执行此操作的说明可以在 HERE 中找到.

它指出

When building TensorFlow Lite libraries using the bazel pipeline, the additional TensorFlow ops library can be included and enabled as follows:

  • Enable monolithic builds if necessary by adding the --config=monolithic build flag.

  • Add the TensorFlow ops delegate library dependency to the build dependencies: tensorflow/lite/delegates/flex:delegate.

Note that the necessary TfLiteDelegate will be installed automatically when creating the interpreter at runtime as long as the delegate is linked into the client library. It is not necessary to explicitly install the delegate instance as is typically required with other delegate types.

问题是构建静态库的标准方法是通过 shell 脚本/make(参见文档 HERE ;这是针对 arm64 的,但也有可以使用的脚本也适用于 x86_64)。我没有明显的方法可以通过 bazel 构建 tensorflow-lite 并在那里修改构建命令。

有没有人在尝试为 arm64/x86_64 架构构建模型时成功构建它并可以分享它?我是 bazel 的新手,找不到详细的演练。

编辑

按照@jdehesa 提出的故障排除步骤后,我能够构建libtensorflowlite.so,但遇到了另一个问题。我的应用程序构建成功,但是在执行应用程序时,找不到 .so 文件:

./myapp: error while loading shared libraries: libtensorflowlite.so: cannot open shared object file: No such file or directory

路径是正确的,因为其他 .so 文件位于可以找到的同一目录中。此外,如果使用静态库,该应用程序也能正常工作。

为了重现该问题,我使用了 tensorflow/tensorflow:devel-gpu-py3 docker 构建镜像(找到说明 here )。

我使用默认设置执行了配置脚本,并使用了命令

bazel build --config=monolithic --define=with_select_tf_ops=true -c opt //tensorflow/lite:libtensorflowlite.so

创建库。我已经通过内置库上传到我的个人仓库(https://github.com/DocDriven/debug-lite)。

最佳答案

编辑:实验性选项with_select_tf_ops 似乎是removed发布后不久。据我所知,似乎没有任何内置选项可以在 current build script for libtensorflowlite 中包含 TF 委托(delegate)库。 .如果您想使用 Bazel 构建库,目前唯一的选择似乎是在目标依赖项列表中包含 tensorflow/lite/delegates/flex:delegateas suggested in the docs .

几天前提交了一个 initial support for building TFLite with CMake 的提交.在该构建脚本中有一个选项 SELECT_TF_OPS在构建中包含委托(delegate)库。我不知道该构建目前是否有效,但我想它最终会成为即将发布的正式版本的一部分。


看来 libtensorflow-lite.a 是用 Makefiles 构建的,出于 Bazel,所以我不确定您是否真的可以将该选项用于该库。然而,有一个实验性共享库目标 libtensorflowlite.so我认为这可能是你需要的。可以给实验选项 with_select_tf_ops在其中包含 TensorFlow 内核。所以我认为构建命令应该是这样的:

bazel build --config=monolithic --define=with_select_tf_ops=true -c opt //tensorflow/lite:libtensorflowlite.so

关于python - 如何使用适用于 x86_64 系统的精选 TensorFlow ops 构建 TensorFlow lite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58623937/

相关文章:

python - 如何将 Django 模型列的值映射到列表?

python - Flask 应用程序配置采用其默认值而不是我的 config.py

python - 将时间序列数据输入 Tensorflow 进行 LSTM 分类器训练

plugins - 错误 : Configuration cannot be run until project has been synced

python - 如何遍历列表中除最后一项之外的所有内容?

python - sklearn 使用 prob_a 进行逻辑二元分类

python - 如何列出 python 脚本运行时加载的 .so 文件?

machine-learning - 机器学习 (CNN/RNN) 的不平衡训练数据

go - 需要一些帮助来使用 bazel 构建自定义 Terraform 提供程序

kubernetes - Bazel Rules_k8s - 如何应用外部配置文件? (来自网址)