PULC_person_exists.md 20.8 KB
Newer Older
1
# PULC 有人/无人分类模型
C
cuicheng01 已提交
2 3 4 5

------


C
cuicheng01 已提交
6
## 目录
C
cuicheng01 已提交
7

C
cuicheng01 已提交
8
- [1. 模型和应用场景介绍](#1)
9
- [2. 模型快速体验](#2)
C
cuicheng01 已提交
10 11 12
    - [2.1 安装 paddlepaddle](#2.1)
    - [2.2 安装 paddleclas](#2.2)
    - [2.3 预测](#2.3)
C
cuicheng01 已提交
13
- [3. 模型训练、评估和预测](#3)
C
cuicheng01 已提交
14 15 16 17 18 19 20
    - [3.1 环境配置](#3.1)
    - [3.2 数据准备](#3.2)
      - [3.2.1 数据集来源](#3.2.1)
      - [3.2.2 数据集获取](#3.2.2)
    - [3.3 模型训练](#3.3)
    - [3.4 模型评估](#3.4)
    - [3.5 模型预测](#3.5)
C
cuicheng01 已提交
21
- [4. 模型压缩](#4)
C
cuicheng01 已提交
22
  - [4.1 SKL-UGI 知识蒸馏](#4.1)
C
cuicheng01 已提交
23
    - [4.1.1 教师模型训练](#4.1.1)
C
cuicheng01 已提交
24
    - [4.1.2 蒸馏训练](#4.1.2)
C
cuicheng01 已提交
25 26 27
- [5. 超参搜索](#5)
- [6. 模型推理部署](#6)
  - [6.1 推理模型准备](#6.1)
C
cuicheng01 已提交
28
    - [6.1.1 基于训练得到的权重导出 inference 模型](#6.1.1)
C
cuicheng01 已提交
29 30 31 32 33 34 35
    - [6.1.2 直接下载 inference 模型](#6.1.2)
  - [6.2 基于 Python 预测引擎推理](#6.2)
    - [6.2.1 预测单张图像](#6.2.1)
    - [6.2.2 基于文件夹的批量预测](#6.2.2)
  - [6.3 基于 C++ 预测引擎推理](#6.3)
  - [6.4 服务化部署](#6.4)
  - [6.5 端侧部署](#6.5)
C
cuicheng01 已提交
36
  - [6.6 Paddle2ONNX 模型转换与预测](#6.6)
C
cuicheng01 已提交
37

C
cuicheng01 已提交
38

C
cuicheng01 已提交
39 40
<a name="1"></a>

C
cuicheng01 已提交
41
## 1. 模型和应用场景介绍
C
cuicheng01 已提交
42

C
cuicheng01 已提交
43
该案例提供了用户使用 PaddleClas 的超轻量图像分类方案(PULC,Practical Ultra Lightweight image Classification)快速构建轻量级、高精度、可落地的有人/无人的分类模型。该模型可以广泛应用于如监控场景、人员进出管控场景、海量数据过滤场景等。
C
cuicheng01 已提交
44

45
下表列出了判断图片中是否有人的二分类模型的相关指标,前两行展现了使用 SwinTranformer_tiny 和 MobileNetV3_small_x0_35 作为 backbone 训练得到的模型的相关指标,第三行至第六行依次展现了替换 backbone 为 PPLCNet_x1_0、使用 SSLD 预训练模型、使用 SSLD 预训练模型 + EDA 策略、使用 SSLD 预训练模型 + EDA 策略 + SKL-UGI 知识蒸馏策略训练得到的模型的相关指标。
46

C
cuicheng01 已提交
47

C
cuicheng01 已提交
48
| 模型 | Tpr(%) | 延时(ms) | 存储(M) | 策略 |
49
|-------|-----------|----------|---------------|---------------|
C
cuicheng01 已提交
50 51 52 53 54 55 56 57
| SwinTranformer_tiny  | 95.69 | 95.30  | 111 | 使用 ImageNet 预训练模型 |
| MobileNetV3_small_x0_35  | 68.25 | 2.85  | 2.6 | 使用 ImageNet 预训练模型 |
| PPLCNet_x1_0  | 89.57 | 2.12  | 7.0 | 使用 ImageNet 预训练模型 |
| PPLCNet_x1_0  | 92.10 | 2.12  | 7.0 | 使用 SSLD 预训练模型 |
| PPLCNet_x1_0  | 93.43 | 2.12  | 7.0 | 使用 SSLD 预训练模型+EDA 策略|
| <b>PPLCNet_x1_0<b>  | <b>96.23<b> | <b>2.12<b>  | <b>7.0<b> | 使用 SSLD 预训练模型+EDA 策略+SKL-UGI 知识蒸馏策略|

从表中可以看出,backbone 为 SwinTranformer_tiny 时精度较高,但是推理速度较慢。将 backbone 替换为轻量级模型 MobileNetV3_small_x0_35 后,速度可以大幅提升,但是会导致精度大幅下降。将 backbone 替换为速度更快的 PPLCNet_x1_0 时,精度较 MobileNetV3_small_x0_35 高 20 多个百分点,与此同时速度依旧可以快 20% 以上。在此基础上,使用 SSLD 预训练模型后,在不改变推理速度的前提下,精度可以提升约 2.6 个百分点,进一步地,当融合EDA策略后,精度可以再提升 1.3 个百分点,最后,在使用 SKL-UGI 知识蒸馏后,精度可以继续提升 2.8 个百分点。此时,PPLCNet_x1_0 达到了 SwinTranformer_tiny 模型的精度,但是速度快 40 多倍。关于 PULC 的训练方法和推理部署方法将在下面详细介绍。
littletomatodonkey's avatar
littletomatodonkey 已提交
58 59 60

**备注:**

61
* `Tpr`指标的介绍可以参考 [3.2 小节](#3.2)的备注部分,延时是基于 Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz 测试得到,开启 MKLDNN 加速策略,线程数为10。
C
cuicheng01 已提交
62
* 关于PP-LCNet的介绍可以参考[PP-LCNet介绍](../models/PP-LCNet.md),相关论文可以查阅[PP-LCNet paper](https://arxiv.org/abs/2109.15099)
C
cuicheng01 已提交
63 64


C
cuicheng01 已提交
65
<a name="2"></a>
C
cuicheng01 已提交
66

C
cuicheng01 已提交
67
## 2. 模型快速体验
littletomatodonkey's avatar
littletomatodonkey 已提交
68 69

<a name="2.1"></a>  
G
gaotingquan 已提交
70

C
cuicheng01 已提交
71
### 2.1 安装 paddlepaddle
G
gaotingquan 已提交
72

C
cuicheng01 已提交
73
- 您的机器安装的是 CUDA9 或 CUDA10,请运行以下命令安装
74

C
cuicheng01 已提交
75 76 77 78 79 80 81 82 83
```bash
python3 -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple
```

- 您的机器是CPU,请运行以下命令安装

```bash
python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
```
G
gaotingquan 已提交
84

C
cuicheng01 已提交
85
更多的版本需求,请参照[飞桨官网安装文档](https://www.paddlepaddle.org.cn/install/quick)中的说明进行操作。
G
gaotingquan 已提交
86

C
cuicheng01 已提交
87
<a name="2.2"></a>  
G
gaotingquan 已提交
88

C
cuicheng01 已提交
89
### 2.2 安装 paddleclas
90

C
cuicheng01 已提交
91
使用如下命令快速安装 paddleclas
92

littletomatodonkey's avatar
littletomatodonkey 已提交
93
```  
C
cuicheng01 已提交
94
pip3 install paddleclas
G
gaotingquan 已提交
95 96
```

C
cuicheng01 已提交
97
<a name="2.3"></a>
98

C
cuicheng01 已提交
99
### 2.3 预测
G
gaotingquan 已提交
100

C
cuicheng01 已提交
101
点击[这里](https://paddleclas.bj.bcebos.com/data/PULC/pulc_demo_imgs.zip)下载 demo 数据并解压,然后在终端中切换到相应目录。
102 103 104 105

* 使用命令行快速预测

```bash
C
cuicheng01 已提交
106
paddleclas --model_name=person_exists --infer_imgs=pulc_demo_imgs/person_exists/objects365_01780782.jpg
107 108 109 110 111
```

结果如下:
```
>>> result
C
cuicheng01 已提交
112
class_ids: [0], scores: [0.9955421453341842], label_names: ['nobody'], filename: pulc_demo_imgs/person_exists/objects365_01780782.jpg
113 114 115 116 117 118 119 120 121 122
Predict complete!
```

**备注**: 更换其他预测的数据时,只需要改变 `--infer_imgs=xx` 中的字段即可,支持传入整个文件夹。


* 在 Python 代码中预测
```python
import paddleclas
model = paddleclas.PaddleClas(model_name="person_exists")
C
cuicheng01 已提交
123
result = model.predict(input_data="pulc_demo_imgs/person_exists/objects365_01780782.jpg")
124 125 126 127 128 129 130
print(next(result))
```

**备注**`model.predict()` 为可迭代对象(`generator`),因此需要使用 `next()` 函数或 `for` 循环对其迭代调用。每次调用将以 `batch_size` 为单位进行一次预测,并返回预测结果, 默认 `batch_size` 为 1,如果需要更改 `batch_size`,实例化模型时,需要指定 `batch_size`,如 `model = paddleclas.PaddleClas(model_name="person_exists",  batch_size=2)`, 使用默认的代码返回结果示例如下:

```
>>> result
C
cuicheng01 已提交
131
[{'class_ids': [0], 'scores': [0.9955421453341842], 'label_names': ['nobody'], 'filename': 'pulc_demo_imgs/person_exists/objects365_01780782.jpg'}]
132 133
```

littletomatodonkey's avatar
littletomatodonkey 已提交
134
<a name="3"></a>
C
cuicheng01 已提交
135

C
cuicheng01 已提交
136
## 3. 模型训练、评估和预测
littletomatodonkey's avatar
littletomatodonkey 已提交
137

C
cuicheng01 已提交
138
<a name="3.1"></a>  
C
cuicheng01 已提交
139

C
cuicheng01 已提交
140
### 3.1 环境配置
C
cuicheng01 已提交
141

W
weishengyu 已提交
142
* 安装:请先参考文档[环境准备](../installation/install_paddleclas.md) 配置 PaddleClas 运行环境。
C
cuicheng01 已提交
143

littletomatodonkey's avatar
littletomatodonkey 已提交
144
<a name="3.2"></a>
C
cuicheng01 已提交
145

C
cuicheng01 已提交
146
### 3.2 数据准备
C
cuicheng01 已提交
147

littletomatodonkey's avatar
littletomatodonkey 已提交
148
<a name="3.2.1"></a>
149

C
cuicheng01 已提交
150
#### 3.2.1 数据集来源
151 152 153

本案例中所使用的所有数据集均为开源数据,`train` 集合为[MS-COCO 数据](https://cocodataset.org/#overview)的训练集的子集,`val` 集合为[Object365 数据](https://www.objects365.org/overview.html)的训练集的子集,`ImageNet_val`[ImageNet-1k 数据](https://www.image-net.org/)的验证集。

littletomatodonkey's avatar
littletomatodonkey 已提交
154
<a name="3.2.2"></a>  
C
cuicheng01 已提交
155

C
cuicheng01 已提交
156
#### 3.2.2 数据集获取
157 158 159 160 161 162 163 164 165 166 167 168 169 170

在公开数据集的基础上经过后处理即可得到本案例需要的数据,具体处理方法如下:

- 训练集合,本案例处理了 MS-COCO 数据训练集的标注文件,如果某张图含有“人”的标签,且这个框的面积在整张图中的比例大于 10%,即认为该张图中含有人,如果某张图中没有“人”的标签,则认为该张图中不含有人。经过处理后,得到 92964 条可用数据,其中有人的数据有 39813 条,无人的数据 53151 条。

- 验证集合,从 Object365 数据中随机抽取一小部分数据,使用在 MS-COCO 上训练得到的较好的模型预测这些数据,将预测结果和数据的标注文件取交集,将交集的结果按照得到训练集的方法筛选出验证集合。经过处理后,得到 27820 条可用数据。其中有人的数据有 2255 条,无人的数据有 25565 条。

处理后的数据集部分数据可视化如下:

![](../../images/PULC/docs/person_exists_data_demo.png)

此处提供了经过上述方法处理好的数据,可以直接下载得到。


C
cuicheng01 已提交
171 172 173 174 175 176 177 178 179 180
进入 PaddleClas 目录。

```
cd path_to_PaddleClas
```

进入 `dataset/` 目录,下载并解压有人/无人场景的数据。

```shell
cd dataset
181 182
wget https://paddleclas.bj.bcebos.com/data/PULC/person_exists.tar
tar -xf person_exists.tar
C
cuicheng01 已提交
183 184 185
cd ../
```

186
执行上述命令后,`dataset/` 下存在 `person_exists` 目录,该目录中具有以下数据:
C
cuicheng01 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

```

├── train
│   ├── 000000000009.jpg
│   ├── 000000000025.jpg
...
├── val
│   ├── objects365_01780637.jpg
│   ├── objects365_01780640.jpg
...
├── ImageNet_val
│   ├── ILSVRC2012_val_00000001.JPEG
│   ├── ILSVRC2012_val_00000002.JPEG
...
├── train_list.txt
├── train_list.txt.debug
├── train_list_for_distill.txt
├── val_list.txt
└── val_list.txt.debug
```

C
cuicheng01 已提交
209
其中 `train/``val/` 分别为训练集和验证集。`train_list.txt``val_list.txt` 分别为训练集和验证集的标签文件,`train_list.txt.debug``val_list.txt.debug` 分别为训练集和验证集的 `debug` 标签文件,其分别是 `train_list.txt``val_list.txt` 的子集,用该文件可以快速体验本案例的流程。`ImageNet_val/` 是 ImageNet-1k 的验证集,该集合和 `train` 集合的混合数据用于本案例的 `SKL-UGI知识蒸馏策略`,对应的训练标签文件为 `train_list_for_distill.txt`
littletomatodonkey's avatar
littletomatodonkey 已提交
210 211

**备注:**
C
cuicheng01 已提交
212

213
* 关于 `train_list.txt``val_list.txt`的格式说明,可以参考 [PaddleClas 分类数据集格式说明](../data_preparation/classification_dataset.md#1-数据集格式说明)
littletomatodonkey's avatar
littletomatodonkey 已提交
214 215

* 关于如何得到蒸馏的标签文件可以参考[知识蒸馏标签获得方法](../advanced_tutorials/ssld.md#3.2)
C
cuicheng01 已提交
216 217


littletomatodonkey's avatar
littletomatodonkey 已提交
218
<a name="3.3"></a>
C
cuicheng01 已提交
219

littletomatodonkey's avatar
littletomatodonkey 已提交
220
### 3.3 模型训练
C
cuicheng01 已提交
221 222


223
`ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml` 中提供了基于该场景的训练配置,可以通过如下脚本启动训练:
C
cuicheng01 已提交
224 225 226 227 228 229

```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3 -m paddle.distributed.launch \
    --gpus="0,1,2,3" \
    tools/train.py \
littletomatodonkey's avatar
littletomatodonkey 已提交
230
        -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml
C
cuicheng01 已提交
231 232
```

233
验证集的最佳指标在 `0.94-0.95` 之间(数据集较小,容易造成波动)。
C
cuicheng01 已提交
234

littletomatodonkey's avatar
littletomatodonkey 已提交
235
**备注:**
C
cuicheng01 已提交
236

237
* 此时使用的指标为Tpr,该指标描述了在假正类率(Fpr)小于某一个指标时的真正类率(Tpr),是产业中二分类问题常用的指标之一。在本案例中,Fpr 为千分之一。关于 Fpr 和 Tpr 的更多介绍,可以参考[这里](https://baike.baidu.com/item/AUC/19282953)
C
cuicheng01 已提交
238

239
* 在eval时,会打印出来当前最佳的 TprAtFpr 指标,具体地,其会打印当前的 `Fpr``Tpr` 值,以及当前的 `threshold`值,`Tpr` 值反映了在当前 `Fpr` 值下的召回率,该值越高,代表模型越好。`threshold` 表示当前最佳 `Fpr` 所对应的分类阈值,可用于后续模型部署落地等。
C
cuicheng01 已提交
240

C
cuicheng01 已提交
241
<a name="3.4"></a>
C
cuicheng01 已提交
242

C
cuicheng01 已提交
243
### 3.4 模型评估
C
cuicheng01 已提交
244 245 246 247 248 249

训练好模型之后,可以通过以下命令实现对模型指标的评估。

```bash
python3 tools/eval.py \
    -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml \
250
    -o Global.pretrained_model="output/PPLCNet_x1_0/best_model"
C
cuicheng01 已提交
251 252 253
```

其中 `-o Global.pretrained_model="output/PPLCNet_x1_0/best_model"` 指定了当前最佳权重所在的路径,如果指定其他权重,只需替换对应的路径即可。
C
cuicheng01 已提交
254

C
cuicheng01 已提交
255
<a name="3.5"></a>
C
cuicheng01 已提交
256

C
cuicheng01 已提交
257
### 3.5 模型预测
C
cuicheng01 已提交
258 259 260 261 262 263

模型训练完成之后,可以加载训练得到的预训练模型,进行模型预测。在模型库的 `tools/infer.py` 中提供了完整的示例,只需执行下述命令即可完成模型预测:

```python
python3 tools/infer.py \
    -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml \
G
gaotingquan 已提交
264
    -o Global.pretrained_model=output/PPLCNet_x1_0/best_model
C
cuicheng01 已提交
265 266 267 268 269
```

输出结果如下:

```
C
cuicheng01 已提交
270
[{'class_ids': [1], 'scores': [0.9999976], 'label_names': ['someone'], 'file_name': 'deploy/images/PULC/person_exists/objects365_02035329.jpg'}]
C
cuicheng01 已提交
271 272
```

littletomatodonkey's avatar
littletomatodonkey 已提交
273
**备注:**
C
cuicheng01 已提交
274 275

* 这里`-o Global.pretrained_model="output/PPLCNet_x1_0/best_model"` 指定了当前最佳权重所在的路径,如果指定其他权重,只需替换对应的路径即可。
littletomatodonkey's avatar
littletomatodonkey 已提交
276

C
cuicheng01 已提交
277
* 默认是对 `deploy/images/PULC/person_exists/objects365_02035329.jpg` 进行预测,此处也可以通过增加字段 `-o Infer.infer_imgs=xxx` 对其他图片预测。
littletomatodonkey's avatar
littletomatodonkey 已提交
278

279
* 二分类默认的阈值为0.5, 如果需要指定阈值,可以重写 `Infer.PostProcess.threshold` ,如`-o Infer.PostProcess.threshold=0.9794`,该值需要根据实际场景来确定,此处的 `0.9794` 是在该场景中的 `val` 数据集在千分之一 Fpr 下得到的最佳 Tpr 所得到的。
C
cuicheng01 已提交
280

C
cuicheng01 已提交
281

C
cuicheng01 已提交
282 283 284 285 286 287
<a name="4"></a>

## 4. 模型压缩

<a name="4.1"></a>

C
cuicheng01 已提交
288
### 4.1 SKL-UGI 知识蒸馏
C
cuicheng01 已提交
289

littletomatodonkey's avatar
littletomatodonkey 已提交
290 291 292
SKL-UGI 知识蒸馏是 PaddleClas 提出的一种简单有效的知识蒸馏方法,关于该方法的介绍,可以参考[SKL-UGI 知识蒸馏](../advanced_tutorials/ssld.md)

<a name="4.1.1"></a>
C
cuicheng01 已提交
293 294

#### 4.1.1 教师模型训练
C
cuicheng01 已提交
295

296
复用 `ppcls/configs/PULC/person_exists/PPLCNet/PPLCNet_x1_0.yaml` 中的超参数,训练教师模型,训练脚本如下:
C
cuicheng01 已提交
297 298 299 300 301 302

```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3 -m paddle.distributed.launch \
    --gpus="0,1,2,3" \
    tools/train.py \
C
cuicheng01 已提交
303
        -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml \
C
cuicheng01 已提交
304 305 306
        -o Arch.name=ResNet101_vd
```

307
验证集的最佳指标为 `0.96-0.98` 之间,当前教师模型最好的权重保存在 `output/ResNet101_vd/best_model.pdparams`
C
cuicheng01 已提交
308

littletomatodonkey's avatar
littletomatodonkey 已提交
309
<a name="4.1.2"></a>
C
cuicheng01 已提交
310

C
cuicheng01 已提交
311
####  4.1.2 蒸馏训练
C
cuicheng01 已提交
312

313
配置文件`ppcls/configs/PULC/person_exists/PPLCNet_x1_0_distillation.yaml`提供了`SKL-UGI知识蒸馏策略`的配置。该配置将`ResNet101_vd`当作教师模型,`PPLCNet_x1_0`当作学生模型,使用ImageNet数据集的验证集作为新增的无标签数据。训练脚本如下:
C
cuicheng01 已提交
314 315 316 317 318 319

```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3 -m paddle.distributed.launch \
    --gpus="0,1,2,3" \
    tools/train.py \
320
        -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0_distillation.yaml \
C
cuicheng01 已提交
321 322 323
        -o Arch.models.0.Teacher.pretrained=output/ResNet101_vd/best_model
```

324
验证集的最佳指标为 `0.95-0.97` 之间,当前模型最好的权重保存在 `output/DistillationModel/best_model_student.pdparams`
C
cuicheng01 已提交
325 326


littletomatodonkey's avatar
littletomatodonkey 已提交
327
<a name="5"></a>
C
cuicheng01 已提交
328

C
cuicheng01 已提交
329
## 5. 超参搜索
C
cuicheng01 已提交
330

G
gaotingquan 已提交
331
[3.3 节](#3.3)[4.1 节](#4.1)所使用的超参数是根据 PaddleClas 提供的 `超参数搜索策略` 搜索得到的,如果希望在自己的数据集上得到更好的结果,可以参考[超参数搜索策略](PULC_train.md#4-超参搜索)来获得更好的训练超参数。
C
cuicheng01 已提交
332

C
cuicheng01 已提交
333
**备注:** 此部分内容是可选内容,搜索过程需要较长的时间,您可以根据自己的硬件情况来选择执行。如果没有更换数据集,可以忽略此节内容。
C
cuicheng01 已提交
334

C
cuicheng01 已提交
335
<a name="6"></a>
C
cuicheng01 已提交
336

C
cuicheng01 已提交
337
## 6. 模型推理部署
C
cuicheng01 已提交
338

littletomatodonkey's avatar
littletomatodonkey 已提交
339
<a name="6.1"></a>
C
cuicheng01 已提交
340 341 342

### 6.1 推理模型准备

343
Paddle Inference 是飞桨的原生推理库, 作用于服务器端和云端,提供高性能的推理能力。相比于直接基于预训练模型进行预测,Paddle Inference可使用 MKLDNN、CUDNN、TensorRT 进行预测加速,从而实现更优的推理性能。更多关于 Paddle Inference 推理引擎的介绍,可以参考 [Paddle Inference官网教程](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/infer/inference/inference_cn.html)
littletomatodonkey's avatar
littletomatodonkey 已提交
344

C
cuicheng01 已提交
345
当使用 Paddle Inference 推理时,加载的模型类型为 inference 模型。本案例提供了两种获得 inference 模型的方法,如果希望得到和文档相同的结果,请选择[直接下载 inference 模型](#6.1.2)的方式。
C
cuicheng01 已提交
346

littletomatodonkey's avatar
littletomatodonkey 已提交
347
<a name="6.1.1"></a>
C
cuicheng01 已提交
348

C
cuicheng01 已提交
349
### 6.1.1 基于训练得到的权重导出 inference 模型
C
cuicheng01 已提交
350 351

此处,我们提供了将权重和模型转换的脚本,执行该脚本可以得到对应的 inference 模型:
C
cuicheng01 已提交
352 353

```bash
C
cuicheng01 已提交
354
python3 tools/export_model.py \
355
    -c ./ppcls/configs/PULC/person_exists/PPLCNet_x1_0.yaml \
C
cuicheng01 已提交
356 357
    -o Global.pretrained_model=output/DistillationModel/best_model_student \
    -o Global.save_inference_dir=deploy/models/PPLCNet_x1_0_person_exists_infer
C
cuicheng01 已提交
358
```
C
cuicheng01 已提交
359
执行完该脚本后会在 `deploy/models/` 下生成 `PPLCNet_x1_0_person_exists_infer` 文件夹,`models` 文件夹下应有如下文件结构:
C
cuicheng01 已提交
360

C
cuicheng01 已提交
361 362 363 364 365 366
```
├── PPLCNet_x1_0_person_exists_infer
│   ├── inference.pdiparams
│   ├── inference.pdiparams.info
│   └── inference.pdmodel
```
C
cuicheng01 已提交
367

C
cuicheng01 已提交
368
**备注:** 此处的最佳权重是经过知识蒸馏后的权重路径,如果没有执行知识蒸馏的步骤,最佳模型保存在`output/PPLCNet_x1_0/best_model.pdparams`中。
C
cuicheng01 已提交
369

littletomatodonkey's avatar
littletomatodonkey 已提交
370
<a name="6.1.2"></a>
C
cuicheng01 已提交
371

C
cuicheng01 已提交
372 373 374 375 376 377 378 379
### 6.1.2 直接下载 inference 模型

[6.1.1 小节](#6.1.1)提供了导出 inference 模型的方法,此处也提供了该场景可以下载的 inference 模型,可以直接下载体验。

```
cd deploy/models
# 下载 inference 模型并解压
wget https://paddleclas.bj.bcebos.com/models/PULC/person_exists_infer.tar && tar -xf person_exists_infer.tar
C
cuicheng01 已提交
380 381
```

C
cuicheng01 已提交
382
解压完毕后,`models` 文件夹下应有如下文件结构:
C
cuicheng01 已提交
383 384

```
C
cuicheng01 已提交
385 386 387 388
├── person_exists_infer
│   ├── inference.pdiparams
│   ├── inference.pdiparams.info
│   └── inference.pdmodel
C
cuicheng01 已提交
389 390
```

littletomatodonkey's avatar
littletomatodonkey 已提交
391
<a name="6.2"></a>
C
cuicheng01 已提交
392

C
cuicheng01 已提交
393
### 6.2 基于 Python 预测引擎推理
C
cuicheng01 已提交
394

C
cuicheng01 已提交
395

C
cuicheng01 已提交
396
<a name="6.2.1"></a>  
C
cuicheng01 已提交
397

C
cuicheng01 已提交
398
#### 6.2.1 预测单张图像
C
cuicheng01 已提交
399

C
cuicheng01 已提交
400
返回 `deploy` 目录:
C
cuicheng01 已提交
401 402

```
C
cuicheng01 已提交
403 404
cd ../
```
C
cuicheng01 已提交
405

C
cuicheng01 已提交
406
运行下面的命令,对图像 `./images/PULC/person_exists/objects365_02035329.jpg` 进行有人/无人分类。
C
cuicheng01 已提交
407

C
cuicheng01 已提交
408 409
```shell
# 使用下面的命令使用 GPU 进行预测
410
python3.7 python/predict_cls.py -c configs/PULC/person_exists/inference_person_exists.yaml
C
cuicheng01 已提交
411
# 使用下面的命令使用 CPU 进行预测
412
python3.7 python/predict_cls.py -c configs/PULC/person_exists/inference_person_exists.yaml -o Global.use_gpu=False
C
cuicheng01 已提交
413
```
C
cuicheng01 已提交
414

C
cuicheng01 已提交
415
输出结果如下。
C
cuicheng01 已提交
416 417

```
C
cuicheng01 已提交
418
objects365_02035329.jpg:	class id(s): [1], score(s): [1.00], label_name(s): ['someone']
C
cuicheng01 已提交
419 420
```

C
cuicheng01 已提交
421

C
cuicheng01 已提交
422
**备注:** 二分类默认的阈值为0.5, 如果需要指定阈值,可以重写 `Infer.PostProcess.threshold` ,如`-o Infer.PostProcess.threshold=0.9794`,该值需要根据实际场景来确定,此处的 `0.9794` 是在该场景中的 `val` 数据集在千分之一 Fpr 下得到的最佳 Tpr 所得到的。该阈值的确定方法可以参考[3.3节](#3.3)备注部分。
C
cuicheng01 已提交
423

C
cuicheng01 已提交
424
<a name="6.2.2"></a>  
C
cuicheng01 已提交
425

C
cuicheng01 已提交
426
#### 6.2.2 基于文件夹的批量预测
C
cuicheng01 已提交
427

C
cuicheng01 已提交
428
如果希望预测文件夹内的图像,可以直接修改配置文件中的 `Global.infer_imgs` 字段,也可以通过下面的 `-o` 参数修改对应的配置。
C
cuicheng01 已提交
429

C
cuicheng01 已提交
430 431 432 433
```shell
# 使用下面的命令使用 GPU 进行预测,如果希望使用 CPU 预测,可以在命令后面添加 -o Global.use_gpu=False
python3.7 python/predict_cls.py -c configs/PULC/person_exists/inference_person_exists.yaml -o Global.infer_imgs="./images/PULC/person_exists/"
```
C
cuicheng01 已提交
434

C
cuicheng01 已提交
435 436 437 438 439 440 441 442 443
终端中会输出该文件夹内所有图像的分类结果,如下所示。

```
objects365_01780782.jpg:	class id(s): [0], score(s): [1.00], label_name(s): ['nobody']
objects365_02035329.jpg:	class id(s): [1], score(s): [1.00], label_name(s): ['someone']
```

其中,`someone` 表示该图里存在人,`nobody` 表示该图里不存在人。

littletomatodonkey's avatar
littletomatodonkey 已提交
444
<a name="6.3"></a>
C
cuicheng01 已提交
445 446 447

### 6.3 基于 C++ 预测引擎推理

C
cuicheng01 已提交
448
PaddleClas 提供了基于 C++ 预测引擎推理的示例,您可以参考[服务器端 C++ 预测](../inference_deployment/cpp_deploy.md)来完成相应的推理部署。如果您使用的是 Windows 平台,可以参考[基于 Visual Studio 2019 Community CMake 编译指南](../inference_deployment/cpp_deploy_on_windows.md)完成相应的预测库编译和模型预测工作。
C
cuicheng01 已提交
449

littletomatodonkey's avatar
littletomatodonkey 已提交
450
<a name="6.4"></a>
C
cuicheng01 已提交
451 452

### 6.4 服务化部署
C
cuicheng01 已提交
453

C
cuicheng01 已提交
454
Paddle Serving 提供高性能、灵活易用的工业级在线推理服务。Paddle Serving 支持 RESTful、gRPC、bRPC 等多种协议,提供多种异构硬件和多种操作系统环境下推理解决方案。更多关于Paddle Serving 的介绍,可以参考[Paddle Serving 代码仓库](https://github.com/PaddlePaddle/Serving)
littletomatodonkey's avatar
littletomatodonkey 已提交
455

C
cuicheng01 已提交
456 457
PaddleClas 提供了基于 Paddle Serving 来完成模型服务化部署的示例,您可以参考[模型服务化部署](../inference_deployment/paddle_serving_deploy.md)来完成相应的部署工作。

littletomatodonkey's avatar
littletomatodonkey 已提交
458
<a name="6.5"></a>
C
cuicheng01 已提交
459

C
cuicheng01 已提交
460
### 6.5 端侧部署
C
cuicheng01 已提交
461

C
cuicheng01 已提交
462
Paddle Lite 是一个高性能、轻量级、灵活性强且易于扩展的深度学习推理框架,定位于支持包括移动端、嵌入式以及服务器端在内的多硬件平台。更多关于 Paddle Lite 的介绍,可以参考[Paddle Lite 代码仓库](https://github.com/PaddlePaddle/Paddle-Lite)
littletomatodonkey's avatar
littletomatodonkey 已提交
463

C
cuicheng01 已提交
464
PaddleClas 提供了基于 Paddle Lite 来完成模型端侧部署的示例,您可以参考[端侧部署](../inference_deployment/paddle_lite_deploy.md)来完成相应的部署工作。
C
cuicheng01 已提交
465

littletomatodonkey's avatar
littletomatodonkey 已提交
466
<a name="6.6"></a>
C
cuicheng01 已提交
467

C
cuicheng01 已提交
468
### 6.6 Paddle2ONNX 模型转换与预测
littletomatodonkey's avatar
littletomatodonkey 已提交
469

C
cuicheng01 已提交
470
Paddle2ONNX 支持将 PaddlePaddle 模型格式转化到 ONNX 模型格式。通过 ONNX 可以完成将 Paddle 模型到多种推理引擎的部署,包括TensorRT/OpenVINO/MNN/TNN/NCNN,以及其它对 ONNX 开源格式进行支持的推理引擎或硬件。更多关于 Paddle2ONNX 的介绍,可以参考[Paddle2ONNX 代码仓库](https://github.com/PaddlePaddle/Paddle2ONNX)
C
cuicheng01 已提交
471

C
cuicheng01 已提交
472
PaddleClas 提供了基于 Paddle2ONNX 来完成 inference 模型转换 ONNX 模型并作推理预测的示例,您可以参考[Paddle2ONNX 模型转换与预测](../../../deploy/paddle2onnx/readme.md)来完成相应的部署工作。