我正在尝试使用TensorflowLite模型测试简单的tensorflow lite c ++代码。它得到两个浮点数并做异或。但是,当我更改输入时,输出不会更改。我猜这行interpreter->typed_tensor<float>(0)[0] = x是错误的,因此输入没有正确应用。我应该如何更改代码才能工作?
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/contrib/lite/string_util.h"
#include "tensorflow/contrib/lite/tools/mutable_op_resolver.h"
int main(){
const char graph_path[14] = "xorGate.lite";
const int num_threads = 1;
std::string input_layer_type = "float";
std::vector<int> sizes = {2};
float x,y;
std::unique_ptr<tflite::FlatBufferModel> model(
tflite::FlatBufferModel::BuildFromFile(graph_path));
if(!model){
printf("Failed to mmap model\n")
exit(0);
}
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
if(!interpreter){
printf("Failed to construct interpreter\n");
exit(0);
}
interpreter->UseNNAPI(false);
if(num_threads != 1){
interpreter->SetNumThreads(num_threads);
}
int input = interpreter->inputs()[0]; …Run Code Online (Sandbox Code Playgroud) 通过将源代码添加到,我已经成功构建了一个运行TF Lite模型的简单C ++应用程序tensorflow/lite/examples,这与官方C ++ TF指南建议的完整TF 相似。现在,我想将其构建为一个静态链接到TF Lite的单独项目(共享库),并使用CMake作为构建系统。
我试图向自己添加一个自定义目标CMakeLists.txt,该目标将使用Bazel构建TF Lite:
set(TENSORFLOW_DIR ${CMAKE_SOURCE_DIR}/thirdparty/tensorflow)
add_custom_target(TFLite
COMMAND bazel build //tensorflow/lite:framework
COMMAND bazel build //tensorflow/lite/kernels:builtin_ops
WORKING_DIRECTORY ${TENSORFLOW_DIR})
Run Code Online (Sandbox Code Playgroud)
我之所以选择这些Bazel目标,是因为其中的BUILD文件tensorflow/lite/examples/minimal将它们作为依赖项,并且当我在TF存储库中使用Bazel构建代码时,它们对我有用。不确定是否足够。
然后,我手动收集include dirs(具有丑陋的临时硬编码路径)和libs:
set(TFLite_INCLUDES
${TENSORFLOW_DIR}
~/.cache/bazel/_bazel_azymohliad/ec8567b83922796adb8477fcbb00a36a/external/flatbuffers/include)
set(TFLite_LIBS
${TENSORFLOW_DIR}/bazel-bin/tensorflow/lite/libframework.pic.a)
target_include_directories(MyLib ... PRIVATE ... ${TFLite_INCLUDES})
target_link_libraries(MyLib ... ${TFLite_LIBS})
Run Code Online (Sandbox Code Playgroud)
通过这种配置,我在链接期间获得了许多未定义的对TFLite内容的引用。我检查了nm,这些符号确实在中丢失了libframework.pic.a,我.o在Bazel输出的各种文件中找到了其中的一些。手动选择所有这些.o文件似乎是错误的。
因此,是否可以像我想要的那样从CMake很好地链接到TF Lite?也许有一些神奇的bazel query include_dirs(//tensorflow/lite:framework)命令可以为我提供所有必要的包含目录的路径,以及一个类似的库链接命令,以便我可以将此信息传递给CMake?
我目前正在研究一个关于神经网络的项目.为此,我想构建一个Android应用程序,它应该使用tensorflow [lite]来解决一些对象检测/识别问题.
因为我希望代码尽可能地可移植,所以我想用C++编写大部分代码,因此使用tensorflow lite的C++ API而不是Java API/wrapper.因此,我修改了tensorflow/contrib/lite/BUILD并添加了以下内容以便能够创建共享张量流库.
cc_binary(
name = "libtensorflowLite.so",
linkopts=["-shared", "-Wl"],
linkshared=1,
copts = tflite_copts(),
deps = [
":framework",
"//tensorflow/contrib/lite/kernels:builtin_ops",
],
)
Run Code Online (Sandbox Code Playgroud)
(这是基于解决这个问题的:https://github.com/tensorflow/tensorflow/issues/17826)
然后我用了
bazel build //tensorflow/contrib/lite:libtensorflowLite.so --crosstool_top=//external:android/crosstool --cpu=arm64-v8a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cxxopt="-std=c++11"
Run Code Online (Sandbox Code Playgroud)
最终建立它.
之后我转向Android Studio并设置了一个基本项目.为了将共享库添加到项目中,我参考了这个示例:
我还为flatbuffers添加了所需的依赖项.
构建/编译过程成功,没有任何链接器错误(好吧,至少在尝试了几个小时之后......).
然后APK成功安装在Android设备上,但在启动后立即崩溃.Logcat提供以下输出:
04-14 20:09:59.084 9623-9623/com.example.hellolibs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hellolibs, PID: 9623
java.lang.UnsatisfiedLinkError: dlopen failed: library "/home/User/tensorflowtest/app/src/main/cpp/../../../../distribution/tensorflow/lib/x86/libtensorflowLite.so" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1657)
at com.example.hellolibs.MainActivity.<clinit>(MainActivity.java:36)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at …Run Code Online (Sandbox Code Playgroud)