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

Code crashes while calling native methods #6

Closed
Aman5692 opened this issue Apr 21, 2015 · 36 comments
Closed

Code crashes while calling native methods #6

Aman5692 opened this issue Apr 21, 2015 · 36 comments

Comments

@Aman5692
Copy link

I have been following
https://github.com/sh1r0/caffe-android-lib/blob/master/README.md
and as i run this
caffeMobile.loadModel(modelPath, weightsPath);

Code crashes with following logcat

04-20 18:06:07.692: D/dalvikvm(9250): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe.so 0x41496e98
04-20 18:06:07.782: D/dalvikvm(9250): Added shared lib /data/data/com.example.caffendk/lib/libcaffe.so 0x41496e98
04-20 18:06:07.782: D/dalvikvm(9250): No JNI_OnLoad found in /data/data/com.example.caffendk/lib/libcaffe.so 0x41496e98, skipping init
04-20 18:06:07.782: D/dalvikvm(9250): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x41496e98
04-20 18:06:07.792: D/dalvikvm(9250): Added shared lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x41496e98
04-20 18:06:07.832: W/dalvikvm(9250): No implementation found for native Lcom/example/caffendk/CaffeMobile;.loadModel:(Ljava/lang/String;Ljava/lang/String;)I
04-20 18:06:07.832: D/AndroidRuntime(9250): Shutting down VM
04-20 18:06:07.832: W/dalvikvm(9250): threadid=1: thread exiting with uncaught exception (group=0x40a13300)

