GithubHelp home page GithubHelp logo

seasonsh / docface Goto Github PK

View Code? Open in Web Editor NEW
369.0 20.0 124.0 666 KB

Face recognition system for ID photos

License: MIT License

Python 96.48% MATLAB 3.52%
face-recognition face-verification biometrics tensorflow multi-gpu

docface's Introduction

DocFace: Matching ID Document Photos to Selfies

By Yichun Shi and Anil K. Jain

Update Notes

  • 09/28/2018: Added a demo code for face alignment.
  • 09/20/2018: The content of DocFace+ is added, including DIAM-Softmax, partially shared sibling networks and support for multiple ID/selfie per class.

Contents

  1. Introduction
  2. Citation
  3. Requirements
  4. Usage
  5. Models
  6. Results

Introduction

This repository includes the TensorFlow implementation of DocFace and DocFace+, which is a system proposed for matching ID photos and live face photos. DocFace is shown to significantly outperform general face matchers on the ID-Selfie matching problem. We here give the example training code and pre-trained models in the paper. For the preprocessing part, we follow the repository of SphereFace to align the face images using MTCNN. The user can also use other methods for face alignment. Because the dataset used in the paper is private, we cannot publish it here. One can test the system on their own dataset.

Citation

@article{shi2018docface+,
  title = {DocFace+: ID Document to Selfie Matching},
  author = {Shi, Yichun and Jain, Anil K.},
  booktitle = {arXiv:1809.05620},
  year = {2018}
}
@article{shi2018docface,
  title = {DocFace: Matching ID Document Photos to Selfies},
  author = {Shi, Yichun and Jain, Anil K.},
  booktitle = {arXiv:1805.02283},
  year = {2018}
}

Requirements

  1. Requirements for Python3
  2. Requirements for Tensorflow r1.2 or newer versions.
  3. Run pip install -r requirements.txt for other dependencies.
  4. Requirements for Matlab 2014b and Caffe for MTCNN face alignment.

Usage

Part 1: Preprocessing

1.1 Dataset Structure

Download the Ms-Celeb-1M and LFW dataset for training and testing the base model. Other dataset such as CASIA-Webface can also be used for training. Because Ms-Celeb-1M is known to be a very noisy dataset, we use the clean list provided by Wu et al. Arrange Ms-Celeb-1M dataset and LFW dataset as the following structure, where each subfolder represents a subject:

Aaron_Eckhart
    Aaron_Eckhart_0001.jpg
Aaron_Guiel
    Aaron_Guiel_0001.jpg
Aaron_Patterson
    Aaron_Patterson_0001.jpg
Aaron_Peirsol
    Aaron_Peirsol_0001.jpg
    Aaron_Peirsol_0002.jpg
    Aaron_Peirsol_0003.jpg
    Aaron_Peirsol_0004.jpg
...

For the ID-Selfie dataset, make sure all the foldesr in such a structure, where ID images and selfies start with "A" and "B", respectively :

Subject1
    A001.jpg
    B001.jpg
    B002.jpg
Subject2
    A001.jpg
    B001.jpg
...

1.2 Face Alignment

To ensure the peroformance, we align all the face images using the original MATLAB version of MTCNN following SphereFace. Here, we provide a simpler code to align a given dataset folder. To use the code, you need to install Caffe for Matlab, and clone the repo of MTCNN and Pdolloar. Then fill in their paths in the following lines in align/face_detect_align.m:

imglist = importdata('/path/to/input/imagelist.txt');
output_dir = '/path/to/output/dataset';
...
matCaffe       = '/path/to/caffe/matlab/';
pdollarToolbox = '/path/to/toolbox';
MTCNN          = '/path/to/mtcnn/code/codes/MTCNNv1';
...
modelPath = '/path/to/mtcnn/code/codes/MTCNNv1/model';

Run the following command in Matlab for face alignement:

run align/face_detect_align.m

