|
@@ -1,28 +1,32 @@
|
|
|
# HumanSeg人像分割模型
|
|
# HumanSeg人像分割模型
|
|
|
|
|
|
|
|
-本教程基于PaddleX核心分割网络,提供针对人像分割场景从预训练模型、Fine-tune、视频分割预测部署的全流程应用指南。
|
|
|
|
|
|
|
+本教程基于PaddleX核心分割模型实现人像分割,开放预训练模型和测试数据、支持视频流人像分割、提供模型Fine-tune到Paddle-Lite移动端部署的全流程应用指南。
|
|
|
|
|
|
|
|
-## 安装
|
|
|
|
|
|
|
+## 目录
|
|
|
|
|
|
|
|
-**前置依赖**
|
|
|
|
|
-* paddlepaddle >= 1.8.0
|
|
|
|
|
-* python >= 3.5
|
|
|
|
|
|
|
+* [预训练模型和测试数据](#1)
|
|
|
|
|
+* [快速体验视频流人像分割](#2)
|
|
|
|
|
+* [模型Fine-tune](#3)
|
|
|
|
|
+* [Paddle-Lite移动端部署](#4)
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
-pip install paddlex -i https://mirror.baidu.com/pypi/simple
|
|
|
|
|
-```
|
|
|
|
|
-安装的相关问题参考[PaddleX安装](https://paddlex.readthedocs.io/zh_CN/latest/install.html)
|
|
|
|
|
|
|
|
|
|
-## 预训练模型
|
|
|
|
|
-HumanSeg开放了在大规模人像数据上训练的两个预训练模型,满足多种使用场景的需求
|
|
|
|
|
|
|
+## <h2 id="1">预训练模型和测试数据</h2>
|
|
|
|
|
+
|
|
|
|
|
+#### 预训练模型
|
|
|
|
|
+
|
|
|
|
|
+本案例开放了两个在大规模人像数据集上训练好的模型,以满足服务器端场景和移动端场景的需求。使用这些模型可以快速体验视频流人像分割,也可以部署到移动端进行实时人像分割,也可以用于完成模型Fine-tuning。
|
|
|
|
|
|
|
|
| 模型类型 | Checkpoint Parameter | Inference Model | Quant Inference Model | 备注 |
|
|
| 模型类型 | Checkpoint Parameter | Inference Model | Quant Inference Model | 备注 |
|
|
|
| --- | --- | --- | ---| --- |
|
|
| --- | --- | --- | ---| --- |
|
|
|
| HumanSeg-server | [humanseg_server_params](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_server.pdparams) | [humanseg_server_inference](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_server_inference.zip) | -- | 高精度模型,适用于服务端GPU且背景复杂的人像场景, 模型结构为Deeplabv3+/Xcetion65, 输入大小(512, 512) |
|
|
| HumanSeg-server | [humanseg_server_params](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_server.pdparams) | [humanseg_server_inference](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_server_inference.zip) | -- | 高精度模型,适用于服务端GPU且背景复杂的人像场景, 模型结构为Deeplabv3+/Xcetion65, 输入大小(512, 512) |
|
|
|
-| HumanSeg-mobile | [humanseg_mobile_params](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile.pdparams) | [humanseg_mobile_inference](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile_inference.zip) | [humanseg_mobile_quant](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile_quant.zip) | 轻量级模型, 适用于移动端或服务端CPU的前置摄像头场景,模型结构为HRNet_w18_samll_v1,输入大小(192, 192) |
|
|
|
|
|
|
|
+| HumanSeg-mobile | [humanseg_mobile_params](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile.pdparams) | [humanseg_mobile_inference](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile_inference.zip) | [humanseg_mobile_quant](https://paddlex.bj.bcebos.com/humanseg/models/humanseg_mobile_quant.zip) | 轻量级模型, 适用于移动端或服务端CPU的前置摄像头场景,模型结构为HRNet_w18_small_v1,输入大小(192, 192) |
|
|
|
|
|
+
|
|
|
|
|
+> * Checkpoint Parameter为模型权重,用于Fine-tuning场景。
|
|
|
|
|
+> * Inference Model和Quant Inference Model为预测部署模型,包含`__model__`计算图结构、`__params__`模型参数和`model.yaml`基础的模型配置信息。
|
|
|
|
|
+> * 其中Inference Model适用于服务端的CPU和GPU预测部署,Qunat Inference Model为量化版本,适用于通过Paddle Lite进行移动端等端侧设备部署。
|
|
|
|
|
|
|
|
|
|
|
|
|
-模型性能
|
|
|
|
|
|
|
+预训练模型的存储大小和推理时长如下所示,其中移动端模型的运行环境为cpu:骁龙855,内存:6GB,图片大小:192*192
|
|
|
|
|
|
|
|
| 模型 | 模型大小 | 计算耗时 |
|
|
| 模型 | 模型大小 | 计算耗时 |
|
|
|
| --- | --- | --- |
|
|
| --- | --- | --- |
|
|
@@ -30,68 +34,91 @@ HumanSeg开放了在大规模人像数据上训练的两个预训练模型,满
|
|
|
|humanseg_mobile_inference | 5.8 M | 42.35ms |
|
|
|humanseg_mobile_inference | 5.8 M | 42.35ms |
|
|
|
|humanseg_mobile_quant | 1.6M | 24.93ms |
|
|
|humanseg_mobile_quant | 1.6M | 24.93ms |
|
|
|
|
|
|
|
|
-计算耗时运行环境: 小米,cpu:骁龙855, 内存:6GB, 图片大小:192*192
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-**NOTE:**
|
|
|
|
|
-其中Checkpoint Parameter为模型权重,用于Fine-tuning场景。
|
|
|
|
|
-
|
|
|
|
|
-* Inference Model和Quant Inference Model为预测部署模型,包含`__model__`计算图结构、`__params__`模型参数和`model.yaml`基础的模型配置信息。
|
|
|
|
|
-
|
|
|
|
|
-* 其中Inference Model适用于服务端的CPU和GPU预测部署,Qunat Inference Model为量化版本,适用于通过Paddle Lite进行移动端等端侧设备部署。
|
|
|
|
|
-
|
|
|
|
|
-执行以下脚本进行HumanSeg预训练模型的下载
|
|
|
|
|
|
|
+执行以下脚本下载全部的预训练模型:
|
|
|
```bash
|
|
```bash
|
|
|
python pretrain_weights/download_pretrain_weights.py
|
|
python pretrain_weights/download_pretrain_weights.py
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 下载测试数据
|
|
|
|
|
-我们提供了[supervise.ly](https://supervise.ly/)发布人像分割数据集**Supervisely Persons**, 从中随机抽取一小部分并转化成PaddleX可直接加载数据格式。通过运行以下代码进行快速下载,其中包含手机前置摄像头的人像测试视频`video_test.mp4`.
|
|
|
|
|
|
|
+#### 测试数据
|
|
|
|
|
+
|
|
|
|
|
+[supervise.ly](https://supervise.ly/)发布了人像分割数据集**Supervisely Persons**, 本案例从中随机抽取一小部分数据并转化成PaddleX可直接加载的数据格式,运行以下代码可下载该数据、以及手机前置摄像头拍摄的人像测试视频`video_test.mp4`.
|
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
python data/download_data.py
|
|
python data/download_data.py
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 快速体验视频流人像分割
|
|
|
|
|
-结合DIS(Dense Inverse Search-basedmethod)光流算法预测结果与分割结果,改善视频流人像分割
|
|
|
|
|
|
|
+## <h2 id="2">快速体验视频流人像分割</h2>
|
|
|
|
|
+
|
|
|
|
|
+#### 前置依赖
|
|
|
|
|
+
|
|
|
|
|
+* PaddlePaddle >= 1.8.0
|
|
|
|
|
+* Python >= 3.5
|
|
|
|
|
+* PaddleX >= 1.0.0
|
|
|
|
|
+
|
|
|
|
|
+安装的相关问题参考[PaddleX安装](../../docs/install.md)
|
|
|
|
|
+
|
|
|
|
|
+### 光流跟踪辅助的视频流人像分割
|
|
|
|
|
+
|
|
|
|
|
+本案例将DIS(Dense Inverse Search-basedmethod)光流跟踪算法的预测结果与PaddleX的分割结果进行融合,以此改善视频流人像分割的效果。运行以下代码进行体验:
|
|
|
|
|
+
|
|
|
|
|
+* 通过电脑摄像头进行实时分割处理
|
|
|
|
|
+
|
|
|
```bash
|
|
```bash
|
|
|
-# 通过电脑摄像头进行实时分割处理
|
|
|
|
|
python video_infer.py --model_dir pretrain_weights/humanseg_mobile_inference
|
|
python video_infer.py --model_dir pretrain_weights/humanseg_mobile_inference
|
|
|
|
|
+```
|
|
|
|
|
+* 对离线人像视频进行分割处理
|
|
|
|
|
|
|
|
-# 对人像视频进行分割处理
|
|
|
|
|
|
|
+```bash
|
|
|
python video_infer.py --model_dir pretrain_weights/humanseg_mobile_inference --video_path data/video_test.mp4
|
|
python video_infer.py --model_dir pretrain_weights/humanseg_mobile_inference --video_path data/video_test.mp4
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-视频分割结果如下:
|
|
|
|
|
|
|
+视频分割结果如下所示:
|
|
|
|
|
|
|
|
<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%">
|
|
<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%">
|
|
|
|
|
|
|
|
-根据所选背景进行背景替换,背景可以是一张图片,也可以是一段视频。
|
|
|
|
|
|
|
+### 人像背景替换
|
|
|
|
|
+
|
|
|
|
|
+本案例还实现了人像背景替换功能,根据所选背景对人像的背景画面进行替换,背景可以是一张图片,也可以是一段视频。
|
|
|
|
|
+
|
|
|
|
|
+* 通过电脑摄像头进行实时背景替换处理, 通过'--background_video_path'传入背景视频
|
|
|
```bash
|
|
```bash
|
|
|
-# 通过电脑摄像头进行实时背景替换处理, 也可通过'--background_video_path'传入背景视频
|
|
|
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --background_image_path data/background.jpg
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --background_image_path data/background.jpg
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
-# 对人像视频进行背景替换处理, 也可通过'--background_video_path'传入背景视频
|
|
|
|
|
|
|
+* 对人像视频进行背景替换处理, 通过'--background_video_path'传入背景视频
|
|
|
|
|
+```bash
|
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --video_path data/video_test.mp4 --background_image_path data/background.jpg
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --video_path data/video_test.mp4 --background_image_path data/background.jpg
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
-# 对单张图像进行背景替换
|
|
|
|
|
|
|
+* 对单张图像进行背景替换
|
|
|
|
|
+```bash
|
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --image_path data/human_image.jpg --background_image_path data/background.jpg
|
|
python bg_replace.py --model_dir pretrain_weights/humanseg_mobile_inference --image_path data/human_image.jpg --background_image_path data/background.jpg
|
|
|
-
|
|
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
背景替换结果如下:
|
|
背景替换结果如下:
|
|
|
|
|
|
|
|
<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%">
|
|
<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%">
|
|
|
|
|
|
|
|
|
|
+**注意**:
|
|
|
|
|
+
|
|
|
|
|
+* 视频分割处理时间需要几分钟,请耐心等待。
|
|
|
|
|
+
|
|
|
|
|
+* 提供的模型适用于手机摄像头竖屏拍摄场景,宽屏效果会略差一些。
|
|
|
|
|
|
|
|
-**NOTE**:
|
|
|
|
|
|
|
+## <h2 id="3">模型Fine-tune</h2>
|
|
|
|
|
|
|
|
-视频分割处理时间需要几分钟,请耐心等待。
|
|
|
|
|
|
|
+#### 前置依赖
|
|
|
|
|
|
|
|
-提供的模型适用于手机摄像头竖屏拍摄场景,宽屏效果会略差一些。
|
|
|
|
|
|
|
+* PaddlePaddle >= 1.8.0
|
|
|
|
|
+* Python >= 3.5
|
|
|
|
|
+* PaddleX >= 1.0.0
|
|
|
|
|
+
|
|
|
|
|
+安装的相关问题参考[PaddleX安装](../../docs/install.md)
|
|
|
|
|
+
|
|
|
|
|
+### 模型训练
|
|
|
|
|
+
|
|
|
|
|
+使用下述命令进行基于预训练模型的模型训练,请确保选用的模型结构`model_type`与模型参数`pretrain_weights`匹配。如果不需要本案例提供的测试数据,可更换数据、选择合适的模型并调整训练参数。
|
|
|
|
|
|
|
|
-## 训练
|
|
|
|
|
-使用下述命令基于与训练模型进行Fine-tuning,请确保选用的模型结构`model_type`与模型参数`pretrain_weights`匹配。
|
|
|
|
|
```bash
|
|
```bash
|
|
|
# 指定GPU卡号(以0号卡为例)
|
|
# 指定GPU卡号(以0号卡为例)
|
|
|
export CUDA_VISIBLE_DEVICES=0
|
|
export CUDA_VISIBLE_DEVICES=0
|
|
@@ -124,11 +151,12 @@ python train.py --model_type HumanSegMobile \
|
|
|
```bash
|
|
```bash
|
|
|
python train.py --help
|
|
python train.py --help
|
|
|
```
|
|
```
|
|
|
-**NOTE**
|
|
|
|
|
-可通过更换`--model_type`变量与对应的`--pretrain_weights`使用不同的模型快速尝试。
|
|
|
|
|
|
|
+**注意**:可以通过更换`--model_type`变量与对应的`--pretrain_weights`使用不同的模型快速尝试。
|
|
|
|
|
+
|
|
|
|
|
+### 评估
|
|
|
|
|
+
|
|
|
|
|
+使用下述命令对模型在验证集上的精度进行评估:
|
|
|
|
|
|
|
|
-## 评估
|
|
|
|
|
-使用下述命令进行评估
|
|
|
|
|
```bash
|
|
```bash
|
|
|
python eval.py --model_dir output/best_model \
|
|
python eval.py --model_dir output/best_model \
|
|
|
--data_dir data/mini_supervisely \
|
|
--data_dir data/mini_supervisely \
|
|
@@ -141,8 +169,9 @@ python eval.py --model_dir output/best_model \
|
|
|
* `--val_list`: 验证集列表路径
|
|
* `--val_list`: 验证集列表路径
|
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
|
|
|
|
|
|
-## 预测
|
|
|
|
|
-使用下述命令进行预测, 预测结果默认保存在`./output/result/`文件夹中。
|
|
|
|
|
|
|
+### 预测
|
|
|
|
|
+
|
|
|
|
|
+使用下述命令对测试集进行预测,预测可视化结果默认保存在`./output/result/`文件夹中。
|
|
|
```bash
|
|
```bash
|
|
|
python infer.py --model_dir output/best_model \
|
|
python infer.py --model_dir output/best_model \
|
|
|
--data_dir data/mini_supervisely \
|
|
--data_dir data/mini_supervisely \
|
|
@@ -156,7 +185,10 @@ python infer.py --model_dir output/best_model \
|
|
|
* `--test_list`: 测试集列表路径
|
|
* `--test_list`: 测试集列表路径
|
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
|
|
|
|
|
|
-## 模型导出
|
|
|
|
|
|
|
+### 模型导出
|
|
|
|
|
+
|
|
|
|
|
+在服务端部署的模型需要首先将模型导出为inference格式模型,导出的模型将包括`__model__`、`__params__`和`model.yml`三个文名,分别为模型的网络结构,模型权重和模型的配置文件(包括数据预处理参数等等)。在安装完PaddleX后,在命令行终端使用如下命令完成模型导出:
|
|
|
|
|
+
|
|
|
```bash
|
|
```bash
|
|
|
paddlex --export_inference --model_dir output/best_model \
|
|
paddlex --export_inference --model_dir output/best_model \
|
|
|
--save_dir output/export
|
|
--save_dir output/export
|
|
@@ -165,7 +197,7 @@ paddlex --export_inference --model_dir output/best_model \
|
|
|
* `--model_dir`: 模型路径
|
|
* `--model_dir`: 模型路径
|
|
|
* `--save_dir`: 导出模型保存路径
|
|
* `--save_dir`: 导出模型保存路径
|
|
|
|
|
|
|
|
-## 离线量化
|
|
|
|
|
|
|
+### 离线量化
|
|
|
```bash
|
|
```bash
|
|
|
python quant_offline.py --model_dir output/best_model \
|
|
python quant_offline.py --model_dir output/best_model \
|
|
|
--data_dir data/mini_supervisely \
|
|
--data_dir data/mini_supervisely \
|
|
@@ -179,3 +211,5 @@ python quant_offline.py --model_dir output/best_model \
|
|
|
* `--quant_list`: 量化数据集列表路径,一般直接选择训练集或验证集
|
|
* `--quant_list`: 量化数据集列表路径,一般直接选择训练集或验证集
|
|
|
* `--save_dir`: 量化模型保存路径
|
|
* `--save_dir`: 量化模型保存路径
|
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
* `--image_shape`: 网络输入图像大小(w, h)
|
|
|
|
|
+
|
|
|
|
|
+## <h2 id="4">Paddle-Lite移动端部署</h2>
|