ageitgey / ageitgey/face_recognition

Running face_encodings in parallel gives RuntimeError: cudaGetDevice()... reason: initialization error

Open
#751 4 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 version: 1.2.3
  • Python version: 3.7.2
  • Operating System: Scientific Linux 7.6

I have dlib installed with GPU support.

>>> import dlib
>>> dlib.DLIB_USE_CUDA
True

I'm using batch_face_locations() to get image locations. Then for each location that was pulled out of the batch, I'm getting the encodings using face_encodings(). I time both of these operations, and the time get the encodings is about 3x longer than the time to get the locations. I supposed that I could speed up the time to get the encodings by getting them all in parallel. So I tried something like this:

import multiprocessing as mp
import face_recognition

def get_encoding(frame, face_locations, return_queue):
    encode = face_recognition.face_encodings(frame, face_locations)
    return_queue.put(encode)

all_batch_face_locations = ... # the frames and associated batch_face_locations returned for all images in my dataset

encodings = []
for frames, batch_face_locs in all_batch_face_locations:
    # get the encodings for the current batch of images in parallel
    procs = []
    queues = []
    for frame_number_in_batch, face_locations in enumerate(batch_face_locs):
        q = mp.Queue()
        p = mp.Process(
            target=get_encoding, 
            args=(frames[frame_number_in_batch], face_locations, q))
        p.start()
        procs.append(p)
        queues.append(q)
        
    for p, q in zip(procs, queues):
        p.join()
        encoding = q.get()
        encodings.append(encoding)

Yet this gives me an error:

...
RuntimeError: Error while calling cudaGetDevice(&the_device_id) in file /tmp/pip-install-2vh9r_rp/dlib/dlib/cuda/gpu_data.cpp:178. code: 3, reason: initialization error

Now I can't actually find anywhere that says that face_recognition.face_encodings() uses the GPU. Even the dlib documentation for the function that face_recognition eventually calls doesn't mention it. But it seems to be using it nonetheless.

I see references in other issues (#98 #374 #649) to running face_encodings() on multiple CPU cores, and I'd at least like to try and experiment with that to see if I can get some improvement. Is there something I'm missing to allow me to run batch_face_locations on the GPU and face_encodings on the CPU? Or, if not, is there some way to also run the encodings on the GPU in batches?

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 by reproducing the multiprocessing example with face_recognition.batch_face_locations() and face_encodings() on the listed Python and dlib setup. Review the referenced issues (#98, #374, and #649) and the dlib.shape_predictor documentation; done means an agreed, tested execution path or clear documentation of the supported CPU/GPU behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
18/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.