Face recognition in this repo performed using Pytorch, described in the papers:
- ArcFace: Additive Angular Margin Loss for Deep Face Recognition. (recomended)
- CosFace: Large Margin Cosine Loss for Deep Face Recognition.
- A Discriminative Feature Learning Approach for Deep Face Recognition.
facerec-cpp - C++ inference implementation with both hdf5
and protobuf
database support.
facerec-ios - iOS inference implementation with protobuf
database support.
git clone https://github.com/egcode/facerec.git
cd facerec
Install all dependencies from requirements.txt
.
Download playground datased to data/dataset_got
folder with command:
python3 download_data.py --download_type dataset_got
Download pretrained model to data/pth
:
python3 download_data.py --download_type pth
Generate dataset .h5
file where labels and embeddings stored:
python3 app/export_embeddings.py \
--model_path ./data/pth/IR_50_MODEL_arcface_ms1celeb_epoch90_lfw9962.pth \
--data_dir ./data/dataset_got/dataset_lanister_raw/ \
--output_dir data/out_embeddings/ \
--model_type IR_50 \
--is_aligned 0 \
--with_demo_images 1 \
--image_size 112 \
--image_batch 5 \
--h5_name dataset_lanister.h5
That's it. Now everything is ready to recognize faces from image or live camera.
Face recognition with image:
python3 app/face_recognition_on_image.py \
--image_path ./data/dataset_got/test3.jpg \
--model_path ./data/pth/IR_50_MODEL_arcface_ms1celeb_epoch90_lfw9962.pth \
--model_type IR_50 \
--unknown_face unknown \
--max_threshold 0.6 \
--distance_metric 1 \
--font_size 0.5 \
--h5_name ./data/out_embeddings/dataset_lanister.h5
Result should be like this image:
Face recognition with live camera is pretty much the same but just with face_recognition_live_cam.py
file. Make sure to generate .h5
file with faces that will be visible on camera.
git clone https://github.com/egcode/facerec.git
cd facerec
Install all dependencies from requirements.txt
if you haven't already.
Download all required validation datasets:
LFW, CALFW, CPLFW, CFP_FF, CFP_FP
and Casia-Webface dataset:
python3 download_data.py --download_type train
this command will download all required datasets to data
folder.
Start training:
python3 train.py
After training started, make sure out
folder created in the project root. All logs, saved model points and tensorboard logs are saved here.
To start tensorboard to view training process just run:
python3 logger.py
and open http://localhost:6006/
to view training process.
git clone https://github.com/egcode/facerec.git
cd facerec
Install all dependencies from requirements.txt
if you haven't already.
Download dataset_got
playground dataset, if you haven't with a command
python3 download_data.py --download_type dataset_got
After downloading complete, path data/dataset_got/dataset_targarien_aligned_112_dirty
is the folder that simulates dirty dataset, where images that starts with DIRT*
represent noise that should be removed.
To cleanup our dataset first we should export embeddings for each image to a .h5
file.
python3 dataset_cleanup/export_dataset_embeddings.py \
./data/pth/IR_50_MODEL_arcface_ms1celeb_epoch90_lfw9962.pth \
./data/dataset_got/dataset_targarien_aligned_112_dirty/ \
--model_type IR_50 \
--image_batch 5 \
--h5_name dataset_targarien_aligned_112_dirty.h5
NOTE: If you are using your own images, make sure that all of them are pre-aligned. Use align_dataset_mtcnn.py
in .align
folder to align if needed,
Now we are ready to perform cleanup.
python3 dataset_cleanup/cluster_clean_dataset.py \
--affinity cosine \
--linkage average \
--distance_threshold 0.7 \
--h5_name data/dataset_targarien_aligned_112_dirty.h5 \
--output_clean_dataset data/dataset_got/dataset_targarien_112_clean \
--output_failed_images data/dataset_got/dataset_targarien_112_dirt
Cluster clean generates two new folders
dataset_targarien_112_dirt
- all dirt that was cleaned out.
dataset_targarien_112_clean
- and new clean dataset without any dirt
If embeddings exported with model that trained with Arface or Cosface affinity
it is recommended to be cosine
, if it's Center loss it's euclidean
. If more information needed about affinity
or linkage
it could be found here
Distance threshold cut out images that are too far to form a cluster. Raw representation is in the image below
Download | Training dataset | Architecture |
---|---|---|
Link | M1-Celeb | IR_50 |
-
Performance
LFW CFP_FF CFP_FP CALFW CPLFW 99.62 99.26 90.09 92.65 81.15 -
Parameters
python3 train.py \
--model_type IR_50 \
--data_dir ./data/MS_Celeb_1M_112 \
--batch_size 128 \
--batch_size_test 128 \
--evaluate_batch_size 128 \
--criterion_type arcface \
--total_loss_type softmax \
--optimizer_type sgd_bn \
--margin_s 32.0 \
--margin_m 0.5 \
--validation_set_split_ratio 0.0 \
--lr 0.1 \
--lr_schedule_steps 30 55 75 \
--apex_opt_level 2
Download | Training dataset | Architecture |
---|---|---|
Link | M1-Celeb | MobileNet_V2 |
-
Performance
LFW CFP_FF CFP_FP CALFW CPLFW 96.93 96.84 79.26 80.95 61.27 -
Parameters
python3 train.py \
--model_type MobileNet_V2 \
--data_dir ./data/MS_Celeb_1M_112_filtered \
--batch_size 512 \
--batch_size_test 512 \
--evaluate_batch_size 512 \
--criterion_type arcface \
--total_loss_type softmax \
--optimizer_type sgd_bn \
--margin_s 32.0 \
--margin_m 0.5 \
--validation_set_split_ratio 0.0 \
--lr 0.1 \
--lr_schedule_steps 20 40 60 \
--epochs 100 \
--apex_opt_level 2 \
--model_save_latest_path ./pth_latest
NOTE: If you use any of the models here, please do not forget to give proper credit to those providing the training dataset as well.
The code is tested using Pytorch 1.0 under OSX 10.14.5 and Ubuntu 16.04 with Python 3.6.
Original InsightFace implementation InsightFace_Pytorch Face_Pytorch face.evoLVe.PyTorch facenet