mtcnn icon indicating copy to clipboard operation
mtcnn copied to clipboard

Memory Leak

Open fisakhan opened this issue 4 years ago • 22 comments

I read 100,000 images from hard drive and run mtcnn on each image to detect faces. I do nothing else and notice that RAM (16 GB) utilization reaches 100%. This means there is memory leakage. How to solve this problem?

fisakhan avatar Apr 03 '20 22:04 fisakhan

Are you closing the file descriptors of your images after predictions? can you supply a demo code for this bug?

ipazc avatar Apr 03 '20 22:04 ipazc

detector = MTCNN()

for file_path in file_paths:
file_name=file_path.split('/')[-1] folder_name=file_path.split('/')[-2]

    try:
        image=cv2.cvtColor(cv2.imread(file_path), cv2.COLOR_BGR2RGB)            
        bbs_all, points_all = detector.detect_faces(image)
   except:
       print('Read error.')

fisakhan avatar Apr 04 '20 15:04 fisakhan

How to close the file descriptors of images after predictions?

fisakhan avatar Apr 04 '20 15:04 fisakhan

I use memory_profile to verify and yes it is memory leak problem in mtcnn. Sizes of my images are larger than 1000x1000 pixels. For smaller images 300x300, the leak is negligible.

fisakhan avatar Apr 06 '20 16:04 fisakhan

Even I am facing this issue. Any way to resolve this?

jokerisgod avatar Sep 21 '20 15:09 jokerisgod

@jokerisgod Instead of MTCNN I now use RetinaFace, which is better than MTCNN in not only memory but also in accuracy.

fisakhan avatar Sep 21 '20 17:09 fisakhan

Thank you @fisakhan

Can you point me to the repo which you're using?

jokerisgod avatar Oct 02 '20 08:10 jokerisgod

Hi, Solution: Upgrade your tensorflow version: 2.4.0.

When I used tensorflow version: 2.2.0, I got same memory leak issue. After that I upgraded tensorflow and problem solved.

cevdetcvr avatar Dec 17 '20 12:12 cevdetcvr

I read 100,000 images from hard drive and run mtcnn on each image to detect faces. I do nothing else and notice that RAM (16 GB) utilization reaches 100%. This means there is memory leakage. How to solve this problem?

Did you find any solution regarding this? I am also facing the same issue.

mohitwadhwa2 avatar Mar 17 '21 08:03 mohitwadhwa2

I read 100,000 images from hard drive and run mtcnn on each image to detect faces. I do nothing else and notice that RAM (16 GB) utilization reaches 100%. This means there is memory leakage. How to solve this problem?

Did you find any solution regarding this? I am also facing the same issue.

Hi, It's about tensorflow version. I updated tensorflow version and problem solved for me! My Tensorflow version: 2.5.0-dev20210223

cevdetcvr avatar Mar 17 '21 08:03 cevdetcvr

I face some problems with tensorflow versions. I didn't check but @cevdetcvr might be right.

fisakhan avatar Mar 18 '21 15:03 fisakhan

org.tensorflow:tensorflow-android:+ not able to change to version 2.4

Could not find org.tensorflow:tensorflow-android:2.4.0.

sheffiksheff avatar Mar 26 '21 12:03 sheffiksheff

@jokerisgod Instead of MTCNN I now use RetinaFace, which is better than MTCNN in not only memory but also in accuracy.

Can you attach the link to the RetinaFace implementation you are using?

himanshu-doi avatar Jun 30 '21 16:06 himanshu-doi

@himanshu-doi https://github.com/peteryuX/retinaface-tf2

fisakhan avatar Jul 01 '21 10:07 fisakhan

https://github.com/timesler/facenet-pytorch/issues/57#issuecomment-667614153

^This comment helped me resolve the memory spike issue for my api using mtcnn module. Try to limit the batch size for rnet and onet models. I can suggest these two methods:

  1. By increasing the confidence threshold for pnet and rnet models in mtcnn to ignore less confident predictions
  2. By reducing/limiting the input image resolution

High resolution images cause pnet to create a large number of box proposals which inflates the memory and increases latency for prediction.

himanshu-doi avatar Jul 09 '21 14:07 himanshu-doi

Just in case anyone still has this issue, adding torch.cuda.empty_cache() after the detect function solved the problem for me (using MTCNN).

adamhtoolwin avatar Jul 29 '21 00:07 adamhtoolwin

I'm currently working with tensor flow 2.10 I instantiated the detector and iteratively processing images. Unfortunately the gpu both gpu and system memory increase continuously until the process is killed by the system.

this is the an example of code producing such a problem.

` import cv2 from mtcnn import MTCNN

image_path = "/home/..../0001_01.jpg" img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB) face_detector = MTCNN() while(1): faces = face_detector.detect_faces(img) print("a") `

Have you some suggestion to solve this issue?

beppe2hd avatar Nov 07 '22 11:11 beppe2hd

您的邮件已接收!

ladyshen avatar Nov 07 '22 11:11 ladyshen

To fix the memory leak, update mtccn.py out = self._pnet.predict(img_y) out = self._rnet.predict(tempimg1) out = self._onet.predict(tempimg1) to out = self._pnet(img_y) out = self._rnet(tempimg1) out = self._onet(tempimg1)

https://stackoverflow.com/questions/64199384/tf-keras-model-predict-results-in-memory-leak

arewcheng avatar May 30 '23 01:05 arewcheng

您的邮件已接收!

ladyshen avatar May 30 '23 01:05 ladyshen

I'm currently working with tensor flow 2.10 I instantiated the detector and iteratively processing images. Unfortunately the gpu both gpu and system memory increase continuously until the process is killed by the system.

this is the an example of code producing such a problem.

` import cv2 from mtcnn import MTCNN

image_path = "/home/..../0001_01.jpg" img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB) face_detector = MTCNN() while(1): faces = face_detector.detect_faces(img) print("a") `

Have you some suggestion to solve this issue?

Hello, did you find any solution to this?

alexarwady avatar Jun 02 '23 09:06 alexarwady