README.md 6.09 KB
Newer Older
chenych's avatar
chenych committed
1
2
# CenterFace
## 论文
Rayyyyy's avatar
Rayyyyy committed
3
4
`CenterFace: Joint Face Detection and Alignment Using Face as Point`
- https://arxiv.org/abs/1911.03599
chenych's avatar
chenych committed
5
6
7

## 模型结构
CenterFace是一种人脸检测算法,采用了轻量级网络mobileNetV2作为主干网络,结合特征金字塔网络(FPN)实现anchor free的人脸检测。
chenych's avatar
chenych committed
8
<div align=center>
chenych's avatar
chenych committed
9
    <img src="./doc/Architecture of the CenterFace.png"/>
chenych's avatar
chenych committed
10
</div>
chenych's avatar
chenych committed
11

chenych's avatar
chenych committed
12
13
14
## 算法原理
CenterFace模型是一种基于单阶段人脸检测算法,作者借鉴了CenterNet的思想,将人脸检测转换为标准点问题,根据人脸中心点来回归人脸框的大小和五个标志点。

chenych's avatar
V1.0  
chenych committed
15
16
| 参数 | 说明 |
| :------: | :------: |
chenych's avatar
chenych committed
17
| F | Feature Map |
chenych's avatar
V1.0  
chenych committed
18
19
20
| HM | 人脸分类的HeatMap |
| Offset | 人脸框中心点偏移 |
| WH | 人脸框宽,高 |
chenych's avatar
chenych committed
21
22
| x_l,y_l | 人脸框左上角点的x,y坐标 |
| x_r,y_r | 人脸框右下角点的x,y坐标 |
chenych's avatar
V1.0  
chenych committed
23
| c | Confidence |
chenych's avatar
chenych committed
24
25

<div align=center>
chenych's avatar
chenych committed
26
    <img src="./doc/process.png"/>
chenych's avatar
chenych committed
27
</div>
chenych's avatar
chenych committed
28

chenych's avatar
chenych committed
29
30
31
## 环境配置
### Docker(方法一)

chenych's avatar
chenych committed
32
33
-v 路径、docker_name和imageID根据实际情况修改

Rayyyyy's avatar
Rayyyyy committed
34
```bash
dcuai's avatar
dcuai committed
35
36
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G -v /opt/hyhal:/opt/hyhal:ro --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
37

chenych's avatar
chenych committed
38
cd /your_code_path/centerface_pytorch/
chenych's avatar
chenych committed
39
40
41
42
43
pip3 install -r requirements.txt
```

### Dockerfile(方法二)

chenych's avatar
chenych committed
44
45
-v 路径、docker_name和imageID根据实际情况修改

Rayyyyy's avatar
Rayyyyy committed
46
```bash
chenych's avatar
chenych committed
47
48
cd ./docker
docker build --no-cache -t centerface:latest .
dcuai's avatar
dcuai committed
49
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G -v /opt/hyhal:/opt/hyhal:ro --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
50

Rayyyyy's avatar
Rayyyyy committed
51
52
cd /your_code_path/centerface_pytorch/
pip3 install -r requirements.txt
chenych's avatar
chenych committed
53
54
55
56
```

### Anaconda(方法三)

chenzk's avatar
chenzk committed
57
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.sourcefind.cn/tool/
chenych's avatar
chenych committed
58
59

```
dcuai's avatar
dcuai committed
60
61
DTK软件栈:dtk24.04.1
python:python3.10
Rayyyyy's avatar
Rayyyyy committed
62
torch:2.1.0
Rayyyyy's avatar
Rayyyyy committed
63
torchvision:0.16.0
chenych's avatar
chenych committed
64
65
66
67
68
69
70
71
72
73
```
`Tips:以上dtk驱动、python、paddle等DCU相关工具版本需要严格一一对应`

2、其他非特殊库直接按照requirements.txt安装

```
pip3 install -r requirements.txt
```

