arcface_paddle

环境

GPU(物理)

  • NVIDIA 3090*2

  • 显卡驱动 515.43.04

  • CUDA版本 11.7

  • CUDAtoolkit (cuda_11.7.0_515.43.04_linux)

  • cuDNN (v8.4.1)

  • paddlepaddle 多卡训练需要NCLL支持 (ncll v2.12.12 cuda11.7)

paddlepaddle版本

  • paddlepaddle-gpu==2.2.0rc0(虚拟环境cuda11.2)

python环境

  • CentOS7.9

  • anaconda3

  • python3.8

anaconda安装insightface

重要:pillow版本建议选择9.5 否则过高会导致安装insightface报错 (错误原因:pillow10移除了getsize方法,需要修改对应位置源码为getbboxgetlength

告警信息:

1
2
tools/test_recognition.py:627: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
tw = font.getsize(text)[0]

环境安装

1
2
3
4
5
6
7
8
# .
conda install paddlepaddle-gpu==2.2.0rc0 cudatoolkit=11.2 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/ -c conda-forge
# insightface
pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
# insightface/recongition/arcface_paddle/
pip install -r requirement.txt -i https://mirror.baidu.com/pypi/simple
# insightface-paddle
pip install insightface-paddle -i https://mirror.baidu.com/pypi/simple

获取数据集

https://github.com/deepinsight/insightface/tree/master/recognition/_datasets_

  • MS1M_v2: MS1M-ArcFace
  • MS1M_v3: MS1M-RetinaFace

从 MXNet 格式数据集抽取图像

1
python tools/mx_recordio_2_images.py --root_dir ms1m-retinaface-t1/ --output_dir MS1M_v3/

数据抽取完成后,格式如下

1
2
3
4
5
6
7
8
9
10
MS1M_v3
|_ images
| |_ 00000001.jpg
| |_ ...
| |_ 05179510.jpg
|_ label.txt
|_ agedb_30.bin
|_ cfp_ff.bin
|_ cfp_fp.bin
|_ lfw.bin

标签数据格式如下

1
2
3
4
# 图像路径与标签的分隔符: "\t"
# 以下是 label.txt 每行的格式
images/00000001.jpg 0
...

模型训练

使用双卡

1
export CUDA_VISIBLE_DEVICES=0,1

训练脚本scripts/train_static.sh

1
2
# 降级scipy,scipy版本过高会报错
pip install scipy==1.7.1 -i https://mirror.baidu.com/pypi/simple

训练静态模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
python -m paddle.distributed.launch --gpus=1 tools/train.py \
--config_file configs/ms1mv3_r50.py \
--is_static True \
--backbone FresResNet50 \
--classifier LargeScaleClassifier \
--embedding_size 512 \
--model_parallel True \
--dropout 0.0 \
--sample_ratio 0.1 \
--loss ArcFace \
--batch_size 64 \
--dataset MS1M_v3 \
--num_classes 93431 \
--data_dir MS1M_v3/ \
--label_file MS1M_v3/label.txt \
--is_bin False \
--log_interval_step 100 \
--validation_interval_step 2000 \
--fp16 True \
--use_dynamic_loss_scaling True \
--init_loss_scaling 27648.0 \
--num_workers 8 \
--train_unit 'epoch' \
--warmup_num 0 \
--train_num 25 \
--decay_boundaries "10,16,22" \
--output MS1M_v3_arcface_static_0.1

3090单卡容易爆显存 batch_size可由128调整至64,或开启多卡训练,需ncll

模型评价sh scripts/validation_static.sh

1
2
3
4
5
6
7
8
python tools/validation.py \
--is_static True \
--backbone FresResNet50 \
--embedding_size 512 \
--checkpoint_dir MS1M_v3_arcface_static_0.1/FresResNet50/24 \
--data_dir MS1M_v3/ \
--val_targets lfw,cfp_fp,agedb_30 \
--batch_size 64

模型导出sh scripts/export_static.sh

1
2
3
4
5
6
7
python tools/export.py \
--is_static True \
--export_type paddle \
--backbone FresResNet50 \
--embedding_size 512 \
--checkpoint_dir MS1M_v3_arcface_static_0.1/FresResNet50/24 \
--output_dir MS1M_v3_arcface_static_0.1/FresResNet50/exported_model

模型推理sh scripts/inference.sh

1
2
3
4
5
python tools/inference.py \
--export_type paddle \
--model_file MS1M_v3_arcface_static_0.1/FresResNet50/exported_model/FresResNet50.pdmodel \
--params_file MS1M_v3_arcface_static_0.1/FresResNet50/exported_model/FresResNet50.pdiparams \
--image_path MS1M_v3/images/00000001.jpg

构建人像索引

  • 建立图像文件夹
1
2
3
4
5
6
7
8
9
10
11
12
fridends
|_ gallery
| |_ Chandler
| |_ Chandler01.jpg
| |_ ...
| |_ Chandler50.jpg
| |_ ...
| |_ Ross
| |_ Ross01.jpg
| |_ ...
| |_ Ross50.jpg
|_ label.txt
  • 建立索引文件label.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
./Chandler/Chandler00037.jpg	Chandler
./Chandler/Chandler00021.png Chandler
./Chandler/Chandler00040.jpg Chandler
./Chandler/Chandler00041.jpg Chandler
./Chandler/Chandler00004.png Chandler
./Chandler/Chandler00034.png Chandler
./Chandler/Chandler00008.png Chandler
...
./Ross/Ross00016.jpg Ross
./Ross/Ross00022.jpg Ross
./Ross/Ross00019.jpg Ross
./Ross/Ross00024.jpg Ross
./Ross/Ross00001.jpg Ross
./Ross/Ross00039.jpg Ross
./Ross/Ross00038.jpg Ross
./Ross/Ross00017.jpg Ross
./Ross/Ross00034.jpg Ross
./Ross/Ross00002.png Ross
  • 构建索引
1
insightfacepaddle --build_index ./demo/friends/index.bin --img_dir ./demo/friends/gallery --label ./demo/friends/gallery/label.txt

检测图片

1
python tools/test_recognition.py --det --rec --index=./demo/friends/index.bin --input=./test/测试2.jpg --output=./output

预测图片

1
python tools/test_recognition.py --det --rec --index=./demo/friends/index.bin --input=./test/测试2.jpg --output=./output

预测视频

1
python tools/test_recognition.py --det --rec --index=./demo/friends/index.bin --input=./test/mp4v.mp4 --output=./output

python脚本

脚本封装位置:arcface_paddle/python

  • 构建索引
1
2
3
4
5
6
7
8
9
10
11
import os
import insightface_paddle as face
import logging

parser = face.parser()
args = parser.parse_args()
args.build_index = "./demo/friends/index.bin"
args.img_dir = "./demo/friends/gallery"
args.label = "./demo/friends/gallery/label.txt"
predictor = face.InsightFace(args)
predictor.build_index()
  • 视频
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import insightface_paddle as face
import logging

PR = os.path.dirname(os.path.abspath(__file__))

logging.basicConfig(level=logging.INFO)

parser = face.parser()
args = parser.parse_args()

args.det = True
args.rec = True
args.index = os.path.join(PR, "demo/friends/index.bin")
args.output = os.path.join(PR, "output")
input_path = os.path.join(PR, "test/MP4V.mp4")

predictor = face.InsightFace(args)
res = predictor.predict(input_path, print_info=True)
for _ in res:
print(_.get('labels'))

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2026 青域 All Rights Reserved.

UV : | PV :