[英]Problems with using tensorflow lite C++ API in Android Studio Project
[英]Register Custom Operator in Tensorflow Lite C API
我正在使用 C API 在 Android 上运行 tensorflow lite。 我的模型需要操作符RandomStandardNormal
,它最近在 tensorflow v2.4.0-rc0
here 中作为自定义操作原型实现
TfLiteInterpreterOptionsAddCustomOp()
函数在tensorflow/lite/c/c_api_experimental.h 中列出:
TFL_CAPI_EXPORT void TfLiteInterpreterOptionsAddCustomOp(
TfLiteInterpreterOptions* options, const char* name,
const TfLiteRegistration* registration, int32_t min_version,
int32_t max_version);
看看这个例子和线程,我试图像这样使用TfLiteInterpreterOptionsAddCustomOp
:
// create model and interpreter options
TfLiteModel *model = TfLiteModelCreateFromFile("path/to/model.tflite");
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
// register custom ops
TfLiteInterpreterOptionsAddCustomOp(options, "RandomStandardNormal", Register_RANDOM_STANDARD_NORMAL(), 1, 1);
// create the interpreter
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
TfLiteInterpreterAllocateTensors(interpreter);
我看到Register_RANDOM_STANDARD_NORMAL()
函数是在tensorflow/lite/kernels/custom_ops_register.h的tflite::ops::custom
C++ 命名空间中tflite::ops::custom
。 但是,当我尝试将它包含在我的 C 文件中时,编译器会抱怨,因为namespace
是 C 中的未知类型。
如何使用 tensorflow lite C API 注册自定义运算符? 我是否需要使用 C++ 编译器才能将 C API 与此自定义运算符一起使用,因为它是在 C++ 中定义的?
注意:我在编译libtensorflowlite_c.so
时在 bazel BUILD deps 中包含//tensorflow/lite/kernels:custom_ops
libtensorflowlite_c.so
看起来这是通过以下解决方法在 Github 上回答的:
https://github.com/tensorflow/tensorflow/issues/44664#issuecomment-723310060
在 tensorflow github 上,@jdduke 提出了一个临时解决方法:
custom_ops_register.h
添加一个extern "C"
包装头extern "C" {
TFL_CAPI_EXPORT TfLiteRegistration* TfLiteRegisterRandomStandardNormal();
}
random_standard_normal.cc
添加extern "C"
包装器实现extern "C" {
TFL_CAPI_EXPORT TfLiteRegistration* TfLiteRegisterRandomStandardNormal() {
return tflite::ops::custom::Register_RANDOM_STANDARD_NORMAL();
}
}
//tensorflow/lite/kernels:custom_ops
作为依赖项包含在tensorflow/lite/c/BUILD
tflite_cc_shared_object(
name = "tensorflowlite_c",
linkopts = select({
"//tensorflow:ios": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:macos": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:windows": [],
"//conditions:default": [
"-z defs",
"-Wl,--version-script,$(location //tensorflow/lite/c:version_script.lds)",
],
}),
per_os_targets = True,
deps = [
":c_api",
":c_api_experimental",
":exported_symbols.lds",
":version_script.lds",
"//tensorflow/lite/kernels:custom_ops", # here
],
)
TfLiteInterpreterOptionsAddCustomOp(options, "RandomStandardNormal", TfLiteRegisterRandomStandardNormal(), 1, 1);
它奏效了! 我的张量终于在 android 上分配了 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.