Skip to content

Guide · Open source

How to use facenet: clone, set up and run the face recognition code

Updated · written with AI from the project's own documentation and checked against our catalogue data

facenet is an MIT-licensed Python project by davidsandberg that implements the FaceNet face recognizer in TensorFlow. It turns a face photo into a list of numbers (an embedding) so that photos of the same person sit close together. This guide explains what the repository contains, what it needs to run, and how to approach its training, alignment and pretrained-model features.

FaceNetdavidsandberg/facenet · ★ 14.4k · MIT

Quick start

  1. 1Confirm your versions. Check that your setup matches the README's tested configuration of TensorFlow r1.7, Python 2.7 or 3.5, and Ubuntu 14.04. Plan for extra work if you use newer releases.
  2. 2Clone the repository. Run the git clone command from the README to download davidsandberg/facenet to your machine.
  3. 3Pick a pretrained model. Choose between 20180408-102900 (CASIA-WebFace, LFW 0.9905) and 20180402-114759 (VGGFace2, LFW 0.9965) based on your needs.
  4. 4Run validation. Follow the wiki page Validate on lfw and include --use_fixed_image_standardization when running validate_on_lfw.py.
  5. 5Prepare your data with alignment. Use the MTCNN code in src/align to align faces before training, as the README describes.
  6. 6Train or classify. Follow the linked wiki guides for softmax training on CASIA-WebFace or for training a classifier on your own images.

What facenet is and what it includes

facenet 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, and its code is heavily inspired by the OpenFace implementation.

The repository includes training code, face alignment using MTCNN, and pretrained models. The README reports that the best pretrained model reaches an LFW accuracy of 0.99650+-0.00252.

  • Training code for models using softmax loss and triplet loss
  • Face alignment with a Python/TensorFlow implementation of MTCNN in src/align
  • Pretrained Inception-ResNet-v1 models trained on CASIA-WebFace and VGGFace2

Check the compatibility before you start

The README is explicit about what the code was tested with. It says the code is tested using TensorFlow r1.7 under Ubuntu 14.04 with Python 2.7 and Python 3.5. Older news entries also say it was updated to run with TensorFlow r1.0 and r0.12, and that it is not sure whether it runs with older versions.

If you plan to use a current TensorFlow or Python release, expect to do compatibility work yourself. The README does not say the code works with newer versions.

  • Tested: TensorFlow r1.7, Ubuntu 14.04, Python 2.7 and 3.5
  • Not documented: current TensorFlow or Python releases

Clone the repository

The install method is clone-and-build. Start by cloning the repository from GitHub with the command in the README. The project is not packaged as a pip install, so you work from the cloned source folder.

git clone https://github.com/davidsandberg/facenet.git

Choose a pretrained model

The README lists two pretrained models. Both use the Inception ResNet v1 architecture. Model 20180408-102900 was trained on CASIA-WebFace with an LFW accuracy of 0.9905. Model 20180402-114759 was trained on VGGFace2 with an LFW accuracy of 0.9965, and the README describes VGGFace2 as the dataset behind the best performing model.

The models need fixed image standardization. The README says to add the option --use_fixed_image_standardization when running a script such as validate_on_lfw.py. If you use any of the models, the README asks you to credit the providers of the training dataset.

  • Use --use_fixed_image_standardization with validation scripts such as validate_on_lfw.py
  • Credit the training dataset providers when you use a model
--use_fixed_image_standardization

Validate on LFW and train your own model

To check accuracy, the README points to the wiki page Validate on lfw. For training, it says the best results come from softmax loss. The README links to a wiki page on classifier training of Inception-ResNet-v1 on CASIA-WebFace, and a separate wiki page on training a classifier on your own images.

Note that the README's news section says the older facenet_train.py was renamed to train_tripletloss.py and facenet_train_classifier.py was renamed to train_softmax.py. Use the current file names when you follow older tutorials.

  • Validation guide: the wiki page Validate on lfw
  • Softmax training guide: Classifier training of Inception-ResNet-v1
  • Own-image classifier guide: Train a classifier on own images

Understand what the alignment step does

The README explains that face alignment uses MTCNN, a multi-task CNN. The author found that the Dlib face detector missed some hard examples such as partial occlusion and silhouettes, which made training data too easy. The repository's Python/TensorFlow MTCNN implementation is in src/align. The README says it does not give identical results to the Matlab/Caffe version, but performance is very similar.

What people use it for

  • Reproducing the FaceNet paper results with TensorFlow
  • Testing a pretrained embedding model on the LFW benchmark
  • Training a face classifier on your own labelled image set using the linked wiki
  • Studying how a face recognition model is trained and evaluated
  • Using the MTCNN alignment code as a preprocessing step

Frequently asked questions

Is facenet free to use?

Yes. The project is licensed under MIT. The README also asks that if you use any of the pretrained models, you give proper credit to the providers of the training dataset.

What do I need to run facenet?

The README says the code is tested with TensorFlow r1.7 under Ubuntu 14.04 with Python 2.7 and Python 3.5. It does not document newer versions, so check compatibility before you start.

How do I install facenet?

Clone the repository with git clone https://github.com/davidsandberg/facenet.git. The install method is clone-and-build, and the README gives no packaged install command.

Is facenet a ready-made face unlock or photo tagging app?

No. The README describes research code, not an end-user product. It is a starting point for building or testing face recognition systems.

What are the alternatives to facenet?

The comparable projects listed for this item are InsightFace, dlib, OpenFace and DeepFace. OpenFace is the project that the facenet code is heavily inspired by.

Why does my model give poor results without standardization?

The README says the models need fixed image standardization. Add --use_fixed_image_standardization when you run scripts such as validate_on_lfw.py, otherwise the input is not standardized as the model expects.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Use facenet if you are a researcher, developer or student who wants a documented TensorFlow FaceNet codebase with pretrained embedding models and training code to study or extend. Pick another tool if you need a plug-and-play face unlock or tagging app, a current TensorFlow or Python stack, or guidance on legal and ethical use of face data, since the README does not cover those.

Install options & downloads