|
| 1 | +# 人像分割HumanSeg |
| 2 | + |
| 3 | +本教程基于PaddleSeg提供高精度人像分割模型,从训练到部署的全流程应用指南,以及视频流人像分割、背景替换的实际效果体验。最新发布超轻量级人像分割模型,支持Web端、移动端场景的实时分割。 |
| 4 | + |
| 5 | +- [人像分割模型](#人像分割模型) |
| 6 | +- [安装](#安装) |
| 7 | +- [快速体验](#快速体验) |
| 8 | + - [视频流人像分割](##视频流人像分割) |
| 9 | + - [视频流背景替换](##视频流背景替换) |
| 10 | +- [训练评估预测](#训练评估预测) |
| 11 | +- [模型导出](#模型导出) |
| 12 | +- [Web端部署](#Web端部署) |
| 13 | +- [移动端部署](#移动端部署) |
| 14 | + |
| 15 | +## 人像分割模型 |
| 16 | +HumanSeg开放了在大规模人像数据上训练的三个人像模型,满足服务端、移动端、Web端多种使用场景的需求 |
| 17 | + |
| 18 | +| 模型类型 | 适用场景 | Checkpoint | Inference Model | |
| 19 | +| --- | --- | --- | ---| |
| 20 | +| 高精度模型 | 适用于服务端GPU且背景复杂的人像场景, 模型结构为Deeplabv3+/ResNet50, 输入大小(512, 512) |[humanseg_server_ckpt](https://paddleseg.bj.bcebos.com/dygraph/humanseg/train/deeplabv3p_resnet50_os8_humanseg_512x512_100k.zip) | [humanseg_server_inference](https://paddleseg.bj.bcebos.com/dygraph/humanseg/export/deeplabv3p_resnet50_os8_humanseg_512x512_100k_with_softmax.zip) | |
| 21 | +| 轻量级模型 | 适用于移动端或服务端CPU的前置摄像头场景,模型结构为HRNet_w18_samll_v1,输入大小(192, 192) | [humanseg_mobile_ckpt](https://paddleseg.bj.bcebos.com/dygraph/humanseg/train/fcn_hrnetw18_small_v1_humanseg_192x192.zip) | [humanseg_mobile_inference](https://paddleseg.bj.bcebos.com/dygraph/humanseg/export/fcn_hrnetw18_small_v1_humanseg_192x192_with_softmax.zip) | |
| 22 | +| 超轻量级模型 | 适用于Web端或移动端实时分割场景,例如手机自拍、Web视频会议,模型结构为优化的ShuffleNetV2,输入大小(192, 192) | [humanseg_lite_ckpt](https://paddleseg.bj.bcebos.com/dygraph/humanseg/train/shufflenetv2_humanseg_192x192.zip) | [humanseg_lite_inference](https://paddleseg.bj.bcebos.com/dygraph/humanseg/export/shufflenetv2_humanseg_192x192_with_softmax.zip) | |
| 23 | + |
| 24 | +**NOTE:** |
| 25 | +* 其中Checkpoint为模型权重,用于Fine-tuning场景。 |
| 26 | + |
| 27 | +* Inference Model为预测部署模型,包含`model.pdmodel`计算图结构、`model.pdiparams`模型参数和`deploy.yaml`基础的模型配置信息。 |
| 28 | + |
| 29 | +* 其中Inference Model适用于服务端的CPU和GPU预测部署,适用于通过Paddle Lite进行移动端等端侧设备部署。更多Paddle Lite部署说明查看[Paddle Lite文档](https://paddle-lite.readthedocs.io/zh/latest/) |
| 30 | + |
| 31 | + |
| 32 | +### 计算复杂度和参数量 |
| 33 | +| 模型类型 | Network | Input Size | FLOPS | Parameters | |
| 34 | +|-|-|-|-|-| |
| 35 | +| 超轻量级模型 | ShuffleNetV2 | 192x192 | 121272192 (121M) | 137012 (137K) | |
| 36 | +| 轻量级模型 | HRNet w18 small v1 | 192x192 | 584182656 (584M) | 1543954 (1.54M) | |
| 37 | +| 高精度模型 | Deeplabv3+/ResNet50 | 512x512 | 114148802560 (114G) | 26789874 (26.8M) |
| 38 | + |
| 39 | + |
| 40 | +<!-- | ShuffleNetV2 | 398x224 | 293631872(294M) | 137012(137K) | --> |
| 41 | + |
| 42 | +## 安装 |
| 43 | + |
| 44 | +#### 1. 安装PaddlePaddle |
| 45 | + |
| 46 | +版本要求 |
| 47 | + |
| 48 | +* PaddlePaddle >= 2.0.2 |
| 49 | + |
| 50 | +* Python >= 3.7+ |
| 51 | + |
| 52 | +由于图像分割模型计算开销大,推荐在GPU版本的PaddlePaddle下使用PaddleSeg。推荐安装10.0以上的CUDA环境。安装教程请见[PaddlePaddle官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html)。 |
| 53 | + |
| 54 | + |
| 55 | +#### 2. 安装PaddleSeg包 |
| 56 | + |
| 57 | +```shell |
| 58 | +pip install paddleseg |
| 59 | +``` |
| 60 | + |
| 61 | +#### 3. 下载PaddleSeg仓库 |
| 62 | + |
| 63 | +```shell |
| 64 | +git clone https://github.com/PaddlePaddle/PaddleSeg |
| 65 | +``` |
| 66 | + |
| 67 | +## 快速体验 |
| 68 | +### 下载Inference Model |
| 69 | + |
| 70 | +执行以下脚本快速下载所有Inference Model |
| 71 | +```bash |
| 72 | +python export_model/download_export_model.py |
| 73 | +``` |
| 74 | + |
| 75 | +### 下载测试数据 |
| 76 | +我们提供了[supervise.ly](https://supervise.ly/)发布人像分割数据集**Supervisely Persons**, 从中随机抽取一小部分并转化成PaddleSeg可直接加载数据格式,同时提供了手机前置摄像头的人像测试视频`video_test.mp4`。通过运行以下代码进行快速下载: |
| 77 | + |
| 78 | +```bash |
| 79 | +python data/download_data.py |
| 80 | +``` |
| 81 | + |
| 82 | +### 视频流人像分割 |
| 83 | +结合DIS(Dense Inverse Search-basedmethod)光流算法预测结果与分割结果,改善视频流人像分割 |
| 84 | +```bash |
| 85 | +# 通过电脑摄像头进行实时分割处理 |
| 86 | +python video_infer.py \ |
| 87 | +--cfg export_model/shufflenetv2_humanseg_192x192_with_softmax/deploy.yaml |
| 88 | + |
| 89 | +# 对人像视频进行分割处理 |
| 90 | +python video_infer.py \ |
| 91 | +--cfg export_model/deeplabv3p_resnet50_os8_humanseg_512x512_100k_with_softmax/deploy.yaml \ |
| 92 | +--video_path data/video_test.mp4 |
| 93 | +``` |
| 94 | + |
| 95 | +视频分割结果如下: |
| 96 | + |
| 97 | +<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%"> |
| 98 | + |
| 99 | +### 视频流背景替换 |
| 100 | +根据所选背景进行背景替换,背景可以是一张图片,也可以是一段视频。 |
| 101 | +```bash |
| 102 | +# 通过电脑摄像头进行实时背景替换处理, 也可通过'--background_video_path'传入背景视频 |
| 103 | +python bg_replace.py \ |
| 104 | +--cfg export_model/shufflenetv2_humanseg_192x192_with_softmax/deploy.yaml \ |
| 105 | +--background_image_path data/background.jpg |
| 106 | + |
| 107 | +# 对人像视频进行背景替换处理, 也可通过'--background_video_path'传入背景视频 |
| 108 | +python bg_replace.py \ |
| 109 | +--cfg export_model/deeplabv3p_resnet50_os8_humanseg_512x512_100k_with_softmax/deploy.yaml \ |
| 110 | +--background_image_path data/background.jpg \ |
| 111 | +--video_path data/video_test.mp4 |
| 112 | + |
| 113 | +# 对单张图像进行背景替换 |
| 114 | +python bg_replace.py \ |
| 115 | +--cfg export_model/deeplabv3p_resnet50_os8_humanseg_512x512_100k_with_softmax/deploy.yaml \ |
| 116 | +--background_image_path data/background.jpg \ |
| 117 | +--image_path data/human_image.jpg |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +背景替换结果如下: |
| 122 | + |
| 123 | +<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/bg_replace.gif" width="20%" height="20%"> |
| 124 | + |
| 125 | + |
| 126 | +**NOTE**: |
| 127 | + |
| 128 | +视频分割处理时间需要几分钟,请耐心等待。 |
| 129 | + |
| 130 | +提供的模型适用于手机摄像头竖屏拍摄场景,宽屏效果会略差一些。 |
| 131 | + |
| 132 | +## 训练评估预测 |
| 133 | +### 下载预训练模型 |
| 134 | + |
| 135 | +执行以下脚本快速下载所有Checkpoint作为预训练模型 |
| 136 | +```bash |
| 137 | +python pretrained_model/download_pretrained_model.py |
| 138 | +``` |
| 139 | + |
| 140 | +### 训练 |
| 141 | +基于上述大规模数据预训练的模型,在抽取的部分[supervise.ly](https://supervise.ly/)数据集上进行Fine-tuning,以HRNet w18 small v1为例,训练命令如下: |
| 142 | +```bash |
| 143 | +export CUDA_VISIBLE_DEVICES=0 # 设置1张可用的卡 |
| 144 | +# windows下请执行以下命令 |
| 145 | +# set CUDA_VISIBLE_DEVICES=0 |
| 146 | +python train.py \ |
| 147 | +--config configs/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely.yml \ |
| 148 | +--save_dir saved_model/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely \ |
| 149 | +--save_interval 100 --do_eval --use_vdl |
| 150 | +``` |
| 151 | + |
| 152 | +更多命令行帮助可运行下述命令进行查看: |
| 153 | +```bash |
| 154 | +python train.py --help |
| 155 | +``` |
| 156 | + |
| 157 | +### 评估 |
| 158 | +使用下述命令进行评估 |
| 159 | +```bash |
| 160 | +python val.py \ |
| 161 | +--config configs/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely.yml \ |
| 162 | +--model_path saved_model/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely/best_model/model.pdparams |
| 163 | +``` |
| 164 | + |
| 165 | +### 预测 |
| 166 | +使用下述命令进行预测, 预测结果默认保存在`./output/result/`文件夹中。 |
| 167 | +```bash |
| 168 | +python predict.py \ |
| 169 | +--config configs/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely.yml \ |
| 170 | +--model_path saved_model/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely/best_model/model.pdparams \ |
| 171 | +--image_path data/human_image.jpg |
| 172 | +``` |
| 173 | + |
| 174 | +## 模型导出 |
| 175 | +### 将模型导出为静态图模型 |
| 176 | + |
| 177 | +请确保位于PaddleSeg目录下,执行以下脚本: |
| 178 | + |
| 179 | +```shell |
| 180 | +export CUDA_VISIBLE_DEVICES=0 # 设置1张可用的卡 |
| 181 | +# windows下请执行以下命令 |
| 182 | +# set CUDA_VISIBLE_DEVICES=0 |
| 183 | +python ../../export.py \ |
| 184 | +--config configs/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely.yml \ |
| 185 | +--model_path saved_model/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely/best_model/model.pdparams \ |
| 186 | +--save_dir export_model/fcn_hrnetw18_small_v1_humanseg_192x192_mini_supervisely_with_softmax \ |
| 187 | +--without_argmax --with_softmax |
| 188 | +``` |
| 189 | + |
| 190 | +### 导出脚本参数解释 |
| 191 | + |
| 192 | +|参数名|用途|是否必选项|默认值| |
| 193 | +|-|-|-|-| |
| 194 | +|config|配置文件|是|-| |
| 195 | +|save_dir|模型和visualdl日志文件的保存根路径|否|output| |
| 196 | +|model_path|预训练模型参数的路径|否|配置文件中指定值| |
| 197 | +|with_softmax|在网络末端添加softmax算子。由于PaddleSeg组网默认返回logits,如果想要部署模型获取概率值,可以置为True|否|False| |
| 198 | +|without_argmax|是否不在网络末端添加argmax算子。由于PaddleSeg组网默认返回logits,为部署模型可以直接获取预测结果,我们默认在网络末端添加argmax算子|否|False| |
| 199 | + |
| 200 | +### 结果文件 |
| 201 | + |
| 202 | +```shell |
| 203 | +output |
| 204 | + ├── deploy.yaml # 部署相关的配置文件 |
| 205 | + ├── model.pdiparams # 静态图模型参数 |
| 206 | + ├── model.pdiparams.info # 参数额外信息,一般无需关注 |
| 207 | + └── model.pdmodel # 静态图模型文件 |
| 208 | +``` |
| 209 | + |
| 210 | +## Web端部署 |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | +参见[Web端部署教程](../../deploy/web) |
| 215 | + |
| 216 | +## 移动端部署 |
| 217 | + |
| 218 | +<img src="../../deploy/lite/example/human_1.png" width="20%" > <img src="../../deploy/lite/example/human_2.png" width="20%" > <img src="../../deploy/lite/example/human_3.png" width="20%" > |
| 219 | + |
| 220 | +参见[移动端部署教程](../../deploy/lite/) |
0 commit comments