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

memory leak in detect_face? #209

Open
yhino opened this issue Jun 15, 2023 · 3 comments
Open

memory leak in detect_face? #209

yhino opened this issue Jun 15, 2023 · 3 comments

Comments

@yhino
Copy link

yhino commented Jun 15, 2023

Hello, I have a problem with memory that keeps increasing after face detection on about 100 images.
Is there a solution or workaround to the problem?

The details are as fllows.

Package and versions:

python==3.8.15

facenet-pytorch==2.5.3
numpy==1.23.5
Pillow==9.5.0
psutil==5.9.5
torch==2.0.1
torchvision==0.15.2

Test code:

import psutil
import numpy as np
from glob import glob
from PIL import Image
from facenet_pytorch.models.mtcnn import MTCNN


def print_memory_usage(label):
	mem_info = psutil.Process().memory_info()
	print(f"{label: <10}: vms={mem_info.vms / (1024 * 1024)}, rss={mem_info.rss / (1024 * 1024)}")


detector = MTCNN(keep_all=True)
print_memory_usage("init")

for i, file in enumerate(glob("images/*")):
	img = Image.open(file)
	img_arr = np.asarray(img.copy(), dtype=np.uint8)
	# print_memory_usage(f"#{i}_load")
	
	_, _, _ = detector.detect(img_arr, landmarks=True)
	# print_memory_usage(f"#{i}_detect")
	
	img.close()
	del img
	del img_arr
	# print_memory_usage(f"#{i}_del")
print_memory_usage("detect")

del detector
print_memory_usage("deinit")

Test data:

  • Image taken with iPhone
    • Image size is 1280x960 ~ 4032x3024

Results:

% python check-memory.py
init      : vms=34063.64453125, rss=188.4609375
detect    : vms=34635.0234375, rss=534.6875
deinit    : vms=34632.1875, rss=531.8515625

The rss has increased by about 300 MB. Memory is not decreasing after deinit(del).

@timesler
Copy link
Owner

timesler commented Jul 3, 2023

Does the memory accumulate with each loop iteration or is it a one off jump of 300MB?

@yhino
Copy link
Author

yhino commented Jul 4, 2023

Thanks for the reply.
Memory is accumulated each loop iteration.

@ostwilkens
Copy link

I have a script which runs mtcnn.detect(pil_img, landmarks=False) many times per day.
It has been running without issues for months, but after a recent restart, it has started crashing due to full memory.
I'm far from sure it's facenet-pytorch however, I'm using lots of other libraries which could be causing this issue.

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

3 participants