Labeled Faces in the Wild for Face Recognition

Nowadays, a new state-of-the-art face recognition model is raised everyday. Researchers adopted Labeled faces in the wild or shortly LFW data set as a de facto standard to evaluate face recognition models and compare with existing ones. Luckily, scikit-learn provides LFW data set as an out-of-the-box module. In this post, we will evaluate a state-of-the-art model on LFW data set within scikit-learn API.

arya-in-hall-of-faces
Arya in Hall of Faces, Game of Thrones

BTW, I pushed the source code of this study to GitHub. You can support this study if you star⭐️ the repo πŸ™.


πŸ™‹β€β™‚οΈ You may consider to enroll my top-rated machine learning course on Udemy

Decision Trees for Machine Learning

Vlog

You can either follow this tutorial or watch the following video. They both cover the building a facial recognition pipeline with deepface for python and testing it on LFW data set.

Loading LFW data set

fetch_lfw_pairs function loads LFW data set. Default calling loads images in gray scale. That’s why, I’ll set its color argument to true. Besides, default usage loads train set images but in this post, I’ll just evaluate an existing model on LFW data set. I just need test set. That’s why, I’ll set subset argument to test. Finally, fetch_lfw_pairs function decreases the image resolution. Setting resize argument to 1 saves the original size.

from sklearn.datasets import fetch_lfw_pairs
fetch_lfw_pairs = fetch_lfw_pairs(subset = 'test'
, color = True, resize = 1)

LFW pairs store image pairs and its label as same person or different persons.

pairs = fetch_lfw_pairs.pairs
labels = fetch_lfw_pairs.target
target_names = fetch_lfw_pairs.target_names

There are 1000 instances in the test set. First half of them are same person whereas second half is different persons.

actuals = []; predictions = []
for i in range(0, pairs.shape[0]):
   pair = pairs[i]
   img1 = pair[0]
   img2 = pair[1]

   fig = plt.figure()

   ax1 = fig.add_subplot(1,3,1)
   plt.imshow(img1/255)

   ax2 = fig.add_subplot(1,3,2)
   plt.imshow(img2/255)

   ax3 = fig.add_subplot(1,3,3)
   plt.text(0, 0.50, target_names[labels[i]])

   plt.show()

Data set stores low resolution images as seen. This will be challenging for a face recognition model.

lfw-pairs
LFW pairs

Face recognition app

We will use deepface package for python for face recognition.

Face recognition task

We’ve retrieved pair images in the code block above. Face recognition task will be handled in the same for loop. DeepFace package for python can handle face recognition with a few lines of code. It wraps several state-of-the-art face recognition models: VGG-Face, Google FaceNet, OpenFace, Facebook DeepFace, DeepID and Dlib ResNet. I can switch the face recognition model by specifying the model name argument in the verify function. I will use Dlib ResNet model in this experiment.

#!pip install deepface
from deepface import DeepFace

#deepface expects bgr instead of rgb
img1 = img1[:,:,::-1]; img2 = img2[:,:,::-1]
obj = DeepFace.verify(img1, img2
   , model_name = 'Dlib', distance_metric = 'euclidean')
prediction = obj["verified"]
predictions.append(prediction)

actual = True if labels[i] == 1 else False
actuals.append(actual)

Evaluation

We stored actual and prediction labels in the dedicated variables. Sklearn offers accuracy metric calculations as out-of-the-functions as well.





from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
accuracy = 100*accuracy_score(actuals, predictions)
precision = 100*precision_score(actuals, predictions)
recall = 100*recall_score(actuals, predictions)
f1 = 100*f1_score(actuals, predictions)

The performance of dlib on LFW data set for test subset is shown below. Results seem satisfactory for low resolution pairs.

instances = 1000
accuracy ='92.7 %'
precision = '94.20289855072464 %'
recall = '91.0 %'
f1 = '92.5737538148525 %'

Confusion matrix might give some insights as well.

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(actuals, predictions)
print(cm)

tn, fp, fn, tp = cm.ravel()
print(tn, fp, fn, tp)

Confusion matrix is demonstrated below.

cm = [
   [472,  28],
   [ 45, 455]
]

true_negative = 472
false_positive = 28
false_negative = 45
true_positive = 455

Conclusion

So, we’ve mentioned how to evaluate a face recognition model on LFW data set within scikit-learn API. I plan to add the accuracy metrics of all models wrapped in deepface package soon.

I pushed the source code of this study to GitHub. You can support this study if you star⭐️ the repo πŸ™.


Like this blog? Support me on Patreon

Buy me a coffee


8 Comments

  1. I’m getting a value error in the 6th cell in your ipynb that you uploaded in github.
    “ValueError: (‘Detected face shape is ‘, (0, 92, 3), ‘. Consider to set enforce_detection argument to False.’)”
    What’s the problem here?

    1. No face found in one of the image pair you passed. Please follow the instruction in the exception message.

      1. After putting enforce_detection = False, new error appears.

        Now showing “TypeError: Cannot handle this data type: (1, 1, 3), <f4".

        How can I feed high resolution images? Is there any way to filter out small images? As this is a fixed datset by scikit learn. Can I manually select my dataset folder or any other way?
        Thanks for the reply.

  2. hi sefik , i want to apply deepface model on other database which is not in sklearn. Can you help me wit code where dataset has classes and classes has images. Then how to make pairs and perform verfication.

  3. I am running DeepFace models on the different dataset and when i use mtcnn as backend detector , RAM gets exhausted using colab or kaggle. Any solution?? And another thing i want to know if i run these models on CPU instead of GPU, Will result vary??

  4. Hello Sefik Sir , How to use YOLO detector within DeepFace as backend detector. Have you posted it?

  5. i ran the same code as in video or blog, but accuracy is 50%. I am using google colab and written the same code as in your github , video or blog. But accuracy is 50%. Sir please tell me the solution.

Comments are closed.