Skip to content

Commit

Permalink
add face task (PaddlePaddle#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoxiaWang authored Oct 26, 2022
1 parent a065daa commit c8785c3
Show file tree
Hide file tree
Showing 7 changed files with 931 additions and 0 deletions.
134 changes: 134 additions & 0 deletions task/recognition/face/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Face Recognition

The face recognition task is a case of achieving large-scale classification on PLSC,
and the goal is to implement and reproduce the SOTA algorithm. It has
the ability to train tens of millions of identities with high throughput in a single server.

Function has supported:
* ArcFace
* CosFace
* PartialFC
* SparseMomentum
* FP16 training
* DataParallel(backbone layer) + ModelParallel(FC layer) distributed training

Backbone includes:
* IResNet
* FaceViT

## Requirements
To enjoy some new features, PaddlePaddle 2.4 is required. For more installation tutorials
refer to [installation.md](../../../tutorials/get_started/installation.md)

## Data Preparation

### Download Dataset

Download the dataset from insightface datasets.

- [MS1MV2](https://github.com/deepinsight/insightface/tree/master/recognition/_datasets_#ms1m-arcface-85k-ids58m-images-57) (87k IDs, 5.8M images)
- [MS1MV3](https://github.com/deepinsight/insightface/tree/master/recognition/_datasets_#ms1m-retinaface) (93k IDs, 5.2M images)
- [Glint360K](https://github.com/deepinsight/insightface/tree/master/recognition/partial_fc#4-download) (360k IDs, 17.1M images)
- [WebFace42M](https://github.com/deepinsight/insightface/blob/master/recognition/arcface_torch/docs/prepare_webface42m.md) (2M IDs, 42.5M images)

Note:
* MS1MV2: MS1M-ArcFace
* MS1MV3: MS1M-RetinaFace
* WebFace42M: cleared WebFace260M

### [Optional] Extract MXNet Dataset to Images
```shell
# for example, here extract MS1MV3 dataset
python -m plsc.data.dataset.tools.mx_recordio_2_images --root_dir /path/to/ms1m-retinaface-t1/ --output_dir ./dataset/MS1M_v3/
```

### Extract LFW Style bin Dataset to Images
```shell
# for example, here extract agedb_30 bin to images
python -m plsc.data.dataset.tools.lfw_style_bin_dataset_converter --bin_path ./dataset/MS1M_v3/agedb_30.bin --out_dir ./dataset/MS1M_v3/agedb_30 --flip_test
```

### Dataset Directory
We put all the data in the `./dataset/` directory, and we also recommend using soft links, for example:
```shell
mkdir -p ./dataset/
ln -s /path/to/MS1M_v3 ./dataset/MS1M_v3
```

## How to Train

```bash
# Note: If running on multiple nodes,
# set the following environment variables
# and then need to run the script on each node.
export PADDLE_NNODES=1
export PADDLE_MASTER="127.0.0.1:12538"
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

python -m paddle.distributed.launch \
--nnodes=$PADDLE_NNODES \
--master=$PADDLE_MASTER \
--devices=$CUDA_VISIBLE_DEVICES \
plsc-train \
-c ./configs/IResNet50_MS1MV3_ArcFace_pfc01_1n8c_dp_mp_fp16o1.yaml
```

## How to Export

```bash
# In general, we only need to export the
# backbone, so we only need to run the
# export command on a single device.
export PADDLE_NNODES=1
export PADDLE_MASTER="127.0.0.1:12538"
export CUDA_VISIBLE_DEVICES=0
python -m paddle.distributed.launch \
--nnodes=$PADDLE_NNODES \
--master=$PADDLE_MASTER \
--devices=$CUDA_VISIBLE_DEVICES \
plsc-export \
-c ./configs/IResNet50_MS1MV3_ArcFace_pfc01_1n8c_dp_mp_fp16o1.yaml \
-o Global.pretrained_model=./output/IResNet50/latest \
-o FP16.level=O0 \ # export FP32 model when training with FP16
-o Model.data_format=NCHW # IResNet required if training with NHWC
```

## Evaluation IJB-C
```bash
python onnx_ijbc.py \
--model-root ./output/IResNet50.onnx \
--image-path ./ijb/IJBC/ \
--target IJBC
```

## Model Zoo

| Datasets | Backbone | Config | Devices | PFC | agedb30 | IJB-C(1E-4) | IJB-C(1E-5) | checkpoint | log |
| :------: | :------- | ------------------------------------------------------------ | --------- | ---- | ------- | ----------- | :---------- | :----------------------------------------------------------- | ------------------------------------------------------------ |
| MS1MV3 | Res50 | [config](./configs/IResNet50_MS1MV3_ArcFace_pfc10_1n8c_dp_mp_fp16o1.yaml) | N1C8*A100 | 1.0 | 0.9825 | 96.52 | 94.60 | [download](https://plsc.bj.bcebos.com/models/face/v2.4/IResNet50_MS1MV3_ArcFace_pfc10_1n8c_dp_mp_fp16o1.pdparams) | [download](https://plsc.bj.bcebos.com/models/face/v2.4/IResNet50_MS1MV3_ArcFace_pfc10_1n8c_dp_mp_fp16o1.log) |

## Citations

```
@misc{plsc,
title={PLSC: An Easy-to-use and High-Performance Large Scale Classification Tool},
author={PLSC Contributors},
howpublished = {\url{https://github.com/PaddlePaddle/PLSC}},
year={2022}
}
@inproceedings{deng2019arcface,
title={Arcface: Additive angular margin loss for deep face recognition},
author={Deng, Jiankang and Guo, Jia and Xue, Niannan and Zafeiriou, Stefanos},
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages={4690--4699},
year={2019}
}
@inproceedings{An_2022_CVPR,
author={An, Xiang and Deng, Jiankang and Guo, Jia and Feng, Ziyong and Zhu, XuHan and Yang, Jing and Liu, Tongliang},
title={Killing Two Birds With One Stone: Efficient and Robust Training of Face Recognition CNNs by Partial FC},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month={June},
year={2022},
pages={4042-4051}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# global configs
Global:
task_type: recognition
train_epoch_func: defualt_train_one_epoch
eval_func: face_verification_eval
checkpoint: null
pretrained_model: null
output_dir: ./output/
device: gpu
save_interval: 1
max_num_latest_checkpoint: 0
eval_during_train: True
eval_interval: 2000
eval_unit: "step"
accum_steps: 1
epochs: 20
print_batch_step: 100
use_visualdl: True
seed: 2022

# FP16 setting
FP16:
level: O1
GradScaler:
init_loss_scaling: 27648.0

DistributedStrategy:
data_parallel: True

# model architecture
Model:
name: IResNet50
num_features : 512
data_format : "NHWC"
class_num: 93431
pfc_config:
sample_ratio: 1.0
model_parallel: True

# loss function config for traing/eval process
Loss:
Train:
- MarginLoss:
m1: 1.0
m2: 0.5
m3: 0.0
s: 64.0
model_parallel: True
weight: 1.0

LRScheduler:
name: Poly
learning_rate: 0.1
decay_unit: step
warmup_steps: 0

Optimizer:
name: Momentum
momentum: 0.9
weight_decay: 5e-4
grad_clip:
name: ClipGradByGlobalNorm
clip_norm: 5.0
always_clip: True
no_clip_list: ['dist']

# data loader for train and eval
DataLoader:
Train:
dataset:
name: FaceIdentificationDataset
image_root: ./dataset/MS1M_v3/
cls_label_path: ./dataset/MS1M_v3/label.txt
transform_ops:
- DecodeImage:
to_rgb: True
channel_first: False
- RandFlipImage:
flip_code: 1
- NormalizeImage:
scale: 1.0/255.0
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
order: ''
- ToCHWImage:
sampler:
name: DistributedBatchSampler
batch_size: 128
drop_last: False
shuffle: True
loader:
num_workers: 8
use_shared_memory: True

Eval:
dataset:
name: FaceVerificationDataset
image_root: ./dataset/MS1M_v3/agedb_30
cls_label_path: ./dataset/MS1M_v3/agedb_30/label.txt
transform_ops:
- DecodeImage:
to_rgb: True
channel_first: False
- NormalizeImage:
scale: 1.0/255.0
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
order: ''
- ToCHWImage:
sampler:
name: BatchSampler
batch_size: 128
drop_last: False
shuffle: False
loader:
num_workers: 0
use_shared_memory: True

Metric:
Eval:
- LFWAcc:
flip_test: True

Export:
export_type: onnx
input_shape: [None, 3, 112, 112]
18 changes: 18 additions & 0 deletions task/recognition/face/eval_ijbc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

python onnx_ijbc.py \
--model-root ./output/IResNet50.onnx \
--image-path ./ijb/IJBC/ \
--target IJBC
26 changes: 26 additions & 0 deletions task/recognition/face/export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export PADDLE_NNODES=1
export PADDLE_MASTER="127.0.0.1:12538"
export CUDA_VISIBLE_DEVICES=0
python -m paddle.distributed.launch \
--nnodes=$PADDLE_NNODES \
--master=$PADDLE_MASTER \
--devices=$CUDA_VISIBLE_DEVICES \
plsc-export \
-c ./configs/IResNet50_MS1MV3_ArcFace_pfc10_1n8c_dp_mp_fp16o1.yaml \
-o Global.pretrained_model=output/IResNet50/latest \
-o FP16.level=O0 \
-o Model.data_format=NCHW
Loading

0 comments on commit c8785c3

Please sign in to comment.