ageitgey / ageitgey/face_recognition

Method to release dlib resources to manage GPU resources

Open
#899 8 comments 4 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

(relates to #868 #722)

  • face_recognition version: 1.2.3 (face_recognition.__version__)
  • Python version: 3.6.8
  • Operating System: Ubuntu Xenial 18.04
Description of problem

Dlib cleanly removes its memory consumption when necessary objects are deleted (or go out of scope). However, face_recognition instantiates module variables that get instantiated during import (here and here) which make it hard to delete for long running apps to be able to conserve GPU memory. The two biggest memory consumers are cnn_face_detection_model and face_encoder which keep persistent in memory and if we are implementing a web server similar to your example here, we run out of memory very quickly after a few API calls.

To solve this and manage memory, I have this current workaround implemented in my code:

import dlib
import face_recognition

# In my class where I use face_recognition

def clean_dlib(self):
        del face_recognition.api.cnn_face_detector 
        del face_recognition.api.face_encoder 

def init_dlib(self):
        face_recognition.api.cnn_face_detector = dlib.cnn_face_detection_model_v1(face_recognition.api.cnn_face_detection_model)
        face_recognition.api.face_encoder = dlib.face_recognition_model_v1(face_recognition.api.face_recognition_model)

I call self.init_dlib() before I use your methods and self.clean_dlib() right after, like so:

self.init_dlib()
face_locations = face_recognition.face_locations(...)
face_encodings = face_recognition.face_encodings(...)
<do the rest of face_comparison etc>
self.clean_dlib()

Obviously, this results in a speed decrease because the model and encodings get reloaded, but it lets me manage memory and not run out just after a few calls (I have a 1050Ti 4GB)

Ask

I was wondering if you have an alternate suggestion or, maybe, would consider a clean API to clear resources?

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 in face_recognition/api.py at the module variables linked in the issue, then compare their use with examples/web_service_example.py. Trace how cnn_face_detector and face_encoder are created and used, and review the related issues #868 and #722. Done should be a documented, clean API for releasing these resources so long-running applications can manage GPU memory.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.