ageitgey / ageitgey/face_recognition

Also running out of memory

Open
#699 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
56.8k
Forks
13.7k
PR merge metrics
No merged PRs in 30d

Description

face-recognition==1.2.3, face-recognition-models==0.3.0
Python version: 3.4.3
Operating System: Ubuntu 14.04
Django 2.0.5
Celery 4.2.1
Redis 2.10.6

Description

I am still getting the out of memory errors after recognizing 2-5 faces/per image in multiple images, one after the other. When I say "one image after the other", I mean it takes about 30-60 sec for me to select the next image from the disk and load it into the program. I was hoping to make this last step automatic by loading multiple images at a time, but this memory error makes that impossible.

The error I get is

[2018-12-08 09:29:15,604: WARNING/ForkPoolWorker-5] Traceback (most recent call last):
[2018-12-08 09:29:15,604: WARNING/ForkPoolWorker-5] File "/home/mark/python-projects/memorabilia-JSON/biometric_identification/tasks.py", line 75, in find_faces_task
    face_locations = face_recognition.face_locations(image, model="cnn", number_of_times_to_upsample=0)
[2018-12-08 09:29:15,604: WARNING/ForkPoolWorker-5] File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/face_recognition/api.py", line 116, in face_locations
    return [_trim_css_to_bounds(_rect_to_css(face.rect), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, "cnn")]
[2018-12-08 09:29:15,605: WARNING/ForkPoolWorker-5] File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/face_recognition/api.py", line 100, in _raw_face_locations
    return cnn_face_detector(img, number_of_times_to_upsample)
[2018-12-08 09:29:15,605: WARNING/ForkPoolWorker-5] MemoryError: std::bad_alloc

My system resources from top:

top - 10:59:59 up 3 days, 17:13,  1 user,  load average: 1.40, 1.33, 1.26
Tasks: 368 total,   4 running, 289 sleeping,   0 stopped,   2 zombie
%Cpu(s):  0.2 us, 12.0 sy,  0.0 ni, 87.1 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
KiB Mem : 16343740 total,  4346864 free,  9669116 used,  2327760 buff/cache
KiB Swap:   999420 total,       60 free,   999360 used.  5877912 avail Mem 

I seem to have ~4.3 GB of free memory, so I am not sure if adding more memory is the answer.

What I Did

I am running the face recognition in a celery task, called from my django app, where I select the image to load, which is on disk.

image = face_recognition.load_image_file(image_path)
face_locations = face_recognition.face_locations(image, model="cnn", number_of_times_to_upsample=0)
face_encodings = face_recognition.face_encodings(image, known_face_locations=face_locations)

I then save the face_locations and face_encodings to a mysql db for later comparison to some known faces.

Each of the images are 3 - 4 MB in size (See examples in #670)

I am using model='cnn' based on your recommendation in #670, because without cnn some faces in my test set were not being found. Now they are found, but I need to reset the celery application after each face

I noticed that you mention image size as a possible cause of this problem (#691). Do you have any insights on the relationship between image size and recognition accuracy? I was thinking perhaps to use PIL to reduce the size of the image before it is loaded into face_recognition, but I am not sure of an 'optimal' image size, and how reducing the size would impact the accuracy of finding and recognizing faces. Do you have any recommendations on an optimal image size for creating face locations and face encodings? Or, perhaps a range of sizes?

Also, when doing facial recognition, does the unknown image need to be close to the resolution of the images used to create the encoding and location? Or, are encoding and location independent of resolution?

I don't see any "clean-up" commands to run after the face_recognition is done to free up memory in the api docs. Are there any such commands or options?

Thanks!

Mark

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing call at tasks.py line 75 and the face_recognition.face_locations(..., model="cnn") stack trace; compare the image-size context in #670 and #691. Determine whether the report supports a reproducible memory fix or only guidance on image sizing and cleanup; done is a documented, verified recommendation or a narrowly scoped fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
machine-learning, mysql, python, redis
Domain
backend, machine-learning, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.