Android OpenCV Tensorflow crashes with CvException because of native Memory leak

asked 2018-05-11 13:00:55 -0500

paatz04 gravatar image

updated 2018-05-12 06:09:14 -0500

Hi,

I'm loading a default tensorflow dnn model with:

    val config = getPath("ssd_mobilenet_v1_coco_2017_11_17.pbtxt", this)
    val model = getPath("frozen_inference_graph.pb", this)
    net = Dnn.readNetFromTensorflow(model, config)

and it works fine. But after less then 1-2 minutes the application crashes with CvException.

 CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.1) /build/master_pack-android/opencv/modules/core/src/matrix.cpp:362: error: (-215) u != 0 in function void cv::Mat::create(int, const int*, int)
]
    at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method)
    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1887)
    at org.opencv.android.JavaCameraView$JavaCameraFrame.rgba(JavaCameraView.java:322)
    at com.argos.android.opencv.activity.DnnActivity.onCameraFrame(DnnActivity.kt:84)
    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
    at java.lang.Thread.run(Thread.java:764)

Is there anything I can do to avoid this ?

image description

edit retag flag offensive close merge delete

Comments

I just used the Android Profiler and noticed that there is a memory leak, RAM goes up to 2 GB and then crashes the application - I'm using the sample code from the OpenCV tutorial for Caffee

paatz04 gravatar imagepaatz04 ( 2018-05-11 13:09:23 -0500 )edit

val blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR, Size(IN_WIDTH.toDouble(), IN_HEIGHT.toDouble()), Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false, true)

this line of code causes the memory leak. Not sure how I'd resolve this, cause the leak is happening in native code.

paatz04 gravatar imagepaatz04 ( 2018-05-11 13:20:22 -0500 )edit

val -- meaning: kotlin ?

also, your exception is from cvtColor (invalid input) not from the dnn.

berak gravatar imageberak ( 2018-05-11 14:04:27 -0500 )edit
1

Thank you @berak! Yes kotlin. But I tried it in java as well, and I'm having the same issues. Yes, I realised it's not the Net, but a memory leak caused by the function blobFromImage.

Native memory keeps rising until the app crashes. Even if i comment out everything else and only run the method blobFromImage. This seems like a bug with the library... I hope this is fixable somehow I also tried calling the 1 paramter blobFromImage without cropping ect. same memory leak

paatz04 gravatar imagepaatz04 ( 2018-05-11 14:10:32 -0500 )edit

ah right. if you really run out of mem, it might just crash anywhere ...

(also, while i DO like kotlin very much, you might want to lower the bar for anyone else trying to reproduce this, and stick with plain java, just saying)

berak gravatar imageberak ( 2018-05-12 12:00:00 -0500 )edit