## 数据集
chenzk's avatar
chenzk committed
74
[WIDER_FACE](http://shuoyang1213.me/WIDERFACE/index.html )
chenych's avatar
chenych committed
75
76
77
78
79


数据集全部解压后的目录结构如下:

```
chenych's avatar
chenych committed
80
81
82
83
84
85
86
├── wider_face:  存放数据集根目录
│   ├── WIDER_train: 训练集解压后的文件目录
│       └── images:
│           ├──  0--Parade:         对应该类别的所有图片
│           ├──  ........
│           └──  61--Street_Battle: 对应该类别的所有图片
│   ├── WIDER_val: 验证集解压后的文件目录
Rayyyyy's avatar
Rayyyyy committed
87
│       └── images:
chenych's avatar
chenych committed
88
89
90
91
│           ├──  0--Parade:         对应该类别的所有图片
│           ├──  ........
│           └──  61--Street_Battle: 对应该类别的所有图片
│   ├── WIDER_test: 训练集解压后的文件目录
Rayyyyy's avatar
Rayyyyy committed
92
│       └── images:
chenych's avatar
chenych committed
93
94
95
96
97
98
│           ├──  0--Parade:         对应该类别的所有图片
│           ├──  ........
│           └──  61--Street_Battle: 对应该类别的所有图片
```

解压完成后执行以下步骤:
Rayyyyy's avatar
Rayyyyy committed
99
2. 将训练图片放置于 ./datasets/images/train的目录下,验证数据放置于./datasets/images/val目录下,存放目录结如下
chenych's avatar
chenych committed
100
101
102
103
104
105
106
107
108
109
110
111
```
├── images
│   ├── train
│       ├── 0--Parade
│       ├──  ........
│       └──  61--Street_Battle
│   ├── val
│       ├── 0--Parade
│       ├──  ........
│       └──  61--Street_Battle
```

Rayyyyy's avatar
Rayyyyy committed
112
113
3. 如果是使用`WIDER_train``WIDER_val`数据, 可直接将`./datasets/labels/`下的`train_wider_face.json`重命名为`train_face.json`, `val_wider_face.json`重命名为`val_face.json`即可,无需进行标注文件格式转换;
反之,需要将训练图片/验证图片对应的人脸标注信息文件`train.txt or val.txt`,放置于`./datasets/annotations/`下(train存放训练图片的标注文件,val存放验证图片的标注文件),存放目录结构如下:
chenych's avatar
chenych committed
114
115

```
chenych's avatar
chenych committed
116
├── annotations
chenych's avatar
chenych committed
117
118
119
120
121
│   ├── train
│       ├── train.txt
│   ├── val
│       ├── val.txt
```
chenych's avatar
chenych committed
122

chenych's avatar
chenych committed
123
124
125
126
127
128
129
130
特别地,标注信息的格式为:

```
# img_file/image_name # #+空格+img_file/image_name
x, y, w, h, left_eye_x, left_eye_y, flag, right_eye_x, right_eye_y, flag, nose_x, nose_y, flag, left_mouth_x, left_mouth_y, flag, right_mouth_x, right_mouth_y, flag, confidence  # x和y是检测框左上角的坐标
```

举个例子:
Rayyyyy's avatar
Rayyyyy committed
131
`./datasets/annotations/train/train.txt``wider_face`训练数据集的标注信息
chenych's avatar
chenych committed
132

chenych's avatar
chenych committed
133
134
135
136
```
# 0--Parade/0_Parade_marchingband_1_849.jpg
449 330 122 149 488.906 373.643 0.0 542.089 376.442 0.0 515.031 412.83 0.0 485.174 425.893 0.0 538.357 431.491 0.0 0.82
...
chenych's avatar
chenych committed
137
138
```

Rayyyyy's avatar
Rayyyyy committed
139
4. 生成训练所需的json格式标注数据:
Rayyyyy's avatar
Rayyyyy committed
140
```bash
chenych's avatar
chenych committed
141
142
143
144
145
146
147
cd ./datasets
python gen_data.py
```

执行完成后会在./datasets/labels下生成训练数据的标注文件 train_face.json、val_face.json


chenych's avatar
chenych committed
148
## 训练
Rayyyyy's avatar
Rayyyyy committed
149
默认训练模型保存在`./exp/`下,如需修改为自己的路径,可以对`centerface_pytorch/src/lib/opts_pose.py`的284行进行修改。
chenych's avatar
chenych committed
150

chenych's avatar
chenych committed
151
### 单机单卡
Rayyyyy's avatar
Rayyyyy committed
152
```bash
chenych's avatar
chenych committed
153
154
155
156
157
cd ./src
bash train.sh
```

### 单机多卡
Rayyyyy's avatar
Rayyyyy committed
158
```bash
chenych's avatar
chenych committed
159
160
161
162
163
164
cd ./src
bash train_multi.sh
```

## 推理
#### 单卡推理
Rayyyyy's avatar
Rayyyyy committed
165
```bash
chenych's avatar
chenych committed
166
167
168
cd lib/external/
bash make.sh
cd ../../
chenych's avatar
chenych committed
169
170
171
172
python test_wider_face.py
```

## result
chenych's avatar
chenych committed
173
<div align=center>
chenych's avatar
chenych committed
174
    <img src="./doc/draw_img.jpg"/>
chenych's avatar
chenych committed
175
</div>
chenych's avatar
chenych committed
176
177

### 精度
chenych's avatar
chenych committed
178

Rayyyyy's avatar
Rayyyyy committed
179
WIDER_FACE验证集上的测试结果如下:
chenych's avatar
chenych committed
180

Rayyyyy's avatar
Rayyyyy committed
181
| Device | Easy(AP) | Medium(AP) | Hard(AP)|
chenych's avatar
V1.0  
chenych committed
182
| :------: | :------: | :------: | :------: |
Rayyyyy's avatar
Rayyyyy committed
183
| Z100L | 0.9264 | 0.9133 | 0.7479 |
Rayyyyy's avatar
Rayyyyy committed
184
| V100S | 0.922 | 0.911 | 0.782|
chenych's avatar
chenych committed
185
186
187
188
189
190
191
192
193

## 应用场景
### 算法类别
人脸识别

### 热点应用行业
教育,交通,公安,医疗

## 源码仓库及问题反馈
chenzk's avatar
chenzk committed
194
- https://developer.sourcefind.cn/codes/modelzoo/centerface_pytorch
chenych's avatar
chenych committed
195
196

## 参考资料
Rayyyyy's avatar
Rayyyyy committed
197
- https://github.com/chenjun2hao/CenterFace.pytorch