Part 2: Training

Note: In this part, we assume you are in the directory $DOCFACE_ROOT/

2.1 Training the base model

  1. Set up the dataset paths in config/basemodel.py:

    # Training dataset path
    train_dataset_path = '/path/to/msceleb1m/dataset/folder'
    
    # Testing dataset path
    test_dataset_path = '/path/to/lfw/dataset/folder'
  2. Due to the memory cost, the user may need more than one GPUs to use a batch size of 256 on Ms-Celeb-1M. In particular, we used four GTX 1080 Ti GPUs. In such cases, change the following entry in config/basemodel.py:

    # Number of GPUs
    num_gpus = 1
  3. Run the following command in the terminal:

    python src/train_base.py config/basemodel.py

    After training, a model folder will appear underlog/faceres_ms/. We will use it for fine-tuning. If the training code is run more than once, multiple folders will appear with time stamps as their names. The user can also skip this part and use the pre-trained base model we provide.

2.2 Fine-tuning on the ID-Selfie datasets

  1. Set up the dataset paths and the pre-trained model path in config/finetune.py

    # Training dataset path
    train_dataset_path = '/path/to/training/dataset/folder'
    
    # Testing dataset path
    test_dataset_path = '/path/to/testing/dataset/folder'
    
    ...
    
    # The model folder from which to retore the parameters
    restore_model = '/path/to/the/pretrained/model/folder'
  2. Tune the parameters of loss functions according to your dataset in config/finetune.py, e.g.

    # Loss functions and their parameters.
    losses = {
        'diam': {'scale': 'auto', 'm': 5.0, 'alpha':1.0}
    }

    In our experiments, we found that there is no necessity to manually choose "scale". But in some cases one may find it helpful to change the "scale" to a fixed value. A smaller "alpha" should be favored when the average number of samples per class is larger.

  3. Run the following command in the terminal to start fine-tuning:

    python src/train_sibling.py config/finetune.py

Part 3: Feature Extraction

Note: In this part, we assume you are in the directory $DOCFACE_ROOT/

To extract features using a pre-trained model (either base network or sibling network), prepare a .txt file of image list. The images should be aligned in the same way as the training dataset. Then run the following command in terminal:

python src/extract_features.py \
--model_dir /path/to/pretrained/model/dir \
--image_list /path/to/imagelist.txt \
--output /path/to/output.npy

Notice that the images in the image list follow the same naming convention of the training dataset. That is, ID images should start with "A**" and selfie images should start with "B**".

Models

  • BaseModel (unconstraind face matching): Google Drive | Baidu Yun

  • Fine-tuned DocFace model: (contact the author)

Results

  • Using our pre-trained base model, one should be able to achieve 99.67% on the standard LFW verification protocol and 99.60% on the BLUFR protocol. Similar results should be achieved by using our code to train the Face-ResNet on Ms-Celeb-1M.

  • Using the proposed Max-margin Pairwise Score loss and sibling network, DocFace acheives a significant improvement compared with Base Model on our private ID-Selfie dataset after transfer learning:




  • Results of DIAM-Softmax and DocFace+ on a combination of ID-Selfie-A, ID-Selifie-B and another larger dataset, most of whose classes have only two images (a pair of ID and selfie):




Contact

Yichun Shi: shiyichu at msu dot edu

docface's People

Contributors

nicolas-raoul avatar seasonsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docface's Issues

base model training without se

Hi, I am currently training the base model without se-module. The reg_loss decreased from 1.031 to 0.065 while the am loss seems stay at 16.287. Isn't training without Se-module just by commenting out the following part:
if i % 2 == 1:
net = se_module(net)
net = net + shortcut
shortcut = net
print('| shortcut')
at the face_resnet.py ? Would you please help me? Thank you.

how can I get the Public IvS dataset?

hello, the paper mentioned the Public IvS dataset and some other paper used the dataset for training; so I want to ask where can I get the Public IvS dataset?

