Ask Your Question

Revision history [back]

Opencv-dnn's erformance declined significantly on Android with JNI

My OpenCV-dnn shared lib program running in an Android APP through JNI is 2x slower than the original standalone executable binary. Its CPU usage is halved. BTW, the aarch is armv8a here.

.so lib and binary share same code and compiling options. So I guess some openmp features used by opencv dnn are restricted in Android APP.

The code is too long and quite irrelevant here. Just something like this.

code in common parts:

void detect() { 
    while (true) { 
    Image img = GetImageFromV4l();
    /* do opencv dnn forwarding */
  }
}

JNI:

std::thread thr;
JNICALL Java_blabla_Detect() {
  // preprocessing, loading models, etc
  thr = std::thread(std::bind(detect));
}

Binary:

int main() {
   // preprocessing, loading models, etc
   detect();
  return 0;
}

Opencv compiling flags:

-isystem /data/build/android-ndk-r16b/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG

shared libs packaged with APK

And I did try to add -fopenmp flag, but that didn't help.

The following is the shared libs packaged with my APK. Did I miss something important for opencv/openmp?

libc++_shared.so  libcurl.so             libgnustl_shared.so  
libopencv_core.so        libopencv_flann.so      
libopencv_imgproc.so  libprotobuf.so        libtensorflow_mobile.so
libc++.so         libdl.so                      
libopenblas.so        libopencv_dnn.so         libopencv_highgui.so    
libopencv_videoio.so  libry_profile.so      
libc.so           libface_engine_jni.so       
libopencv_calib3d.so  libopencv_features2d.so  
libopencv_imgcodecs.so  libopencv_video.so    libstlport_shared.so
click to hide/show revision 2
retagged

updated 2019-01-10 01:54:21 -0600

berak gravatar image

Opencv-dnn's erformance declined significantly on Android with JNI

My OpenCV-dnn shared lib program running in an Android APP through JNI is 2x slower than the original standalone executable binary. Its CPU usage is halved. BTW, the aarch is armv8a here.

.so lib and binary share same code and compiling options. So I guess some openmp features used by opencv dnn are restricted in Android APP.

The code is too long and quite irrelevant here. Just something like this.

code in common parts:

void detect() { 
    while (true) { 
    Image img = GetImageFromV4l();
    /* do opencv dnn forwarding */
  }
}

JNI:

std::thread thr;
JNICALL Java_blabla_Detect() {
  // preprocessing, loading models, etc
  thr = std::thread(std::bind(detect));
}

Binary:

int main() {
   // preprocessing, loading models, etc
   detect();
  return 0;
}

Opencv compiling flags:

-isystem /data/build/android-ndk-r16b/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG

shared libs packaged with APK

And I did try to add -fopenmp flag, but that didn't help.

The following is the shared libs packaged with my APK. Did I miss something important for opencv/openmp?

libc++_shared.so  libcurl.so             libgnustl_shared.so  
libopencv_core.so        libopencv_flann.so      
libopencv_imgproc.so  libprotobuf.so        libtensorflow_mobile.so
libc++.so         libdl.so                      
libopenblas.so        libopencv_dnn.so         libopencv_highgui.so    
libopencv_videoio.so  libry_profile.so      
libc.so           libface_engine_jni.so       
libopencv_calib3d.so  libopencv_features2d.so  
libopencv_imgcodecs.so  libopencv_video.so    libstlport_shared.so