当前位置:   article > 正文

android 引入ncnn框架运行onnx模型_android onnx

android onnx
开发背景

利用神经网络算法优化噪点,目前是想在app上跑神经网络算法。在网上查找腾讯开源了ncnn框架。
集成步骤如下:

下载nccn库

这个也可以自己编译,简单集成下载库地址为https://github.com/Tencent/ncnn/releases,除了下载android之外还要下载window包,因为windows包有我们需要的模型转换工具和模型转为代码文件工具。

新建项目

android studio 4版本,新建native c++ 项目,新建好后,在ide菜单file->project structure->modules选择ndk版本和build工具版本。在src/main文件夹上新建jni文件夹把相关的库进去arm64-v8a/libncnn.a,armeabi-v7a/libncnn.a。

配置项目信息

首先配置build.gradle里面的信息在externalNativeBuild添加如下编译参数

externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang"
                cFlags "-fopenmp -static-openmp -O2 -fvisibility=hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
                cppFlags "-fopenmp -static-openmp -O2 -fvisibility=hidden -fvisibility-inlines-hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
                arguments "-DANDROID_STL=c++_shared", "-DANDROID_CPP_FEATURES=rtti exceptions"
                cppFlags ""
                cppFlags "-std=c++11"
                cppFlags "-frtti"
                cppFlags "-fexceptions"
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
ndk{
      abiFilters 'armeabi-v7a', 'arm64-v8a' 
    }
  • 1
  • 2
  • 3

然后再配置cmakelist.text

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("ncnnexample")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib
                       ncnn
                       android
                       jnigraphics
                       z
                       m
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

include_directories(src/main/cpp/include)

add_library(ncnn STATIC IMPORTED)
SET_TARGET_PROPERTIES(  ncnn
                        PROPERTIES IMPORTED_LOCATION
                        ${CMAKE_SOURCE_DIR}/../jni/${CMAKE_ANDROID_ARCH_ABI}/libncnn.a)                     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
遇到问题

1、libomp.so not found这个是ndk版本有关系,可以网上查下。
2、ncnn运行结果出错或崩溃,这个需要使用同一个版本ncnn,包括onnx2ncnn,ncnn2mem和.a要一致。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/747121
推荐阅读
相关标签
  

闽ICP备14008679号