Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自己编译mdl库运行速度超慢且自己使用tools转换的model不能工作!!! #75

Closed
2php opened this issue Oct 17, 2017 · 20 comments
Labels

Comments

@2php
Copy link

2php commented Oct 17, 2017

在android demo的基础上增加了so库的编译,生成apk后运行(googlenet)速度达到25s,与预览版相差十万八千里,不知道这是怎么回事?

其次,使用model转换工具生成googlenet的data.min.bin和model.min.json,替换预览版的运行,等了5分钟都没有结果,这又是为何?

@cocodark
Copy link
Contributor

能把你so库编译的脚本贴出来看看吗?

@cocodark
Copy link
Contributor

关于第二个问题,你用test/googlenet目录下的模型试试呢

@2php
Copy link
Author

2php commented Oct 18, 2017

说明一下我是使用tools下的googlenet/deploy.prototxt和googlenet/full.caffemodel转换的,生成后的model.min.json是格式化的json,看了一下,demo中的是经过压缩的,从调试信息可以看出,在计算json的hash值时不匹配造成,症结在此,解决方法就是:计算格式化json后的hash值替换tools后生成的,OK!

关于第一个问题,我开始时使用的是debug,换成release后速度提到4s,不过还是很慢啊,估计是优化的问题,还有demo的模型应该是经过优化的!!!

@2php
Copy link
Author

2php commented Oct 18, 2017

编译so的cmake脚本:

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)

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_definitions (
-Wno-format-security
)

#set(CMAKE_VERBOSE_MAKEFILE on)
#set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")

引用第三方库头文件

include_directories(

F:/opencv/build/include

F:/opencv/build/include/opencv

F:/opencv/build/include/opencv2

        )

引用第三方库

#add_library(

ml # so库的名称

SHARED

IMPORTED # 表示只需要导入,不需要构建so库。

)

第三方库的路径

#set_target_properties(

ml # so库的名称

PROPERTIES IMPORTED_LOCATION # import so库

libs/libml.so # so库路径

)

本so库的名称、源码等

include_directories(
src/main/cpp/include
src/main/cpp/third-party
)
add_library( # Sets the name of the library.
mdl
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/base/layer.cpp
src/main/cpp/base/matrix.cpp
src/main/cpp/commons/commons.cpp
src/main/cpp/layer/batch_normal_layer.cpp
src/main/cpp/layer/bias_layer.cpp
src/main/cpp/layer/concat_layer.cpp
src/main/cpp/layer/convolution_layer.cpp
src/main/cpp/layer/fc_layer.cpp
src/main/cpp/layer/lrn_layer.cpp
src/main/cpp/layer/pooling_layer.cpp
src/main/cpp/layer/relu_layer.cpp
src/main/cpp/layer/scale_layer.cpp
src/main/cpp/layer/soft_max_layer.cpp
src/main/cpp/layer/split_layer.cpp
src/main/cpp/loader/loader.cpp
src/main/cpp/math/gemm.cpp
src/main/cpp/third-party/json/json11.cpp
src/main/cpp/net.cpp
src/main/cpp/mdl_jni.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.
mdl
# Links the target library to the log library
# included in the NDK.
${log-lib} )

@cocodark
Copy link
Contributor

@2php 你的Cmake中好像没有打开neon支持

@cocodark
Copy link
Contributor

@2php 关于第二个问题,你是手动改了tools生成的sign值才能校验通过是吗?

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 是的,我估计是由于linux和windows上关于换行符的原因导致hash计算错误,因为开发环境都是windows

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark NEON不知道是不是这样开启?externalNativeBuild {
cmake {
arguments '-DANDROID_ARM_NEON=TRUE', '-DANDROID_TOOLCHAIN=gcc'
cppFlags "-fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 -O3 -DNDEBUG -fexceptions -frtti -Wno-psabi -funwind-tables -fsigned-char -no-canonical-prefixes -fdata-sections -ffunction-sections -Wa,--noexecstack -fopenmp -std=c++11 -fPIC -Wall -Wno-sign-compare -Wno-uninitialized -s -fPIE -pie -llog"
abiFilters "armeabi-v7a", "arm64-v8a"
}
}

@cocodark
Copy link
Contributor

开启neon的方式应该是ok的,你通过jni接口设置线程数了吗?参照Android demo

@cocodark
Copy link
Contributor

你是在windows上生成的json,然后在android上加载?

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 是的windows上生成;线程都是demo默认的
if (type == mobilenet) {
mdlSolver.setThreadNum(1);
} else {
mdlSolver.setThreadNum(3);
}
只是替换了so和模型

@cocodark
Copy link
Contributor

linux和windows上换行符表示差异导致hash计算不一致,MDL在异常信息会抛出linux平台计算的hash值,手动替换下,这个问题之前有人提过。

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 嗯,惭愧没浏览大神们的Issues

@cocodark
Copy link
Contributor

ndk { abiFilters "armeabi-v7a" } externalNativeBuild { cmake { arguments "-DANDROID_ABI=armeabi-v7a with NEON" arguments "-DANDROID_ARM_MODE=arm", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE", "-DCMAKE_BUILD_TYPE=Release" cppFlags "-std=c++11 -fexceptions -frtti -flax-vector-conversions" } }这是我之前源码编译so的配置,你试试

@cocodark
Copy link
Contributor

"-DCMAKE_BUILD_TYPE=Release"我看你没有加这个控制选项

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 好的,release编译我是切换AS的变量来实现的

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 用你的编译参数运行了一下,使用转换后的model,运行时间8s,生成出来的so有5M左右,应该是没有经过优化

@cocodark
Copy link
Contributor

你的so体积超出太多,你检查下你的编译参数 和cmakeList.txt,正常so体积在500K左右

@2php
Copy link
Author

2php commented Oct 18, 2017

@cocodark 嗯,需要加上build.sh上面的一些参数,慢慢测试,非常感谢回复~~~

@cocodark
Copy link
Contributor

后面我会提供一个源码编译的Android demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants