timesler / facenet-pytorch

Pretrained Pytorch face detection (MTCNN) and facial recognition (InceptionResnet) models
MIT License
4.54k stars 951 forks source link

memory leak in detect_face? #209

Open yhino opened 1 year ago

yhino commented 1 year ago

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:

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 commented 1 year ago

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

yhino commented 1 year ago

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

ostwilkens commented 1 year ago

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.