GithubHelp home page GithubHelp logo

cosface's Introduction

Face recognition using Pytorch

Result

Contents

Intro

Face recognition in this repo performed using Pytorch, described in the papers:

facerec-cpp - C++ inference implementation with both hdf5 and protobuf database support.
facerec-ios - iOS inference implementation with protobuf database support.

QuickStart

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:
Result

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.

Train

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.

Dataset Cleanup

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


Pre-trained models

1.

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
  • Training statistics

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
  • Training statistics

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.

Compatibility

The code is tested using Pytorch 1.0 under OSX 10.14.5 and Ubuntu 16.04 with Python 3.6.

Inspiration

Original InsightFace implementation InsightFace_Pytorch Face_Pytorch face.evoLVe.PyTorch facenet

cosface's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.