serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
10.82k stars 1.91k forks source link

Running on GPU for Facenet face verification (Jupyter Lab) #194

Closed zul96a closed 3 years ago

zul96a commented 3 years ago

Hello! Currently I'm developing a project on Jupyter lab running on a server. At first before this, the notebook uses CPU and return a time of around 7 seconds when verifying one pair of images. But, when I switch to GPU, the running time increases to 11 seconds. I'm not sure which part I'm doing it wrong.

Here are my screenshot of my code and nvidia-smi image

image

serengil commented 3 years ago

How many cpu cores you have? If the number of cpu cores is a lot, then it is very normal to have fast speed with cpu.

import multiprocessing
print(multiprocessing.cpu_count())

Setting CUDA_VISIBLE_DEVICES param to -1 will run it on cpu.

zul96a commented 3 years ago

I checked and the machine has 16 cores CPU. Is there any way to increase the speed of face verification drastically while still using Facenet?

serengil commented 3 years ago

You can build FaceNet model once and store in the memory.

model = DeepFace.build_model('Facenet')

Then, use this pre-built model in verification tasks.

DeepFace.verify(selfie_path, MyKad_path, model_name = 'Facenet', model = model, detector_backend = 'mtcnn', distance_metric = 'cosine')

Model building is the most costly stage. In this way, you can build it once and you will not spend time to build it anymore.

zul96a commented 3 years ago

Ok thanks! Sir, you really reply very quick. I can't thank you enough!

serengil commented 3 years ago

read me of the repo explains that kind of tricks. I recommend you to read the read-me well.

please do not forget to star the repo :)