ageitgey / face_recognition

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

When using GPU with CNN model, I am not getting any better results than Hog #1143

Open abhianand7 opened 4 years ago

abhianand7 commented 4 years ago

Description

I switched to CNN model from Hog and using the below code.

image1_locations = face_recognition.face_locations(image2, number_of_times_to_upsample=2, model='cnn') image2_locations = `face_recognition.face_locations(image2, number_of_times_to_upsample=2, model='cnn') image1_encoding = face_recognition.face_encodings(image1, known_face_locations=image1_locations, model='large', num_jitters=1)[0]

image2_encoding = face_recognition.face_encodings(image2, known_face_locations=image2_locations, model='large', num_jitters=1)[0]

face_distance = face_recognition.face_distance([image1_encoding], image2_encoding)`

dlib has been compiled with cuda support as well.

dlib.DLIB_USE_CUDA return True

I am using this facial location to compare with another image to calculate facial distance, but the facial distance did not improve at all. is there something that I'm missing.

Any help would be really great @ageitgey

Crisfw commented 4 years ago

I also want to ask the same question.

petar-varga commented 4 years ago

To truly test if a GPU is used with cnn model, please use nvidia-smi to check the usage. If the GPU is idle when training/detecting, it means it doesn't use CUDA. I had similar issues where the times were the same for hog and cnn - the issue was improper configuration. I was on Windows, and I can't quite remember what was the fix for me. I think it was mainly the usage of correct CUDA drivers, SDKs etc.

abhianand7 commented 4 years ago

@petar-varga I did check the GPU usage using nvidia-smi and the library is using it but still no improvements.

petar-varga commented 4 years ago

Hmm, that is weird. The GPU and memory utilization jump to what kind of values?

Could you try adding the following and see what is the result when you run your script?

import dlib.cuda as cuda
print(cuda.get_num_devices())
abhianand7 commented 4 years ago

@petar-varga

import dlib.cuda as cuda print(cuda.get_num_devices()) this does return 1.

as for GPU memory usage Screenshot from 2020-05-22 14-14-29

Screenshot from 2020-05-22 14-15-08

I am not sure where is the problem exactly. It does seem to be using GPU properly as expected.

petar-varga commented 4 years ago

@abhianand7 Does the performance (when measured with timeit module for example) vary if you use the CNN vs hog model? I'm trying to find out if the GPU is actually getting loaded, or in other words if this problem is due to the cuda/dlib/cudnn misconfiguration or the face_recognition library causing the issue.

abhianand7 commented 4 years ago

@petar-varga Yes I do see 2x-3x speed improvements when using CNN with GPU vs when using the HOG model, but apparently no accuracy improvements.

petar-varga commented 4 years ago

@abhianand7 Hmm, that's out of my depth then... Maybe @ageitgey could chime in and explain what could be the reason for same accuracy?

abhianand7 commented 4 years ago

@petar-varga I have been trying to explore dlib as well to understand the reason for this behavior. Next I will directly try to use dlib and compare to pin-point the reason for this behavior.

petar-varga commented 4 years ago

@abhianand7 That sounds good. Maybe try and find discrepancies between face_recognition calls to dlib and direct usage of dlib. It is my understanding that CNN should provide speed improvement in training/detecting and better accuracy compared to the HOG model.

Let us know if you manage to find the reason for this behavior.

aliizadi commented 4 years ago
* face_recognition version: 1.3.0

* Python version:3.6.9

* Operating System:Ubuntu 18.04

Description

I switched to CNN model from Hog and using the below code.

image1_locations = face_recognition.face_locations(image2, number_of_times_to_upsample=2, model='cnn') image2_locations = `face_recognition.face_locations(image2, number_of_times_to_upsample=2, model='cnn') image1_encoding = face_recognition.face_encodings(image1, known_face_locations=image1_locations, model='large', num_jitters=1)[0]

image2_encoding = face_recognition.face_encodings(image2, known_face_locations=image2_locations, model='large', num_jitters=1)[0]

face_distance = face_recognition.face_distance([image1_encoding], image2_encoding)`

dlib has been compiled with cuda support as well.

dlib.DLIB_USE_CUDA return True

I am using this facial location to compare with another image to calculate facial distance, but the facial distance did not improve at all. is there something that I'm missing.

Any help would be really great @ageitgey

Hi @abhianand7, CNN method is used for face location(face detection) in Dlib, not for face encoding. As you can see in the API after finding the face location by each of CNN or hog you call face_encodings which uses deep metric learning to create face embeddings. So in your case, both hog or CNN methods find the same face location and that's why you see the same face encodings for both. In summary, the CNN method gives you a better result in finding face locations, not face encoding. Here is a good description of differences between hog and CNN. Here is also a good description for deep metric learning.