README.md 7.5 KB
Newer Older
Z
Zeyu Chen 已提交
1
# HumanSeg人像分割模型
W
wuyefeilin 已提交
2

Z
Zeyu Chen 已提交
3
本教程基于PaddleSeg核心分割网络,提供针对人像分割场景从预训练模型、Fine-tune、视频分割预测部署的全流程应用指南。最新发布HumanSeg-lite模型超轻量级人像分割模型,支持移动端场景的实时分割。
C
chenguowei01 已提交
4 5 6

## 环境依赖

Z
Zeyu Chen 已提交
7 8 9
* Python == 3.5/3.6/3.7
* PaddlePaddle >= 1.7.2

Z
Zeyu Chen 已提交
10
PaddlePaddle的安装可参考[飞桨快速安装](https://www.paddlepaddle.org.cn/install/quick)
C
chenguowei01 已提交
11 12 13 14 15 16

通过以下命令安装python包依赖,请确保在该分支上至少执行过一次以下命令
```shell
$ pip install -r requirements.txt
```

Z
Zeyu Chen 已提交
17
## 预训练模型
Z
Zeyu Chen 已提交
18
HumanSeg开放了在大规模人像数据上训练的三个预训练模型,满足多种使用场景的需求
C
chenguowei01 已提交
19

Z
Zeyu Chen 已提交
20
| 模型类型 | Checkpoint | Inference Model | Quant Inference Model | 备注 |
C
chenguowei01 已提交
21 22 23 24 25 26 27 28
| --- | --- | --- | ---| --- |
| HumanSeg-server  | [humanseg_server_ckpt](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_server_ckpt.zip) | [humanseg_server_inference](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_server_inference.zip) | -- | 高精度模型,适用于服务端GPU且背景复杂的人像场景, 模型结构为Deeplabv3+/Xcetion65  |
| HumanSeg-mobile | [humanseg_mobile_ckpt](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_mobile_ckpt.zip) | [humanseg_mobile_inference](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_mobile_inference.zip) | [humanseg_mobile_quant](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_mobile_quant.zip) | 轻量级模型, 适用于移动端或服务端CPU的前置摄像头场景,模型结构为HRNet_w18_samll_v1  |
| HumanSeg-lite | [humanseg_lite_ckpt](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_lite_ckpt.zip) | [humanseg_lite_inference](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_lite_inference.zip) |  [humanseg_lite_quant](https://paddleseg.bj.bcebos.com/humanseg/models/humanseg_lite_quant.zip) | 超轻量级模型, 适用于手机自拍人像,且有移动端实时分割场景, 模型结构为优化的ShuffleNetV2 |

模型计算耗时(小米,cpu:骁龙855, 内存:6GB, 图片大小:192*192)

| 模型 | humanseg_mobile_inference | humanseg_mobile_quant | humanseg_lite_inference | humanseg_lite_quant |
C
chenguowei01 已提交
29
| --- | --- | --- | --- | --- |
C
chenguowei01 已提交
30 31
| 耗时(ms) | 42.25 | 24.93 | 17.26 | 11.89 |

Z
Zeyu Chen 已提交
32 33 34

**NOTE:**
其中Checkpoint为模型权重,用于Fine-tuning场景。
Z
Zeyu Chen 已提交
35

Z
Zeyu Chen 已提交
36
* Inference Model和Quant Inference Model为预测部署模型,包含`__model__`计算图结构、`__params__`模型参数和`model.yaml`基础的模型配置信息。
Z
Zeyu Chen 已提交
37

Z
Zeyu Chen 已提交
38
* 其中Inference Model适用于服务端的CPU和GPU预测部署,Qunat Inference Model为量化版本,适用于通过Paddle Lite进行移动端等端侧设备部署。更多Paddle Lite部署说明查看[Paddle Lite文档](https://paddle-lite.readthedocs.io/zh/latest/)
C
chenguowei01 已提交
39

Z
Zeyu Chen 已提交
40
执行以下脚本进行HumanSeg预训练模型的下载
C
chenguowei01 已提交
41
```bash
Z
Zeyu Chen 已提交
42
python pretrained_weights/download_pretrained_weights.py
C
chenguowei01 已提交
43 44
```

Z
Zeyu Chen 已提交
45
## 下载测试数据
Z
Zeyu Chen 已提交
46
我们提供了[supervise.ly](https://supervise.ly/)发布人像分割数据集**Supervisely Persons**, 从中随机抽取一小部分并转化成PaddleSeg可直接加载数据格式。通过运行以下代码进行快速下载,其中包含手机前置摄像头的人像测试视频`video_test.mp4`.
C
chenguowei01 已提交
47 48 49 50 51

```bash
python data/download_data.py
```

Z
Zeyu Chen 已提交
52
## 快速体验视频流人像分割
C
chenguowei01 已提交
53
结合DIS(Dense Inverse Search-basedmethod)光流算法预测结果与分割结果,改善视频流人像分割
C
chenguowei01 已提交
54
```bash
Z
Zeyu Chen 已提交
55
# 通过电脑摄像头进行实时分割处理
C
chenguowei01 已提交
56
python video_infer.py --model_dir pretrained_weights/humanseg_lite_inference
C
chenguowei01 已提交
57

Z
Zeyu Chen 已提交
58
# 对人像视频进行分割处理
C
chenguowei01 已提交
59
python video_infer.py --model_dir pretrained_weights/humanseg_lite_inference --video_path data/video_test.mp4
C
chenguowei01 已提交
60 61
```

C
chenguowei01 已提交
62 63 64 65
视频分割结果如下:

<img src="https://paddleseg.bj.bcebos.com/humanseg/data/video_test.gif" width="20%" height="20%"><img src="https://paddleseg.bj.bcebos.com/humanseg/data/result.gif" width="20%" height="20%">

Z
Zeyu Chen 已提交
66 67 68
**NOTE**:

视频分割处理时间需要几分钟,请耐心等待。
C
chenguowei01 已提交
69

C
chenguowei01 已提交
70
## 训练
Z
Zeyu Chen 已提交
71
使用下述命令基于与训练模型进行Fine-tuning,请确保选用的模型结构`model_type`与模型参数`pretrained_weights`匹配。
C
chenguowei01 已提交
72
```bash
C
chenguowei01 已提交
73
python train.py --model_type HumanSegMobile \
C
chenguowei01 已提交
74 75 76 77
--save_dir output/ \
--data_dir data/mini_supervisely \
--train_list data/mini_supervisely/train.txt \
--val_list data/mini_supervisely/val.txt \
C
chenguowei01 已提交
78
--pretrained_weights pretrained_weights/humanseg_mobile_ckpt \
C
chenguowei01 已提交
79 80
--batch_size 8 \
--learning_rate 0.001 \
C
chenguowei01 已提交
81 82
--num_epochs 10 \
--image_shape 192 192
C
chenguowei01 已提交
83 84 85 86 87 88 89 90 91 92 93
```
其中参数含义如下:
* `--model_type`: 模型类型,可选项为:HumanSegServer、HumanSegMobile和HumanSegLite
* `--save_dir`: 模型保存路径
* `--data_dir`: 数据集路径
* `--train_list`: 训练集列表路径
* `--val_list`: 验证集列表路径
* `--pretrained_weights`: 预训练模型路径
* `--batch_size`: 批大小
* `--learning_rate`: 初始学习率
* `--num_epochs`: 训练轮数
C
chenguowei01 已提交
94
* `--image_shape`: 网络输入图像大小(w, h)
C
chenguowei01 已提交
95

Z
Zeyu Chen 已提交
96
更多命令行帮助可运行下述命令进行查看:
C
chenguowei01 已提交
97 98 99
```bash
python train.py --help
```
Z
Zeyu Chen 已提交
100
**NOTE**
Z
Zeyu Chen 已提交
101
可通过更换`--model_type`变量与对应的`--pretrained_weights`使用不同的模型快速尝试。
C
chenguowei01 已提交
102 103 104 105 106 107

## 评估
使用下述命令进行评估
```bash
python val.py --model_dir output/best_model \
--data_dir data/mini_supervisely \
C
chenguowei01 已提交
108 109
--val_list data/mini_supervisely/val.txt \
--image_shape 192 192
C
chenguowei01 已提交
110 111 112 113 114
```
其中参数含义如下:
* `--model_dir`: 模型路径
* `--data_dir`: 数据集路径
* `--val_list`: 验证集列表路径
C
chenguowei01 已提交
115
* `--image_shape`: 网络输入图像大小(w, h)
C
chenguowei01 已提交
116 117 118 119 120 121

## 预测
使用下述命令进行预测
```bash
python infer.py --model_dir output/best_model \
--data_dir data/mini_supervisely \
C
chenguowei01 已提交
122 123
--test_list data/mini_supervisely/test.txt \
--image_shape 192 192
C
chenguowei01 已提交
124 125 126 127 128
```
其中参数含义如下:
* `--model_dir`: 模型路径
* `--data_dir`: 数据集路径
* `--test_list`: 测试集列表路径
C
chenguowei01 已提交
129
* `--image_shape`: 网络输入图像大小(w, h)
C
chenguowei01 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

## 模型导出
```bash
python export.py --model_dir output/best_model \
--save_dir output/export
```
其中参数含义如下:
* `--model_dir`: 模型路径
* `--save_dir`: 导出模型保存路径

## 离线量化
```bash
python quant_offline.py --model_dir output/best_model \
--data_dir data/mini_supervisely \
--quant_list data/mini_supervisely/val.txt \
C
chenguowei01 已提交
145 146
--save_dir output/quant_offline \
--image_shape 192 192
C
chenguowei01 已提交
147 148 149 150 151 152
```
其中参数含义如下:
* `--model_dir`: 待量化模型路径
* `--data_dir`: 数据集路径
* `--quant_list`: 量化数据集列表路径,一般直接选择训练集或验证集
* `--save_dir`: 量化模型保存路径
C
chenguowei01 已提交
153
* `--image_shape`: 网络输入图像大小(w, h)
C
chenguowei01 已提交
154 155 156 157 158 159 160 161 162 163 164 165

## 在线量化
利用float训练模型进行在线量化。
```bash
python quant_online.py --model_type HumanSegMobile \
--save_dir output/quant_online \
--data_dir data/mini_supervisely \
--train_list data/mini_supervisely/train.txt \
--val_list data/mini_supervisely/val.txt \
--pretrained_weights output/best_model \
--batch_size 2 \
--learning_rate 0.001 \
C
chenguowei01 已提交
166 167
--num_epochs 2 \
--image_shape 192 192
C
chenguowei01 已提交
168 169 170 171 172 173 174 175 176 177 178
```
其中参数含义如下:
* `--model_type`: 模型类型,可选项为:HumanSegServer、HumanSegMobile和HumanSegLite
* `--save_dir`: 模型保存路径
* `--data_dir`: 数据集路径
* `--train_list`: 训练集列表路径
* `--val_list`: 验证集列表路径
* `--pretrained_weights`: 预训练模型路径,
* `--batch_size`: 批大小
* `--learning_rate`: 初始学习率
* `--num_epochs`: 训练轮数
C
chenguowei01 已提交
179
* `--image_shape`: 网络输入图像大小(w, h)