Ask Your Question

Revision history [back]

Android Java Dnn.forward multiple output layers -> segmentation fault

Hello all,

I want to run a yolov3-tiny darknet model on an android device using opencv. that yolo model has 2 output layers named "yolo_16" and "yolo_23". When i try to run the model with either one of these layers as output the code works as expected:

net.forward(outputs, "yolo_16")

but when i try to get both layers in one call like in these examples

  • https://answers.opencv.org/question/205657/darknet-yolo-extract-data-from-dnnforward/
  • https://github.com/opencv/opencv/issues/12441#issuecomment-434702795

i get a segmentation fault error like this:

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 5691 (Thread-8)


the opencv version is 3.4.3 installed according to these instructions on android studio.

a reduced version of the code that i use is here:

public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 {

    private Mat                  mRgba;
    private Net                  net;
    private List<String>         ln;
    private CameraBridgeViewBase mOpenCvCameraView;

    private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
        @Override public void onManagerConnected(int status) {
            if (status == LoaderCallbackInterface.SUCCESS) { mOpenCvCameraView.enableView(); }
            else { super.onManagerConnected(status); }
        }
    };

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 200);
            return;
        }

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.color_blob_detection_activity_surface_view);
        mOpenCvCameraView.setMaxFrameSize(720, 720);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
        net = Dnn.readNetFromDarknet(getFile("yolov3-tiny.cfg", this), getFile("yolov3-tiny.weights", this));
        ln = getOutputNames(net);

    }
    public void onCameraViewStopped() {
        mRgba.release();
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();

        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
        Mat inputBlob = Dnn.blobFromImage(mRgba, 1.0, new Size(416,416), new Scalar(0), false, false);

        net.setInput(inputBlob);
        List<Mat> output = new ArrayList<Mat>();

        net.forward(output, ln);

        inputBlob.release();
        return mRgba;
    }

    @Override public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null) { mOpenCvCameraView.disableView(); }
    }

    @Override public void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) { OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback); }
        else { mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); }
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null) { mOpenCvCameraView.disableView(); }
    }



    private static MatOfByte getFile(String file, Context context) {
        AssetManager assetManager = context.getAssets();
        BufferedInputStream inputStream = null;
        try {
            // Read data from assets.
            inputStream = new BufferedInputStream(assetManager.open(file));
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();

            return new MatOfByte(data);
        } catch (IOException ex) { }
        return new MatOfByte();
    }

    private static List<String> getOutputNames(Net net) {
        List<String> names = new ArrayList<>();

        List<Integer> outLayers = net.getUnconnectedOutLayers().toList();
        List<String> layersNames = net.getLayerNames();

        outLayers.forEach((item) -> names.add(layersNames.get(item - 1)));
        return names;
    }

and the full error is here:

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 5691 (Thread-8)

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'HUAWEI/ANE-LX1/HWANE:8.0.0/HUAWEIANE-LX1/178(C432):user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 5634, tid: 5691, name: Thread-8  >>> com.example.opencv_yolo <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
Cause: null pointer dereference
    x0   0000000000000000  x1   0000000000000000  x2   0000000000000000  x3   0000000000000000
    x4   000000742cf09380  x5   0000000000000000  x6   000000743b66b3c0  x7   0000000000000000
    x8   34b0d45095ce85c8  x9   34b0d45095ce85c8  x10  000000000000006b  x11  000000743b76b140
    x12  000000743b76b160  x13  000000743b76b1e0  x14  00000000ffffffff  x15  000000000016b200
    x16  000000743eccdcb8  x17  000000743ec6b948  x18  0000000000000400  x19  000000741cfc3c30
    x20  0000000000000000  x21  0000000000000000  x22  000000743b7fcbf0  x23  0000000000000000
    x24  0000000000000000  x25  000000741cfc3b10  x26  0000000000000000  x27  000000743b7fcbf0
    x28  000000741cfc3c30  x29  000000741cfc3c90  x30  000000741e5fdb14
    sp   000000741cfc3a70  pc   000000741e5fde10  pstate 0000000060000000
