如何为 tflite 解释器调用 Flex 委托?

How to invoke the Flex delegate for tflite interpreters?

本文关键字:Flex 委托 调用 解释器 tflite      更新时间:2023-10-16

我有一个TensorFlow模型,我想将其转换为tflite模型,该模型将部署在ARM64平台上。

碰巧我的模型的两个操作(RandomStandardNormal,Softplus(似乎需要自定义实现。由于执行时间不是那么重要,我决定使用使用扩展运行时的混合模型。我通过以下方式转换了它:

graph_def_file = './model/frozen_model.pb'
inputs = ['eval_inputs']
outputs = ['model/y']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_file_name = 'vae_' + str(tf.__version__) + '.tflite'
tflite_model = converter.convert()
open(tflite_file_name, 'wb').write(tflite_model)

这奏效了,我最终得到了一个看似有效的 tflite 模型文件。每当我尝试使用解释器加载此模型时,都会收到错误(无论我使用 Python 还是 C++ API(:

ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you invoke the Flex delegate before inference.
ERROR: Node number 4 (FlexSoftplus) failed to prepare.

我很难在 tf 网站上找到有关如何为两个 API 调用 Flex 委托的文档。我偶然发现了一个头文件("tensorflow/lite/delegates/flex/delegate_data.h"(,它似乎与这个问题有关,但将其包含在我的C++项目中会产生另一个错误:

In file included from /tensorflow/tensorflow/core/common_runtime/eager/context.h:28:0,
from /tensorflow/tensorflow/lite/delegates/flex/delegate_data.h:18,
from /tensorflow/tensorflow/lite/delegates/flex/delegate.h:19,
from demo.cpp:7:
/tensorflow/tensorflow/core/lib/core/status.h:23:10: fatal error: tensorflow/core/lib/core/error_codes.pb.h: No such file or directory
#include "tensorflow/core/lib/core/error_codes.pb.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

碰巧,以前有没有人遇到并解决了这个问题?如果您有示例代码段,请分享链接!

使用 bazel 流水线构建 TensorFlow Lite 库时,可以包含并启用额外的 TensorFlow ops 库,如下所示:

如有必要,通过添加 --config=整体式构建标志启用整体式构建。

将 TensorFlow ops 委托库依赖项添加到构建依赖项:tensorflow/lite/delegates/flex:delegate。

请注意,只要委托链接到客户端库,在运行时创建解释器时将自动安装必要的 TfLiteDelegate。不必像其他委托类型通常需要的那样显式安装委托实例。

蟒蛇点包

Python支持正在积极开发中。

来源: https://www.tensorflow.org/lite/guide/ops_select

根据 2019/9/25 的 https://www.tensorflow.org/lite/guide/ops_select#android_aar

Python对"选择运算符"的支持正在积极开发中。

您可以使用 FlexDelegate 在 Android 中测试模型。 我以同样的方式成功运行了我的模型。

例如 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/java/src/test/java/org/tensorflow/lite/InterpreterFlexTest.java