How to test model??

How to test the model, cause i can't find any test script. Sorry still quite a noob in machine learning.

queue().empy!

Hi: @seasonSH Yichun
thank you for your work .i trained the base model,how can i do?

essing/queues.py", line 105, in get
    raise Empty
queue.Empty
Process Process-3:
Traceback (most recent call last):
  File "/home/a302/laojiu/anaconda3/envs/tensorflow1.2.0/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap
    self.run()
  File "/home/a302/laojiu/anaconda3/envs/tensorflow1.2.0/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/home/a302/laojiu/github/DocFace/src/utils.py", line 245, in batch_queue_worker
    batch = self.get_batch(config.batch_size, config.batch_format)
  File "/home/a302/laojiu/github/DocFace/src/utils.py", line 202, in get_batch
    indices_batch.extend(self.index_queue.get(block=True, timeout=30))
  File "/home/a302/laojiu/anaconda3/envs/tensorflow1.2.0/lib/python3.6/multiprocessing/queues.py", line 105, in get
    raise Empty
queue.Empty
Traceback (most recent call last):
  File "src/train_base.py", line 111, in <module>
    main(args)
  File "src/train_base.py", line 80, in main
    batch = trainset.pop_batch_queue()
  File "/home/a302/laojiu/github/DocFace/src/utils.py", line 260, in pop_batch_queue
    batch = self.batch_queue.get(block=True, timeout=60)
  File "/home/a302/laojiu/anaconda3/envs/tensorflow1.2.0/lib/python3.6/multiprocessing/queues.py", line 105, in get
    raise Empty