backtrace:
    #00 pc 000000000087ce10  /data/app/com.example.opencv_yolo-SCQM5fgihxChzYIsVWiIiQ==/lib/arm64/libopencv_java3.so (_ZN2cv3dnn22experimental_dnn_34_v73Net7forwardERKNS_12_OutputArrayERKSt6vectorINS_6StringESaIS7_EE+1056)
    #01 pc 00000000001c8088  /data/app/com.example.opencv_yolo-SCQM5fgihxChzYIsVWiIiQ==/lib/arm64/libopencv_java3.so (Java_org_opencv_dnn_Net_forward_14+364)
    #02 pc 0000000000512700  /system/lib64/libart.so (art_quick_generic_jni_trampoline+144)
    #03 pc 0000000000509638  /system/lib64/libart.so (art_quick_invoke_static_stub+600)
    #04 pc 00000000000d80b4  /system/lib64/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+260)
    #05 pc 00000000002821dc  /system/lib64/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+352)
    #06 pc 000000000027c8a4  /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+672)
    #07 pc 00000000004f2130  /system/lib64/libart.so (MterpInvokeStatic+468)
    #08 pc 00000000004fae94  /system/lib64/libart.so (ExecuteMterpImpl+14612)
    #09 pc 000000000025d620  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+444)
    #10 pc 0000000000263d20  /system/lib64/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+212)
    #11 pc 000000000027c884  /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+640)
    #12 pc 00000000004f0b0c  /system/lib64/libart.so (MterpInvokeVirtual+612)
    #13 pc 00000000004fad14  /system/lib64/libart.so (ExecuteMterpImpl+14228)
    #14 pc 000000000025d620  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+444)
    #15 pc 0000000000263d20  /system/lib64/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+212)
    #16 pc 000000000027c884  /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+640)
    #17 pc 00000000004f1b58  /system/lib64/libart.so (MterpInvokeInterface+1732)
    #18 pc 00000000004faf14  /system/lib64/libart.so (ExecuteMterpImpl+14740)
    #19 pc 000000000025d620  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+444)
    #20 pc 0000000000263d20  /system/lib64/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+212)
    #21 pc 000000000027c884  /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+640)
    #22 pc 00000000004f0b0c  /system/lib64/libart.so (MterpInvokeVirtual+612)
    #23 pc 00000000004fad14  /system/lib64/libart.so (ExecuteMterpImpl+14228)
    #24 pc 000000000025d620  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+444)
    #25 pc 0000000000263d20  /system/lib64/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+212)
    #26 pc 000000000027c884  /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+640)
    #27 pc 00000000004f1b58  /system/lib64/libart.so (MterpInvokeInterface+1732)
    #28 pc 00000000004faf14  /system/lib64/libart.so (ExecuteMterpImpl+14740)
    #29 pc 000000000025d620  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+444)
    #30 pc 00000000004e4a88  /system/lib64/libart.so (artQuickToInterpreterBridge+1468)
    #31 pc 000000000051281c  /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)
    #32 pc 0000000000509384  /system/lib64/libart.so (art_quick_invoke_stub+580)
    #33 pc 00000000000d8078  /system/lib64/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+200)
    #34 pc 000000000043111c  /system/lib64/libart.so (_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_8ArgArrayEPNS_6JValueEPKc+104)
    #35 pc 00000000004322a8  /system/lib64/libart.so (_ZN3art35InvokeVirtualOrInterfaceWithJValuesERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_jmethodIDP6jvalue+432)
    #36 pc 0000000000458e88  /system/lib64/libart.so (_ZN3art6Thread14CreateCallbackEPv+1140)
    #37 pc 00000000000671b8  /system/lib64/libc.so (_ZL15__pthread_startPv+36)
    #38 pc 000000000001eee4  /system/lib64/libc.so (__start_thread+68)

does anyone know what this means, or how it can be fixed?

best regards, marco