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

ncnn-mobilenet #353

Closed
Nanamare opened this issue Apr 13, 2018 · 4 comments
Closed

ncnn-mobilenet #353

Nanamare opened this issue Apr 13, 2018 · 4 comments

Comments

@Nanamare
Copy link

Nanamare commented Apr 13, 2018

I try to apply mobilenet_V1 using NCNN.
Current, I make successfully new_mobilenet.bin, new_mobilenet.param (old model -> new model -> ncnn model), also clone this repository

but Occurred error, only changes mobilenet param, bin file from squeezenet.
The mobilenet.bin file is loaded, not loads all binaries.

D/SqueezeNcnn: load_model 1278216 17103120

The param file is not loaded at all.

  Build fingerprint: 'HUAWEI/ALP-L29/HWALP:8.0.0/HUAWEIALP-L29/126(C636):user/release-keys'
    Revision: '0'
    ABI: 'arm'
    pid: 14841, tid: 14841, name: ent.squeezencnn  >>> com.tencent.squeezencnn <<<
    signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x10
    Cause: null pointer dereference
        r0 00000022  r1 00000000  r2 ae703f55  r3 f56e94f7
        r4 00000070  r5 00000000  r6 d369f130  r7 00000000
        r8 d369eee4  r9 007685dd  sl 00000010  fp ff83247c
        ip f5728634  sp ff832468  lr d366d1d7  pc d366d1d8  cpsr 600e0030
    backtrace:
        #00 pc 000101d8  /data/app/com.tencent.squeezencnn-_xKl3q3AI_OqGRyayxXDVA==/lib/arm/libsqueezencnn.so
        #01 pc 0000913f  /data/app/com.tencent.squeezencnn-_xKl3q3AI_OqGRyayxXDVA==/lib/arm/libsqueezencnn.so (Java_com_tencent_squeezencnn_SqueezeNcnn_Init+106)
        #02 pc 00002143  /data/app/com.tencent.squeezencnn-_xKl3q3AI_OqGRyayxXDVA==/oat/arm/base.odex (offset 0x2000)
@nihui
Copy link
Member

nihui commented Apr 13, 2018

the library in https://github.com/dangbo/ncnn-mobile is too old, try updating ncnn library

@Nanamare
Copy link
Author

@nihui
thanks but i cloned release(Android), same issue.
T.T plz assign project member..!

build.gradle

apply plugin: 'com.android.application'

android {
        compileSdkVersion 27
    defaultConfig {
        applicationId "com.nanamare.mac.mobilenet"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang"
                cFlags "-fopenmp -O2 -fvisibility=hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
                cppFlags "-fopenmp -O2 -fvisibility=hidden -fvisibility-inlines-hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
                cppFlags ""
                cppFlags "-std=c++11"
                cppFlags "-frtti"
                cppFlags "-fexceptions"
            }
        }
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'//, 'x86', 'x86_64', 'armeabi'
            stl "gnustl_static"
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    ext {
        nativeDebuggable = true
    }



}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Project hierarchy
2018-04-16 10 26 15

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})

include_directories(src/main/cpp/include
                    src/main/cpp/)

file(GLOB MOBILNET_SRC src/main/cpp/*.h
                    src/main/cpp/*.cpp)

add_library(libncnn STATIC IMPORTED)
set_target_properties(libncnn
  PROPERTIES IMPORTED_LOCATION
  ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libncnn.a)

add_library(objectclassification SHARED ${MOBILNET_SRC})

find_library(log-lib log)
target_link_libraries(objectclassification
                       libncnn
                       jnigraphics
                       z
                       ${log-lib})

native-lib.cpp

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_nanamare_mac_mobilenet_MobileNetNCNN_init(JNIEnv *env, jobject instance,
                                                   jbyteArray params_, jbyteArray bin_,
                                                   jbyteArray labels_) {
    jbyte *params = env->GetByteArrayElements(params_, NULL);
    jbyte *bin = env->GetByteArrayElements(bin_, NULL);
    jbyte *labels = env->GetByteArrayElements(labels_, NULL);

    {
        int len = env->GetArrayLength(params_);
        mobileNetParams.resize(len);
        env->GetByteArrayRegion(params_, 0, len, (jbyte*)mobileNetParams.data());
        int ret = mobileNet.load_param(mobileNetParams.data());
        __android_log_print(ANDROID_LOG_DEBUG, "MobileNet params", "load params %d %d", ret, len);
    }

    {
        int len = env->GetArrayLength(bin_);
        mobileNetBin.resize(len);
        env->GetByteArrayRegion(bin_, 0, len, (jbyte*)mobileNetBin.data());
        int ret = mobileNet.load_model(mobileNetBin.data());
        __android_log_print(ANDROID_LOG_DEBUG, "MobileNet binary", "load binary %d %d", ret, len);
    }

    {
        int len = env->GetArrayLength(labels_);
        std::string words_buffer;
        words_buffer.resize(len);
        env->GetByteArrayRegion(labels_, 0, len, (jbyte*)words_buffer.data());
        mobileNetLabels = split_string(words_buffer, "\n");
    }

    env->ReleaseByteArrayElements(params_, params, 0);
    env->ReleaseByteArrayElements(bin_, bin, 0);
    env->ReleaseByteArrayElements(labels_, labels, 0);

    return JNI_TRUE;
}

but error log (load_param not working)

04-16 10:29:08.671 21431-21431/com.nanamare.mac.mobilenet D/MobileNet params: load params **0** 10562
04-16 10:29:08.678 21431-21431/com.nanamare.mac.mobilenet D/MobileNet binary: load binary **0** 17103120

but static ncnn::Net mobileNet; correctly mapping
2018-04-16 10 32 38

@nihui
Copy link
Member

nihui commented Apr 16, 2018

you shall pass the buffer of mobilenet.param.bin instead of the plain param text file.
mobilenet.param.bin can be generated via ncnn2mem utility.

@Nanamare
Copy link
Author

@nihui
Success. Thank you!!!

04-16 13:36:01.444 28035-28035/com.nanamare.mac.mobilenet D/MobileNet params: load params 5088 5088
04-16 13:36:01.452 28035-28035/com.nanamare.mac.mobilenet D/MobileNet binary: load binary 17103120 17103120

Is it not optional mobilenet.param.bin? (mandatory?)

2018-04-16 1 37 14

@nihui nihui closed this as completed Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants