- Impoving triplet sample strategy: Assume identities list as
class_indices
- We iterate all person identities
class_indices
in an epoch. Then for each identity in a batch, we randomly sample its corespondingK
images. - At the beginning of the epoch, we shuffle
class_indices
.
- We iterate all person identities
- Triplet loss can be optimized togother with softmax loss: run
bash train_triplet_with_softmax.sh
- This implementation supports multi-gpus.
- The Data Sample is implemented by
tf.data
, which can load images fast in format ofjpg, png
. - The Triplet Selection is implemented by
tf.py_func
and it's flexible to modify as you need. - It's fast to train and the average time of one iteration with multi-gpus (210*4 images) is just about
0.6s
inV100
. - It achieves 98.3% accurace on LFW with softmax pretrain(97.1%).
This project provide an efficient implementation for deep face recognition using Triplet Loss. When trained on CASIA-Webface and tested on on LFW,this code can achieve an 98.3% accuracy with softmax pretrain and 98.6% with CosFace pretrain. The framework using triplet loss can be seen as the following figure. It contrains Data Sample, Feature Extractor, Triplet Selection and Triplet Loss modules. The details can be seen as our technical report: MassFace: an effecient implementation using triplet loss for face recognition
Data Sample: we sample total P*K images for P persons with K images each in an iteration. We implement it by tensorflow tf.data
api.
Feature Extractor: We use MobileFacenets [1] to extract the feature of the input image as a deep representation. It only has about 6.0MB parameters and can be infered very fast as well.
Triplet Selection: Triplet selection aims to choice the valid triplet (i, j, k) which is used as input of triplet loss. The valid triplet menas that i, j have the identity and i, k have different identity. We implement serveral mining strategy to select triplet pairs.
- Batch All
- Batch Random
- Batch Min Min
- Batch Min Max
- Batch Hardest
Mining methods:The triplet selection is based on a pool of the feature vectors. There are serveral methods to obtain the pool of feature vectors with size B.
- Online
- Offline
- Semi-online
- Install tensorflow 1.8:
pip install tensorflow==1.8.0
- Get code of this respository:
git clone https://github.com/yule-li/MassFace.git
- Align webface and lfw dataset to
112x112
(casia-112x112,lfw-112x112) using insightface align method - If you want to train this code on your dataset, you just need to keep your dataset having the same directory like webface in which all images of the same identity located in one folder.
- Pretrain with softmax loss: run
./train_softmax.sh
with argumentNETWORK=mobilenet
- Train with triplet loss: modify the
PRETRAINED_MODEL
to your pretrained model with softmax loss and run./train_triplet.sh
- Modify
MODEL_DIR
intest.sh
to your trained model ./test.sh