ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.3k stars 13.48k forks source link

Slow first calls to face_encodings() #1160

Open dalgibbard opened 4 years ago

dalgibbard commented 4 years ago

Description

Hi all, it seems that the very first call to face-locations/face-encodings methods are slower than future calls. In particular face_recognition.face_encodings() is taking a long time - looking through the code; it looks like it calls this:

pose_predictor_68_point = dlib.shape_predictor(predictor_68_point_model)

Is there a way to pre-warm this (if that's even feasible?)

It's the difference between:

What I Did

        locations_start = get_time_ms()
        face_locations = face_recognition.face_locations(
            rgb_small_frame,
            number_of_times_to_upsample=1,
            model="cnn")
        print(f"Locations time: {get_time_ms() - locations_start}")

        encodings_start = get_time_ms()
        face_encodings = face_recognition.face_encodings(
            rgb_small_frame, face_locations,
            num_jitters=1,
            model="large")
        print(f"Encodings time: {get_time_ms() - encodings_start}")
luckenbach commented 4 years ago

I am seeing akin behavior and I am able to reproduce it via the benchmark.py script provided by face_recgnition inside of the examples dir

when running this command I prefixed with time just to get an understanding of how long the script itself ran as individual tests seem to indicate the proper speed.



Timings at 240p:
 - Face locations: 0.0679s (14.73 fps)
 - Face landmarks: 0.0030s (335.78 fps)
 - Encode face (inc. landmarks): 0.0047s (213.45 fps)
 - End-to-end: 0.0671s (14.90 fps)

Timings at 480p:
 - Face locations: 0.2395s (4.18 fps)
 - Face landmarks: 0.0027s (371.71 fps)
 - Encode face (inc. landmarks): 0.0041s (245.03 fps)
 - End-to-end: 0.2451s (4.08 fps)

Timings at 720p:
 - Face locations: 0.5400s (1.85 fps)
 - Face landmarks: 0.0025s (397.96 fps)
 - Encode face (inc. landmarks): 0.0043s (231.60 fps)

 - End-to-end: 0.5481s (1.82 fps)

Timings at 1080p:
 - Face locations: 1.2067s (0.83 fps)
 - Face landmarks: 0.0025s (406.06 fps)
 - Encode face (inc. landmarks): 0.0043s (230.46 fps)
 - End-to-end: 1.2102s (0.83 fps)

real    30m8.689s
user    30m2.780s
sys 0m10.721s```
jc508 commented 2 years ago

just some more evidence Nov 2021 Jetson Nano dlib.DLIB_USE_CUDA = True dlib version = 19.22.0 dlib compiled = Nov 14 2021 16:36:45 face_recognition.version = 1.2.3 cv2.version = 4.1.1

I also note that, not using the benchmark, the first call to face_recognition.face_locations with model="cnn" will take 22seconds to return subsequent calls take about 0.5 sec if using model="hog" then the 1st call takes 0.6 sec as do all the subsequent calls.

Benchmarks (Note: All benchmarks are only using a single CPU core)
Timings at 240p:
 - Face locations: 0.2627s (3.81 fps)
 - Face landmarks: 0.0072s (139.34 fps)
 - Encode face (inc. landmarks): 0.0278s (35.97 fps)
 - End-to-end: 0.2887s (3.46 fps)

Timings at 480p:
 - Face locations: 1.0106s (0.99 fps)
 - Face landmarks: 0.0073s (136.88 fps)
 - Encode face (inc. landmarks): 0.0282s (35.48 fps)
 - End-to-end: 1.0451s (0.96 fps)

Timings at 720p:
 - Face locations: 2.2683s (0.44 fps)
 - Face landmarks: 0.0074s (135.88 fps)
 - Encode face (inc. landmarks): 0.0283s (35.37 fps)
 - End-to-end: 2.3093s (0.43 fps)

Timings at 1080p:
 - Face locations: 5.3314s (0.19 fps)
 - Face landmarks: 0.0075s (133.54 fps)
 - Encode face (inc. landmarks): 0.0286s (34.95 fps)
 - End-to-end: 5.3916s (0.19 fps)