cmdbug / YOLOv5_NCNN

🍅 Deploy ncnn on mobile phones. Support Android and iOS. 移动端ncnn部署,支持Android与iOS。
GNU General Public License v3.0
1.46k stars 297 forks source link

请教下,ncnn重新编译替换的问题 #68

Closed colinleng closed 3 years ago

colinleng commented 3 years ago

1、从git上clonele ncnn进行编译之后对src/main/cpp/ncnn下的文件对应进行了替换 2、修改CMakeLists.txt文件为一下内容:

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.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
if(DEFINED ANDROID_NDK_MAJOR AND ${ANDROID_NDK_MAJOR} GREATER 20)
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-openmp")
endif()
# 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.

# 搜索当前目录下的所有.cpp文件
aux_source_directory(. SRC_LIST)
aux_source_directory(./ocr OCR_SRC_LIST)
add_library(
        yolov5
        SHARED
        ${SRC_LIST}
        ${OCR_SRC_LIST}
        )

#add_library( # Sets the name of the library.
#        yolov5
#
#        # Sets the library as a shared library.
#        SHARED
#
#        # Provides a relative path to your source file(s).
#        jni_interface.cpp
#        YoloV5.cpp
#        YoloV4.cpp
#        SimplePose.cpp
#        Yolact.cpp
#        ocr/clipper.cpp
#        ocr/NCNNDBNet.cpp
#        ocr/ocr.cpp
#        ocr/RRLib.cpp
#        ocr/ZUtil.cpp
#        ENet.cpp
#        FaceLandmark.cpp
#        DBFace.cpp
#        MbnFCN.cpp
#        MobileNetV3Seg.cpp
#        YoloV5CustomLayer.cpp
#        NanoDet.cpp
#        )

include_directories(
        ncnn/include
        ocr
)
# 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(log-lib log)
find_library(android-lib android)
find_library(vulkan-lib vulkan)
find_library(jnigraphics-lib jnigraphics)
add_library( ncnn STATIC IMPORTED )
set_target_properties( # Specifies the target library.
        ncnn

        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION

        # Provides the path to the library you want to import.
        ${CMAKE_SOURCE_DIR}/ncnn/${ANDROID_ABI}/libncnn.a )

# ncnnvulkan
add_library(glslang STATIC IMPORTED)
add_library(OGLCompiler STATIC IMPORTED)
add_library(OSDependent STATIC IMPORTED)
add_library(SPIRV STATIC IMPORTED)
set_target_properties(glslang PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ncnnvulkan/${ANDROID_ABI}/libglslang.a)
set_target_properties(OGLCompiler PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ncnnvulkan/${ANDROID_ABI}/libOGLCompiler.a)
set_target_properties(OSDependent PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ncnnvulkan/${ANDROID_ABI}/libOSDependent.a)
set_target_properties(SPIRV PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ncnnvulkan/${ANDROID_ABI}/libSPIRV.a)

# disable rtti and exceptions
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
# enable rtti and exceptions
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")

include_directories(
        ${CMAKE_SOURCE_DIR}/opencv/include/
)
add_library(libopencv_java4 STATIC IMPORTED)
set_target_properties(
        libopencv_java4
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/opencv/${ANDROID_ABI}/libopencv_java4.so
        )
target_link_libraries( # Specifies the target library.
        yolov5

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${vulkan-lib}
        ${android-lib}
        ${jnigraphics-lib}
        libopencv_java4)

# 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.
        yolov5

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${vulkan-lib}
        ${android-lib}
        ${jnigraphics-lib}
        ncnn
        glslang SPIRV OGLCompiler OSDependent )

但是项目编译的时候会报:YOLOv5_NCNN/android_YOLOV5_NCNN/app/src/main/cpp/MbnFCN.cpp:15:20: error: no member named 'get_gpu_count' in namespace 'ncnn' 错误,请问这个大概是什么问题引起的呢 另:模型已按照 https://zhuanlan.zhihu.com/p/275989233 这个文章的方法进行转换并对文章内的图片demo进行测试没有问题,先想将模型放到这个项目当中。所以需要使用 YoloV5CustomLayer这个进行操作。

cmdbug commented 3 years ago

因为demo用的是vulkan有gpu接口,应该是你下载的ncnn库不是gpu版本。 或者自己改下代码,加下 Vulkan 的宏判断

#if NCNN_VULKAN
    hasGPU = ncnn::get_gpu_count() > 0;
#endif
cmdbug commented 3 years ago

宏在 platform.h 文件里面,导入下头文件

colinleng commented 3 years ago

如果我按照说明当中去掉opencv的话,是不是就是指在CMakeLists.txt当中将 include_directories( ${CMAKE_SOURCE_DIR}/opencv/include/ ) add_library(libopencv_java4 STATIC IMPORTED) set_target_properties( libopencv_java4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/opencv/${ANDROID_ABI}/libopencv_java4.so ) 注释掉也可以呢?

colinleng commented 3 years ago

其二,就是我在 https://github.com/Tencent/ncnn 当中下载gpu版本的之后进行编译VULKAN版本,之后替换掉VULKAN版本内的文件就可以

colinleng commented 3 years ago

您好,不好意思还需要请教下,我使用您给方法,手动改动了代码,在报错的地方,添加了

if NCNN_VULKAN

endif

apk可以正常编译成功。但是,在使用yolov5_custom_layer的时候,会闪退。

D/Hwaps: APS: EventAnalyzed: registerCallbackInApsManagerService, mPkgName:com.wzt.yolov5; result = true E/libc: Access denied finding property "emui_hwagp_high_freq_scenes" E/libc: Access denied finding property "emui_hwagp_high_freq_scenes" W/com.wzt.yolov5: type=1400 audit(0.0:204906): avc: denied { read } for pid=15037 name="u:object_r:hwagp_high_freq_scenes_prop:s0" dev="tmpfs" ino=17124 scontext=u:r:untrusted_app:s0:c93,c258,c512,c768 tcontext=u:object_r:hwagp_high_freq_scenes_prop:s0 tclass=file permissive=0 V/AudioManager: querySoundEffectsEnabled... W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@267368 V/ActivityThread: callActivityOnCreate I/DecorView[]: pkgName:com.wzt.yolov5 old windowMode:0 new windoMode:1, isFixedSize:false W/ncnn: layer YoloV5Focus not exists or registered A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xf in tid 15037 (com.wzt.yolov5), pid 15037 (com.wzt.yolov5) 您好,这个时候是什么错误呢?不好意思,麻烦您。

cmdbug commented 3 years ago

W/ncnn: layer YoloV5Focus not exists or registered 会建工程的话建议自己新建个工程,demo想要全部都正常的话需要按说明重新编译ncnn。 或者直接用 nihui 的工程 yolov5

colinleng commented 3 years ago

已搞定,重新编译了VULKAN版本,进行替换之后就可以了,感谢🙏

colinleng commented 3 years ago

已搞定,重新编译了VULKAN版本,进行替换之后就可以了,感谢🙏