Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在(Android Studio)NDK(C/C+;+;API)中运行Tensorflow Lite推理? 信息 我从Keras建立了Tensorflow(TF)模型,并将其转换为Tensorflow Lite(TFL) 我在AndroidStudio中构建了一个Android应用程序,并使用JavaAPI运行TFL模型 在Java应用程序中,我使用了TFL支持库(请参阅),并通过在我的build.gradle依赖项下包含implementation'org.TensorFlow:TensorFlow Lite:+'使用了JCenter的TensorFlow Lite AAR_C++_Android Studio_Gradle_Android Ndk_Tensorflow Lite - Fatal编程技术网

C++ 如何在(Android Studio)NDK(C/C+;+;API)中运行Tensorflow Lite推理? 信息 我从Keras建立了Tensorflow(TF)模型,并将其转换为Tensorflow Lite(TFL) 我在AndroidStudio中构建了一个Android应用程序,并使用JavaAPI运行TFL模型 在Java应用程序中,我使用了TFL支持库(请参阅),并通过在我的build.gradle依赖项下包含implementation'org.TensorFlow:TensorFlow Lite:+'使用了JCenter的TensorFlow Lite AAR

C++ 如何在(Android Studio)NDK(C/C+;+;API)中运行Tensorflow Lite推理? 信息 我从Keras建立了Tensorflow(TF)模型,并将其转换为Tensorflow Lite(TFL) 我在AndroidStudio中构建了一个Android应用程序,并使用JavaAPI运行TFL模型 在Java应用程序中,我使用了TFL支持库(请参阅),并通过在我的build.gradle依赖项下包含implementation'org.TensorFlow:TensorFlow Lite:+'使用了JCenter的TensorFlow Lite AAR,c++,android-studio,gradle,android-ndk,tensorflow-lite,C++,Android Studio,Gradle,Android Ndk,Tensorflow Lite,推理时间不是很长,所以现在我想在Android的NDK中使用TFL 因此,我在AndroidStudio的NDK中构建了Java应用程序的精确副本,现在我正试图在项目中包含TFL库。我遵循并在本地构建了TFL库(并获得了一个AAR文件),并将该库包含在Android Studio中的NDK项目中 现在我尝试在我的C++文件中使用TFL库,试图在代码中包含 >代码,但是我得到一个错误消息:找不到TysFrace(或者我试图使用的任何其他名称,根据我在 CMAKELIST.TXT < /Cord>文

推理时间不是很长,所以现在我想在Android的NDK中使用TFL

因此,我在AndroidStudio的NDK中构建了Java应用程序的精确副本,现在我正试图在项目中包含TFL库。我遵循并在本地构建了TFL库(并获得了一个AAR文件),并将该库包含在Android Studio中的NDK项目中

现在我尝试在我的C++文件中使用TFL库,试图在代码中包含<代码> >代码,但是我得到一个错误消息:<代码>找不到TysFrace<代码>(或者我试图使用的任何其他名称,根据我在<代码> CMAKELIST.TXT < /Cord>文件中给出的名称)。 文件夹 App build.gradle:

Project build.gradle:

CMakeLists.txt:

native-lib.cpp:

  • 我接着说:试图在
    工作区中设置
    android\u sdk\u存储库
    /
    android\u ndk\u存储库
    时出错:
    工作区:149:1:无法在工作区文件中的任何load语句之后重新定义存储库(对于存储库“androidsdk”)
    ,将这些语句定位在不同的位置会导致相同的错误
  • 我删除了对
    WORKSPACE
    的这些更改,并继续了zimenglyu的帖子:我编译了
    libtensorflowLite.so
    ,编辑了
    CMakeLists.txt
    ,从而引用了
    libtensorflowLite.so
    文件,但保留了
    FlatBuffer
    部分。Android项目编译成功,但没有明显的变化,我仍然不能包含任何TFLite库
尝试编译TFL时,我在
tensorflow/tensorflow/lite/BUILD
中添加了一个
cc\u二进制文件(如下所示):

尝试为
x86\u 64
arm64-v8a
构建它时,我得到了一个错误:
cc\u工具链\u套件规则@local\u config\u cc/:toolchain:cc\u工具链\u套件'@local\u config\u cc/:toolchain'不包含cpu'x86\u 64'的工具链。

检查第47行中的
external/local\u config\u cc/BUILD
(提供了错误):

cc_toolchain_suite(
    name = "toolchain",
    toolchains = {
        "k8|compiler": ":cc-compiler-k8",
        "k8": ":cc-compiler-k8",
        "armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a",
        "armeabi-v7a": ":cc-compiler-armeabi-v7a",
    },
)
这些是唯一找到的2个
cc_工具链。在存储库中搜索“cc compiler-”我只找到了“aarch64”,我假设它是针对64位ARM的,但没有找到“x86_64”。虽然有“x64_windows”,但我使用的是Linux

尝试使用aarch64构建,如下所示:

bazel build -c opt --fat_apk_cpu=aarch64 --cpu=aarch64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain //tensorflow/lite/java:tensorflow-lite
导致错误:

ERROR: /.../external/local_config_cc/BUILD:47:1: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'aarch64'
/path/to/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2339: error: undefined reference to 'tflite::impl::Interpreter::~Interpreter()'
使用Android Studio中的库: 通过更改build config中的
soname
并使用
CMakeLists.txt中的完整路径,我能够为
x86_64
体系结构构建库。这导致了
.so
共享库。另外-我能够通过调整
aarch64\u makefile.inc
文件,使用TFLite Docker容器为
arm64-v8a
构建库,但我没有更改任何构建选项,而是让
build\u aarch64\u lib.sh
无论它构建什么。这导致了一个
.a
静态库

所以现在我有两个TFLite库,但我仍然无法使用它们(例如,我不能
#包括“…”
任何东西)

当尝试构建项目时,只使用
x86_64
可以很好地工作,但是尝试包含
arm64-v8a
库会导致忍者错误:
“…/libtensorflow lite.a”,需要“…/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative lib.so”,缺少并且没有已知的规则来创建它

不同的方法-使用Gradle构建/编译源文件: 我在Android Studio中创建了一个本地C++项目
  • 我从Tensorflow的
    lite
    目录中获取了基本的C/C++源文件和头文件,并在
    app/src/main/cpp
    中创建了一个类似的结构,其中包括(a)Tensorflow、(B)absl和(C)flatbuffers文件
  • 我将所有tensorflow头文件中的
    #include“tensorflow/…
    行更改为相对路径,以便编译器可以找到它们
  • 在应用程序的
    build.gradle
    中,我为
    .tflite
    文件添加了一个无压缩行:
    aaptOptions{noCompress“tflite”}
  • 我在应用程序中添加了一个
    assets
    目录
  • native lib.cpp中
    I添加了
  • 尝试使用包含的源文件生成项目(生成目标为
    arm64-v8a
  • 我得到一个错误:

    ERROR: /.../external/local_config_cc/BUILD:47:1: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'aarch64'
    
    /path/to/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2339: error: undefined reference to 'tflite::impl::Interpreter::~Interpreter()'
    
    中,第2339行是
    “delete”ptr;“
    行:

    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
        static_assert(sizeof(_Tp) > 0,
                      "default_delete can not delete incomplete type");
        static_assert(!is_void<_Tp>::value,
                      "default_delete can not delete incomplete type");
        delete __ptr;
      }
    
    \u LIBCPP\u INLINE\u VISIBILITY void操作符()(\u Tp*\u ptr)const\u NOEXCEPT{
    静态断言(sizeof(_Tp)>0,
    “默认删除不能删除不完整类型”);
    静态断言(!is_void::value,
    “默认删除不能删除不完整类型”);
    删除uu ptr;
    }
    
    问题: 如何在androidstudio中包含TFLite库,以便从NDK运行TFL推断


    或者-我如何使用gradle(目前使用cmake)来构建和编译源文件?

    我以以下方式使用原生TFL和C-API:

    设置:
  • 下载最新版本的
  • 将下载的
    .arr
    文件的文件类型更改为
    .zip
    并解压缩该文件以获取共享库(
    .so
    文件)
  • 从中的
    c
    目录下载所有头文件
  • 在Android工作室中创建Android C++应用程序
  • app/src/main中创建
    jni
    目录(
    New
    ->
    文件夹
    ->
    jni文件夹
    bazel build -c opt --fat_apk_cpu=aarch64 --cpu=aarch64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain //tensorflow/lite/java:tensorflow-lite
    
    ERROR: /.../external/local_config_cc/BUILD:47:1: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'aarch64'
    
    /path/to/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2339: error: undefined reference to 'tflite::impl::Interpreter::~Interpreter()'
    
    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
        static_assert(sizeof(_Tp) > 0,
                      "default_delete can not delete incomplete type");
        static_assert(!is_void<_Tp>::value,
                      "default_delete can not delete incomplete type");
        delete __ptr;
      }
    
    #include "../jni/c_api.h"
    #include "../jni/common.h"
    #include "../jni/builtin_ops.h"
    
    TfLiteModel * model = TfLiteModelCreateFromFile(full_path);
    TfLiteInterpreter * interpreter = TfLiteInterpreterCreate(model);
    TfLiteInterpreterAllocateTensors(interpreter);
    TfLiteTensor * input_tensor =
                TfLiteInterpreterGetInputTensor(interpreter, 0);
    const TfLiteTensor * output_tensor =
                TfLiteInterpreterGetOutputTensor(interpreter, 0);
    TfLiteStatus from_status = TfLiteTensorCopyFromBuffer(
                input_tensor,
                input_data,
                TfLiteTensorByteSize(input_tensor));
    TfLiteStatus interpreter_invoke_status = TfLiteInterpreterInvoke(interpreter);
    TfLiteStatus to_status = TfLiteTensorCopyToBuffer(
                output_tensor,
                output_data,
                TfLiteTensorByteSize(output_tensor));
    
    set(JNI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jni)
    add_library(tflite-lib SHARED IMPORTED)
    set_target_properties(tflite-lib
            PROPERTIES IMPORTED_LOCATION
            ${JNI_DIR}/${ANDROID_ABI}/libtfl.so)
    include_directories( ${JNI_DIR} )
    target_link_libraries(
            native-lib
            tflite-lib
            ...)
    
    git clone https://github.com/tensorflow/tensorflow
    cd ./tensorflow/
    
    ./configure
    
    $ ./configure
    You have bazel 3.7.2-homebrew installed.
    Please specify the location of python. [Default is /Library/Developer/CommandLineTools/usr/bin/python3]: /Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/bin/python
    
    Found possible Python library paths:
      /Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/lib/python3.7/site-packages
    Please input the desired Python library path to use.  Default is [/Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/lib/python3.7/site-packages]
    
    Do you wish to build TensorFlow with ROCm support? [y/N]: N
    No ROCm support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with CUDA support? [y/N]: N
    No CUDA support will be enabled for TensorFlow.
    
    Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
    Clang will not be downloaded.
    
    Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: 
    
    Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
    Searching for NDK and SDK installations.
    
    Please specify the home path of the Android NDK to use. [Default is /Users/cuongvng/library/Android/Sdk/ndk-bundle]: /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462
    
    $ ls /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462
    CHANGELOG.md      build             ndk-stack         prebuilt          source.properties wrap.sh
    NOTICE            meta              ndk-which         python-packages   sources
    NOTICE.toolchain  ndk-build         package.xml       shader-tools      sysroot
    README.md         ndk-gdb           platforms         simpleperf        toolchains
    
    WARNING: The NDK version in /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462 is 21, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.
    
    Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29']] [Default is 21]: 29
    
    Please specify the home path of the Android SDK to use. [Default is /Users/cuongvng/library/Android/Sdk]: /Users/cuongvng/Library/Android/sdk
    
    Please specify the Android SDK API level to use. [Available levels: ['28', '29', '30']] [Default is 30]: 30
    
    Please specify an Android build tools version to use. [Available versions: ['29.0.2', '29.0.3', '30.0.3', '31.0.0-rc1']] [Default is 31.0.0-rc1]: 30.0.3
    
    bazel build -c opt --config=android_arm //tensorflow/lite:libtensorflowlite.so
    # or
    bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so