queue.Empty
(tensorflow1.2.0) a302@a302-DGX-Station:~/laojiu/github/DocFace$ 
``` @

pair loss

seems the mps loss described in the paper is not the same with the code, which one is the right one?

How to do the model parallelism ?

Great Work !
I found that the code works well on multiple gpu training but only for the data parallelism.
However, it is hard to train a model with the classes more than 10 million or even more. In that case,
the model parallelism should solve the problem. I was doing the implementations but the diam softmax may seriously interfere the model parallelism. Could you give the solutions or any idea ?

java error

i transfer it to pb,when i run it in java it encouter problems. i try the model facenet with pb file and success.so i am confused that weather docface model can run in java?
Exception in thread "main" java.lang.RuntimeException: Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero
[[{{node None/FaceResNet/NetA_1/conv2/SE/Flatten/flatten/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](None/FaceResNet/NetA_1/conv2/SE/AvgPool2D/AvgPool, None/FaceResNet/NetA_1/conv2/SE/Flatten/flatten/Reshape/shape)]]
at com.umetrip.cv.service.TFTest.performInference(TFTest.java:64)
at com.umetrip.cv.service.TFTest.main(TFTest.java:26)
Caused by: java.lang.IllegalArgumentException: Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero
[[{{node None/FaceResNet/NetA_1/conv2/SE/Flatten/flatten/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](None/FaceResNet/NetA_1/conv2/SE/AvgPool2D/AvgPool, None/FaceResNet/NetA_1/conv2/SE/Flatten/flatten/Reshape/shape)]]
at org.tensorflow.Session.run(Native Method)
at org.tensorflow.Session.access$100(Session.java:48)
at org.tensorflow.Session$Runner.runHelper(Session.java:314)
at org.tensorflow.Session$Runner.run(Session.java:264)
at com.umetrip.cv.service.TFTest.performInference(TFTest.java:58)
... 1 more

public IvS data?

Thank you for your codes! Can you please share public IvS dataset? I couldn't find it.

about MPS loss

hi, seasonSH
thanks for your work.
I use the MPS loss, when I train the model, the loss is allways about 0.5, is it right ? what is the final loss when using the MPS loss ?

the weight imprinting implement question

thanks for the excellent work !
I try to implement your work in pytorch. When I implement the weight imprinting, i set the learning rate of the classifier layer to 0. then i use the follow steps:
1、calculate the features and normalize them as the weight vecotors.
2、replace the old weight vector
3、calculate the loss use am softmax
4、backward gradients and update weights except the classifier layer.

is it correct ?
i am not familiar with the tensorflow , so i don't understand your code very well.
please give me some advices, thanks !

how to debug your code?

Hi , thanks for sharing your work. I want to train my traindataset but the error appeared all the time ,then I debug the code and the following text always extists . Can you tell me how to debug?
"usage: train_base.py [-h] config_file
train_base.py: error: the following arguments are required: config_file"

How to Run DocFace fine tuning on CPU

Python: 3.6.9
Tensorflow: 1.8

I am trying to run the
python src/train_sibling.py config/finetune.py

with setting num_gpus = 0

But I see that the code is written for running on GPU's only.

Also getting the following error when trying to run.

Traceback (most recent call last):
  File "src/train_sibling.py", line 124, in <module>
    main(args)
  File "src/train_sibling.py", line 45, in main
    network.initialize(config, trainset.num_classes)
  File "/home/zero/PycharmProjects/DocFace/src/sibling_net.py", line 60, in initialize
    image_splits = tf.split(image_batch_placeholder, config.num_gpus)
  File "/home/zero/PycharmProjects/DocFace/venv/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1391, in split
    axis=axis, num_split=num_or_size_splits, value=value, name=name)
  File "/home/zero/PycharmProjects/DocFace/venv/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 7709, in split
    "Split", split_dim=axis, value=value, num_split=num_split, name=name)
  File "/home/zero/PycharmProjects/DocFace/venv/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 708, in _apply_op_helper
    (key, op_type_name, attr_value.i, attr_def.minimum))
ValueError: Attr 'num_split' of 'Split' Op passed 0 less than minimum 1.

Is there any way for me to run it on CPU? @nicolas-raoul @seasonSH

How to perform the final verification/classification?

Hi,

The guideline has provided the way to extract features for test images. But how to verify if two images belong to the same person or not? To calculate the distance between the extracted features? Can the pre-trained/fine-tuned network be used to output the probability of that?

Many thanks!

FaceResNet arch is different from the referenced paper?

Looking at the code, why is there an SE module and why was it not referenced in the DocFace paper?, From my limited understanding of tf-slim, does the following

            with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=phase_train):
                print('input shape:', [dim.value for dim in images.shape])
                
                net = conv_module(images, 0, [32, 64], scope='conv1')
                print('module_1 shape:', [dim.value for dim in net.shape])

                net = conv_module(net, 2, [64, 128], scope='conv2')
                print('module_2 shape:', [dim.value for dim in net.shape])

                net = conv_module(net, 4, [128, 256], scope='conv3')
                print('module_3 shape:', [dim.value for dim in net.shape])

                net = conv_module(net, 10, [256, 512], scope='conv4')
                print('module_4 shape:', [dim.value for dim in net.shape])

                net = conv_module(net, 6, [512], scope='conv5')
                print('module_5 shape:', [dim.value for dim in net.shape])

actually use batch_norm and dropout in the model at all? How does the model know when and where to create the mentioned layers?

How to create lfw_pairs.txt

10 300
Abel_Pacheco 1 4
Akhmed_Zakayev 1 3
Akhmed_Zakayev 2 3
Amber_Tamblyn 1 2
Can you explain how it was generated?

diam implement question

hi, seasonSH:
thanks for your work.
in your implementaion, the config of diam is:

Loss functions and their parameters.

losses = {
'diam': {'scale': 'auto', 'm': 5.0, 'alpha':1.0}
}
but the in the am-softmax paper, the m is about 0.35, so, my question is why in your implemention the m is set to 5, it's much larger than the origin am-softmax paper setting ?
Looking forward for your replay !

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.