04-20 18:06:07.842: E/AndroidRuntime(9250): FATAL EXCEPTION: main
04-20 18:06:07.842: E/AndroidRuntime(9250): java.lang.UnsatisfiedLinkError: Native method not found: com.example.caffendk.CaffeMobile.loadModel:(Ljava/lang/String;Ljava/lang/String;)I
04-20 18:06:07.842: E/AndroidRuntime(9250): at com.example.caffendk.CaffeMobile.loadModel(Native Method)
04-20 18:06:07.842: E/AndroidRuntime(9250): at com.example.caffendk.MainActivity.onCreate(MainActivity.java:25)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.Activity.performCreate(Activity.java:5008)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.os.Looper.loop(Looper.java:137)
04-20 18:06:07.842: E/AndroidRuntime(9250): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-20 18:06:07.842: E/AndroidRuntime(9250): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 18:06:07.842: E/AndroidRuntime(9250): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 18:06:07.842: E/AndroidRuntime(9250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-20 18:06:07.842: E/AndroidRuntime(9250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-20 18:06:07.842: E/AndroidRuntime(9250): at dalvik.system.NativeStart.main(Native Method)

I desperately need to run caffe on android, so can somebody suggest me what's going wrong.

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

I noticed that, you've changed the package name to com.example.caffendk.

04-20 18:06:07.842: E/AndroidRuntime(9250): java.lang.UnsatisfiedLinkError: Native method not found: com.example.caffendk.CaffeMobile.loadModel:(Ljava/lang/String;Ljava/lang/String;)I

While you did not change the package of native methods in jni part, thus the native methods cannot be found on java side.

@Aman5692
Copy link
Author

I changed the package name but it's not work even. There is no caffe-mobile/libs/armeabi-v7a/*.so, So copy .so file from caffe-android-demo.

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

If you wanna change the package name, you should build the lib on your own, i.e. ./build.py.
Otherwise, the package name of my pre-compiled lib is com.sh1r0.caffe_android_demo.

@Aman5692
Copy link
Author

static {
    System.loadLibrary("caffe");
    System.loadLibrary("caffe_jni");
}

String modelPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/deploy_mobile.prototxt";
String weightsPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel";
String imgPath = "/sdcard/Pictures/cat.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CaffeMobile obj = new CaffeMobile();
    int x = obj.loadModel(modelPath, weightsPath);
    Log.i("Print", "Model loaded successfully");
    int y = obj.predictImage(imgPath);
}

but log "Print : Model loaded successfully" is not printed :(

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Do you have native logs on logcat?

@Aman5692
Copy link
Author

these are the logs :
TAG : LOG
dalvikvm(22643): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe.so 0x414952a8
dalvikvm(22643): Added shared lib /data/data/com.example.caffendk/lib/libcaffe.so 0x414952a8
dalvikvm(22643): No JNI_OnLoad found in /data/data/com.example.caffendk/lib/libcaffe.so 0x414952a8, skipping init
dalvikvm(22643): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x414952a8
dalvikvm(22643): Added shared lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x414952a8

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Do you use the latest one (7945cce)?
Please provide me the native logs by adding the code below before calling loadModel:

obj.enableLog(true);

@Aman5692
Copy link
Author

Thanks sh1r0 for ur reply........
When i was following ur readme file i got stuck at one place

copy caffe-mobile/libs/armeabi-v7a/*.so to your jni lib directory

There were no .so files formed in this location, so i copied the .so files from the demo project and pasted in jni lib directory.
Since then i'm unable to call any jni method.

@Aman5692
Copy link
Author

Code above loadModel is

public class MainActivity extends Activity {

static {
    System.loadLibrary("caffe");
    System.loadLibrary("caffe_jni");
}

String modelPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/deploy_mobile.prototxt";
String weightsPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel";
String imgPath = "/sdcard/Pictures/cat.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CaffeMobile obj = new CaffeMobile();
    obj.enableLog(true);
    obj.loadModel(modelPath, weightsPath);

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Did you build your own libs? Is there any problem?

@Aman5692
Copy link
Author

No i just build caffe-android-lib using build.py script.
But .so file were not created in libs/armeabi-v7a folder.

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Sorry, I have no idea with such little information. Please provide the logs during building.

@Aman5692
Copy link
Author

This is the only logcat that is generated.

04-20 21:59:47.422: E/Trace(10796): error opening trace file: No such file or directory (2)
04-20 21:59:47.862: D/dalvikvm(10796): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe.so 0x414944c0
04-20 21:59:47.962: D/dalvikvm(10796): Added shared lib /data/data/com.example.caffendk/lib/libcaffe.so 0x414944c0
04-20 21:59:47.962: D/dalvikvm(10796): No JNI_OnLoad found in /data/data/com.example.caffendk/lib/libcaffe.so 0x414944c0, skipping init
04-20 21:59:47.962: D/dalvikvm(10796): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x414944c0
04-20 21:59:47.962: D/dalvikvm(10796): Added shared lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x414944c0

If you want i can upload the project on gmail and share with you, please provide your gmail id.

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

You seemed to paste the logs from logcat (Android), instead of the building logs.
Did you follow this to build your own libs? If the building process is done without error, there should be .sos in caffe-mobile/libs/armeabi-v7a/.

@Aman5692
Copy link
Author

i followed https://github.com/sh1r0/caffe-android-lib#build when i run build.py i got these error :
Traceback (most recent call last):
File "./build.py", line 131, in
main(sys.argv[1:])
File "./build.py", line 125, in main
build_protobuf()
File "./build.py", line 68, in build_protobuf
ndk_build()
File "./build.py", line 60, in ndk_build
call(['ndk-build', '-j', str(jobs)])
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(_popenargs, *_kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in init
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory

and
There is no any folder like "caffe-mobile/libs/armeabi-v7a/" and no .so's

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Then what do the logs look like? If the building is done without any errors, your shell should output messages like below.

Install        : libcaffe.so => libs/armeabi-v7a/libcaffe.so
SharedLibrary  : libcaffe_jni.so
Install        : libcaffe_jni.so => libs/armeabi-v7a/libcaffe_jni.so

@Aman5692
Copy link
Author

i followed https://github.com/sh1r0/caffe-android-lib#build when i run build.py i got these error :
Traceback (most recent call last):
File "./build.py", line 131, in
main(sys.argv[1:])
File "./build.py", line 125, in main
build_protobuf()
File "./build.py", line 68, in build_protobuf
ndk_build()
File "./build.py", line 60, in ndk_build
call(['ndk-build', '-j', str(jobs)])
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(popenargs, *kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in init
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory

and
There is no any folder like "caffe-mobile/libs/armeabi-v7a/" and no .so's

@sh1r0
Copy link
Owner

sh1r0 commented Apr 21, 2015

Which ndk version do you use?
I just had a clean try, with ./build.py ~/android-ndk-r9/, and built with success.

@Aman5692
Copy link
Author

I'm using android-ndk-r10

@Aman5692
Copy link
Author

I tried to build using android-ndk-r9, but got this :

inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/optical_flow.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/motion_stabilizing.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/fast_marching.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/deblurring.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/fast_marching_inl.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/log.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/videostab.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/inpainting.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/videostab/global_motion.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv2/opencv_modules.hpp
creating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cvaux.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/highgui.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cvwimage.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cxcore.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cxmisc.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cv.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cxcore.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cvaux.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cv.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/ml.h
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/include/opencv/cxeigen.hpp
inflating: OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCVModules_armeabi-release.cmake
Traceback (most recent call last):
File "build.py", line 131, in
main(sys.argv[1:])
File "build.py", line 125, in main
build_protobuf()
File "build.py", line 68, in build_protobuf
ndk_build()
File "build.py", line 60, in ndk_build
call(['ndk-build', '-j', str(jobs)])
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(_popenargs, *_kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in init
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory

@sh1r0
Copy link
Owner

sh1r0 commented Apr 22, 2015

AFAIK, both r9, r10 are okay.
It seemed that the error happened when you tried to build protobuf. Could you check that protobuf-2.5.0.tar.bz2 is correctly downloaded in your directory? And if your extraction is successful, there should be a bunch of files in protobuf/jni/ like:

aclocal.m4      configure                     install-sh        protobuf-lite.pc.in
Android.mk      configure.ac                  INSTALL.txt       protobuf.pc.in
Application.mk  CONTRIBUTORS.txt              java              python
autogen.sh      COPYING.txt                   ltmain.sh         README.txt
CHANGES.txt     depcomp                       m4                src
config.guess    editors                       Makefile.am       vsprojects
config.h        examples                      Makefile.in
config.h.in     generate_descriptor_proto.sh  missing
config.sub      gtest                         ndk-configure.sh

@Aman5692
Copy link
Author

My protobuf/jni folder has all the files and folders mentioned above. So it seems protobuf is downloaded correctly

@sh1r0
Copy link
Owner

sh1r0 commented Apr 22, 2015

Could you try to manually run ndk-build in protobuf/?

@Aman5692
Copy link
Author

I tried to manually build protobuf, and it compiles well and .so files are formed in local/libs
But i'm still not able to call any native method from android

@sh1r0
Copy link
Owner

sh1r0 commented Apr 22, 2015

So, do you have .sos in caffe-mobile/libs/armeabi-v7a/ now?

@Aman5692
Copy link
Author

There is no libs folder in caffe-mobile

@sh1r0
Copy link
Owner

sh1r0 commented Apr 22, 2015

Hmm... Actually, protobuf is one of the dependencies, and thus you need to re-run ./build.py <path to your ndk> to build the others.

@Aman5692
Copy link
Author

I run ./build.py several times but getting same error every time :

Traceback (most recent call last):
File "./build.py", line 132, in
main(sys.argv[1:])
File "./build.py", line 126, in main
build_protobuf()
File "./build.py", line 69, in build_protobuf
ndk_build()
File "./build.py", line 61, in ndk_build
call(['ndk-build', '-j', str(jobs)])
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(_popenargs, *_kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in init
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory

@sh1r0
Copy link
Owner

sh1r0 commented Apr 22, 2015

Could you provide the details about how you build, e.g. your commands?

@Aman5692
Copy link
Author

I tried to build again from scratch using your script build.py, and i got these messages at the last.
Can you tell if these were the expected messages, or there is some error in build.

/home/caffe/caffe-android-lib/caffe-mobile/jni/../../Boost-for-Android/build/lib//libboost_thread-gcc-mt-1_55.a(thread.o):thread.cpp:function non-virtual thunk to boost::exception_detail::bad_alloc_::~bad_alloc_(): warning: relocation refers to discarded section
/home/caffe/caffe-android-lib/caffe-mobile/jni/../../Boost-for-Android/build/lib//libboost_thread-gcc-mt-1_55.a(thread.o):thread.cpp:function non-virtual thunk to boost::exception_detail::bad_exception_::~bad_exception_(): warning: relocation refers to discarded section
/home/caffe/caffe-android-lib/caffe-mobile/jni/../../Boost-for-Android/build/lib//libboost_thread-gcc-mt-1_55.a(thread.o):thread.cpp:function non-virtual thunk to boost::exception_detail::error_info_injectorboost::thread_resource_error::~error_info_injector(): warning: relocation refers to discarded section
[armeabi-v7a] Install : libcaffe.so => libs/armeabi-v7a/libcaffe.so
[armeabi-v7a] Compile++ arm : caffe_jni <= caffe_jni.cpp
[armeabi-v7a] Compile++ arm : caffe_jni <= caffe_mobile.cpp
[armeabi-v7a] SharedLibrary : libcaffe_jni.so
[armeabi-v7a] Install : libcaffe_jni.so => libs/armeabi-v7a/libcaffe_jni.so

@sh1r0
Copy link
Owner

sh1r0 commented Apr 23, 2015

Yep, those warnings can be bypassed. 👍

@Aman5692
Copy link
Author

Hi sh1r0,
this is the code ......

package com.sh1r0.caffe_android_demo;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("SdCardPath")
public class MainActivity extends ActionBarActivity {
final String TAG = "Cerca Trova";

static {
    System.loadLibrary("caffe");
    System.loadLibrary("caffe_jni");
}

String modelPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/deploy_mobile.prototxt";
String weightsPath = "/sdcard/caffe_mobile/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel";
String imgPath = "/sdcard/Pictures/p.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toast.makeText(getBaseContext(), "I'm in", Toast.LENGTH_LONG).show();
    setContentView(R.layout.activity_main);
    Toast.makeText(getBaseContext(), "Going to load model", Toast.LENGTH_LONG).show();
    CaffeMobile obj = new CaffeMobile();
    Log.i(TAG, "Object created");
    obj.enableLog(true);
    Log.i(TAG, "Native log enabled");
    Toast.makeText(getBaseContext(), "object created", Toast.LENGTH_LONG).show();
    obj.loadModel(modelPath, weightsPath);
    Toast.makeText(getBaseContext(), "model loaded", Toast.LENGTH_LONG).show();
    Log.i(TAG, "Model loaded successfully");
    int y = obj.predictImage(imgPath);
    Toast.makeText(getBaseContext(), Integer.toString(y), Toast.LENGTH_LONG).show();
    Log.i(TAG, "Prediction made");
}

}

while building this time, all .so files were generated properly which and i pasted them from caffe-mobile/libs/armeabi-v7a/*.so to jni lib directory

My device has all the required stuff like prototxt, solver and .model file.

But still when i run the code i dont see any toast or log file, but still the app is not crashing, can u suggest what's going wrong and how can i get the results.

@sh1r0
Copy link
Owner

sh1r0 commented Apr 24, 2015

Could you provide the logs? Or, please try caffe-android-demo first to see if there is any problem.

@Aman5692
Copy link
Author

Logs are same still same as earlier

04-24 13:21:51.552: E/Trace(13701): error opening trace file: No such file or directory (2)
04-24 13:21:52.014: D/dalvikvm(13701): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe.so 0x41495fe8
04-24 13:21:52.093: D/dalvikvm(13701): Added shared lib /data/data/com.example.caffendk/lib/libcaffe.so 0x41495fe8
04-24 13:21:52.093: D/dalvikvm(13701): No JNI_OnLoad found in /data/data/com.example.caffendk/lib/libcaffe.so 0x41495fe8, skipping init
04-24 13:21:52.093: D/dalvikvm(13701): Trying to load lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x41495fe8
04-24 13:21:52.103: D/dalvikvm(13701): Added shared lib /data/data/com.example.caffendk/lib/libcaffe_jni.so 0x41495fe8

But i will try caffe-android-demo ....

@Aman5692
Copy link
Author

Caffe-android-demo works good on my device.
Can you tell how i can use your code to load my finetuned mnist model

@sh1r0
Copy link
Owner

sh1r0 commented Apr 24, 2015

AFAIW, in Android Studio projects, *.sos are preferably placed in jniLibs/armeabi-v7a/.
If you want to use your own model, you should add a prototxt which is modified from your training one, and use memory data layer instead.

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