Top Related Projects
The world's simplest facial recognition api for Python and the command line
State-of-the-art 2D and 3D Face Analysis Project
Pretrained Pytorch face detection (MTCNN) and facial recognition (InceptionResnet) models
Face recognition with deep neural networks.
🔥🔥High-Performance Face Recognition Library on PaddlePaddle & PyTorch🔥🔥
A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
Quick Overview
FaceNet is an open-source face recognition system implemented in TensorFlow. It's based on the paper "FaceNet: A Unified Embedding for Face Recognition and Clustering" by Schroff et al. The project provides pre-trained models and tools for face recognition, verification, and clustering.
Pros
- High accuracy in face recognition tasks
- Implements state-of-the-art deep learning techniques
- Provides pre-trained models for easy use
- Supports both face verification and face clustering
Cons
- Requires significant computational resources for training
- Documentation could be more comprehensive
- May require additional data preprocessing for optimal performance
- Limited support for real-time applications out of the box
Code Examples
- Face detection and alignment:
from facenet.src import facenet
from facenet.src.align import detect_face
# Load the MTCNN face detector
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
# Detect and align faces in an image
img = facenet.load_image(image_path)
faces = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
- Face embedding generation:
import tensorflow as tf
from facenet.src import facenet
# Load the pre-trained FaceNet model
model_path = 'path/to/pretrained/model'
facenet.load_model(model_path)
# Generate face embeddings
with tf.Session() as sess:
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
feed_dict = { images_placeholder: aligned_images, phase_train_placeholder:False }
emb = sess.run(embeddings, feed_dict=feed_dict)
- Face verification:
from scipy import spatial
# Compare two face embeddings
def verify_faces(emb1, emb2, threshold=0.7):
dist = spatial.distance.cosine(emb1, emb2)
return dist < threshold
Getting Started
-
Clone the repository:
git clone https://github.com/davidsandberg/facenet.git cd facenet
-
Install dependencies:
pip install -r requirements.txt
-
Download a pre-trained model from the project's GitHub page.
-
Use the provided scripts to perform face recognition tasks:
python src/align/align_dataset_mtcnn.py /path/to/input/dir /path/to/output/dir --image_size 160 --margin 32 --random_order --gpu_memory_fraction 0.25 python src/classifier.py TRAIN /path/to/aligned/faces /path/to/pretrained/model /path/to/classifier.pkl python src/validate_on_lfw.py /path/to/lfw/dir /path/to/pretrained/model
Competitor Comparisons
The world's simplest facial recognition api for Python and the command line
Pros of face_recognition
- Easier to use with a high-level API, requiring less deep learning expertise
- Built-in command-line tools for quick face recognition tasks
- Supports multiple backends (dlib, OpenCV) for flexibility
Cons of face_recognition
- Less customizable for advanced users or specific use cases
- May have lower accuracy compared to FaceNet in some scenarios
- Fewer options for fine-tuning and model selection
Code Comparison
face_recognition:
import face_recognition
image = face_recognition.load_image_file("image.jpg")
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)
FaceNet:
import tensorflow as tf
from facenet.src import facenet
with tf.Graph().as_default():
with tf.Session() as sess:
facenet.load_model("20180402-114759.pb")
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
feed_dict = {images_placeholder: images, phase_train_placeholder: False}
emb = sess.run(embeddings, feed_dict=feed_dict)
State-of-the-art 2D and 3D Face Analysis Project
Pros of InsightFace
- More recent and actively maintained repository
- Supports a wider range of face recognition tasks and models
- Offers pre-trained models with better performance on benchmarks
Cons of InsightFace
- Steeper learning curve due to more complex architecture
- Requires more computational resources for training and inference
- Less documentation and tutorials compared to FaceNet
Code Comparison
FaceNet:
embeddings = model.embeddings(images)
distance = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
loss = facenet.triplet_loss(anchor, positive, negative, alpha)
InsightFace:
embedding = model.get_feature(img)
sim = mx.symbol.dot(anchor, positive.T)
triplet_loss = mx.symbol.sum(mx.symbol.maximum(0, margin - pos + neg))
Both repositories provide implementations for face recognition tasks, but InsightFace offers more advanced features and better performance. FaceNet is simpler to use and has more documentation, making it suitable for beginners. InsightFace is more powerful but requires more expertise to utilize effectively. The code snippets show similar approaches to embedding generation and loss calculation, with InsightFace using MXNet and FaceNet using TensorFlow.
Pretrained Pytorch face detection (MTCNN) and facial recognition (InceptionResnet) models
Pros of facenet-pytorch
- Implemented in PyTorch, offering better integration with modern deep learning workflows
- Provides pre-trained models and easy-to-use inference pipelines
- Actively maintained with more recent updates
Cons of facenet-pytorch
- Limited to PyTorch ecosystem, potentially less flexible for non-PyTorch users
- Fewer pre-trained model options compared to the original FaceNet implementation
Code Comparison
FaceNet (TensorFlow):
import tensorflow as tf
import facenet
# Load model
model = facenet.load_model('path/to/model')
# Perform face recognition
embeddings = facenet.get_embeddings(images, model)
facenet-pytorch:
from facenet_pytorch import InceptionResnetV1, MTCNN
# Load model
resnet = InceptionResnetV1(pretrained='vggface2').eval()
# Perform face recognition
embeddings = resnet(images)
Both repositories implement the FaceNet architecture for face recognition, but facenet-pytorch offers a more modern PyTorch implementation with easier integration into current deep learning pipelines. The original FaceNet provides more pre-trained models and flexibility, while facenet-pytorch focuses on simplicity and PyTorch compatibility. The code comparison shows the difference in model loading and inference between the two implementations.
Face recognition with deep neural networks.
Pros of OpenFace
- Written in Python and Lua, making it more accessible to a wider range of developers
- Includes a web demo for easy visualization and testing
- Provides pre-trained models for immediate use
Cons of OpenFace
- Less actively maintained, with fewer recent updates
- Smaller community and fewer stars on GitHub
- Limited to facial recognition tasks, while FaceNet offers broader face analysis capabilities
Code Comparison
OpenFace (Python):
import openface
align = openface.AlignDlib("models/dlib/shape_predictor_68_face_landmarks.dat")
net = openface.TorchNeuralNet("models/openface/nn4.small2.v1.t7", 96)
FaceNet (Python with TensorFlow):
import tensorflow as tf
from models import inception_resnet_v1
images_placeholder = tf.placeholder(tf.float32, shape=(None, 160, 160, 3), name='input_image')
embeddings = inception_resnet_v1.inference(images_placeholder, keep_probability=1.0, phase_train=False)
Both repositories provide implementations for face recognition, but FaceNet offers a more comprehensive and actively maintained solution, while OpenFace focuses on simplicity and ease of use.
🔥🔥High-Performance Face Recognition Library on PaddlePaddle & PyTorch🔥🔥
Pros of face.evoLVe
- More recent and actively maintained repository
- Supports a wider range of backbone networks (ResNet, IR-SE, MobileNet, etc.)
- Includes more advanced loss functions (ArcFace, CosFace, SphereFace, etc.)
Cons of face.evoLVe
- Less documentation and tutorials compared to FaceNet
- Potentially more complex to set up and use for beginners
- Fewer pre-trained models available
Code Comparison
FaceNet:
embeddings = model.embeddings(images)
loss = facenet.triplet_loss(anchor, positive, negative, alpha)
face.evoLVe:
features = backbone(images)
thetas = head(features, labels)
loss = criterion(thetas, labels)
Both repositories provide implementations for face recognition tasks, but face.evoLVe offers more flexibility in terms of network architectures and loss functions. FaceNet, while older, has more extensive documentation and may be easier for beginners to use. face.evoLVe's code structure allows for easier experimentation with different backbones and heads, while FaceNet focuses primarily on the triplet loss approach.
A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
Pros of DeepFace
- More comprehensive, offering multiple face recognition models and additional capabilities like emotion detection
- Easier to use with higher-level abstractions and a more user-friendly API
- Actively maintained with regular updates and improvements
Cons of DeepFace
- May have slightly lower performance in some scenarios compared to FaceNet's implementation
- Larger package size due to inclusion of multiple models and features
- Potentially higher computational requirements for full functionality
Code Comparison
FaceNet:
import facenet
# Perform face recognition
embeddings = facenet.get_embeddings(images)
distances = facenet.compare_embeddings(embeddings[0], embeddings[1])
DeepFace:
from deepface import DeepFace
# Perform face recognition
result = DeepFace.verify(img1_path, img2_path)
is_match = result["verified"]
The code comparison shows that DeepFace offers a more straightforward API for face verification, while FaceNet provides lower-level access to embeddings and distances. DeepFace's approach is generally easier for beginners, but FaceNet's method allows for more fine-grained control over the recognition process.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Face Recognition using Tensorflow
This is a TensorFlow implementation of the face recognizer described in the paper "FaceNet: A Unified Embedding for Face Recognition and Clustering". The project also uses ideas from the paper "Deep Face Recognition" from the Visual Geometry Group at Oxford.
Compatibility
The code is tested using Tensorflow r1.7 under Ubuntu 14.04 with Python 2.7 and Python 3.5. The test cases can be found here and the results can be found here.
News
Date | Update |
---|---|
2018-04-10 | Added new models trained on Casia-WebFace and VGGFace2 (see below). Note that the models uses fixed image standardization (see wiki). |
2018-03-31 | Added a new, more flexible input pipeline as well as a bunch of minor updates. |
2017-05-13 | Removed a bunch of older non-slim models. Moved the last bottleneck layer into the respective models. Corrected normalization of Center Loss. |
2017-05-06 | Added code to train a classifier on your own images. Renamed facenet_train.py to train_tripletloss.py and facenet_train_classifier.py to train_softmax.py. |
2017-03-02 | Added pretrained models that generate 128-dimensional embeddings. |
2017-02-22 | Updated to Tensorflow r1.0. Added Continuous Integration using Travis-CI. |
2017-02-03 | Added models where only trainable variables has been stored in the checkpoint. These are therefore significantly smaller. |
2017-01-27 | Added a model trained on a subset of the MS-Celeb-1M dataset. The LFW accuracy of this model is around 0.994. |
2017‑01‑02 | Updated to run with Tensorflow r0.12. Not sure if it runs with older versions of Tensorflow though. |
Pre-trained models
Model name | LFW accuracy | Training dataset | Architecture |
---|---|---|---|
20180408-102900 | 0.9905 | CASIA-WebFace | Inception ResNet v1 |
20180402-114759 | 0.9965 | VGGFace2 | Inception ResNet v1 |
NOTE: If you use any of the models, please do not forget to give proper credit to those providing the training dataset as well.
Inspiration
The code is heavily inspired by the OpenFace implementation.
Training data
The CASIA-WebFace dataset has been used for training. This training set consists of total of 453 453 images over 10 575 identities after face detection. Some performance improvement has been seen if the dataset has been filtered before training. Some more information about how this was done will come later. The best performing model has been trained on the VGGFace2 dataset consisting of ~3.3M faces and ~9000 classes.
Pre-processing
Face alignment using MTCNN
One problem with the above approach seems to be that the Dlib face detector misses some of the hard examples (partial occlusion, silhouettes, etc). This makes the training set too "easy" which causes the model to perform worse on other benchmarks. To solve this, other face landmark detectors has been tested. One face landmark detector that has proven to work very well in this setting is the Multi-task CNN. A Matlab/Caffe implementation can be found here and this has been used for face alignment with very good results. A Python/Tensorflow implementation of MTCNN can be found here. This implementation does not give identical results to the Matlab/Caffe implementation but the performance is very similar.
Running training
Currently, the best results are achieved by training the model using softmax loss. Details on how to train a model using softmax loss on the CASIA-WebFace dataset can be found on the page Classifier training of Inception-ResNet-v1 and .
Pre-trained models
Inception-ResNet-v1 model
A couple of pretrained models are provided. They are trained using softmax loss with the Inception-Resnet-v1 model. The datasets has been aligned using MTCNN.
Performance
The accuracy on LFW for the model 20180402-114759 is 0.99650+-0.00252. A description of how to run the test can be found on the page Validate on LFW. Note that the input images to the model need to be standardized using fixed image standardization (use the option --use_fixed_image_standardization
when running e.g. validate_on_lfw.py
).
Top Related Projects
The world's simplest facial recognition api for Python and the command line
State-of-the-art 2D and 3D Face Analysis Project
Pretrained Pytorch face detection (MTCNN) and facial recognition (InceptionResnet) models
Face recognition with deep neural networks.
🔥🔥High-Performance Face Recognition Library on PaddlePaddle & PyTorch🔥🔥
A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot