Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support keypoint detection #2440

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/module_usage/tutorials/cv_modules/human_detection.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,11 @@ Similar to model training and evaluation, the following steps are required:
Other related parameters can be set by modifying the fields under `Global` and `Predict` in the `.yaml` configuration file. For details, please refer to [PaddleX Common Model Configuration File Parameter Description](../../instructions/config_parameters_common.en.md).

#### 4.4.2 Model Integration
The weights you produce can be directly integrated into the human detection module. You can refer to the Python sample code in [Quick Integration](#iii-quick-integration) and simply replace the model with the path to your trained model.

1. **Pipeline Integration**

The pedestrian detection module can be integrated into PaddleX pipelines such as [**Human Keypoint Detection**](../../../pipeline_usage/tutorials/cv_pipelines/human_keypoint_detection_en.md). You can update the keypoint detection module in the production line simply by replacing the model path. In production line integration, you can deploy your models using high-performance deployment and service deployment methods.

2. **Module Integration**

The weights you produce can be directly integrated into the pedestrian detection module. You can refer to the Python example code in [Quick Integration](#iii-quick-integration). Simply replace the model with the path to your trained model to complete the integration.
9 changes: 9 additions & 0 deletions docs/module_usage/tutorials/cv_modules/human_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,13 @@ python main.py -c paddlex/configs/human_detection/PP-YOLOE-S_human.yaml \
其他相关参数均可通过修改`.yaml`配置文件中的`Global`和`Predict`下的字段来进行设置,详细请参考[PaddleX通用模型配置文件参数说明](../../instructions/config_parameters_common.md)。

#### 4.4.2 模型集成

模型可以直接集成到 PaddleX 产线中,也可以直接集成到您自己的项目中。

1.**产线集成**

行人检测模块可以集成的PaddleX产线有[**人体关键点检测**](../../../pipeline_usage/tutorials/cv_pipelines/human_keypoint_detection.md),只需要替换模型路径即可完成相关产线的关键点检测模块的模型更新。在产线集成中,你可以使用高性能部署和服务化部署来部署你得到的模型。

2.**模块集成**

您产出的权重可以直接集成到行人检测模块中,可以参考[快速集成](#三快速集成)的 Python 示例代码,只需要将模型替换为你训练的到的模型路径即可。
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[简体中文](keypoint_detection.md) | English

Coming soon...
283 changes: 283 additions & 0 deletions docs/module_usage/tutorials/cv_modules/keypoint_detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
简体中文 | [English](keypoint_detection_en.md)

# 关键点检测模块使用教程

## 一、概述
关键点检测是计算机视觉领域中的一个重要任务,旨在识别图像或视频中物体(如人体、面部、手势等)的特定关键点位置。通过检测这些关键点,可以实现对物体的姿态估计、动作识别、人机交互、动画生成等多种应用。关键点检测在增强现实、虚拟现实、运动捕捉等领域都有广泛的应用。

关键点检测算法主要包括 Top-Down 和 Bottom-Up 两种方案。Top-Down 方案通常依赖一个目标检测算法识别出感兴趣物体的边界框,关键点检测模型的输入为经过裁剪的单个目标,输出为这个目标的关键点预测结果,模型的准确率会更高,但速度会随着对象数量的增加而变慢。不同的是,Bottom-Up 方法不依赖于先进行目标检测,而是直接对整个图像进行关键点检测,然后对这些点进行分组或连接以形成多个姿势实例,其速度是固定的,不会随着物体数量的增加而变慢,但精度会更低。

## 二、支持模型列表

<table>
<tr>
<th >模型</th>
<th >方案</th>
<th >输入尺寸</th>
<th >AP(0.5:0.95)</th>
<th >GPU推理耗时(ms)</th>
<th >CPU推理耗时 (ms)</th>
<th >模型存储大小(M)</th>
<th >介绍</th>
</tr>
<tr>
<td>PP-TinyPose_128x96</td>
<td>Top-Down</td>
<td>128*96</td>
<td>58.4</td>
<td></td>
<td></td>
<td>4.9</td>
<td rowspan="2">PP-TinyPose 是百度飞桨视觉团队自研的针对移动端设备优化的实时关键点检测模型,可流畅地在移动端设备上执行多人姿态估计任务</td>
</tr>
<tr>
<td>PP-TinyPose_256x192</td>
<td>Top-Down</td>
<td>128*96</td>
<td>68.3</td>
<td></td>
<td></td>
<td>4.9</td>
</tr>
</table>

**注:以上精度指标为COCO数据集 AP(0.5:0.95),所依赖的检测框为ground truth标注得到。所有模型 GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为8,精度类型为 FP32。**


## 三、快速集成
> ❗ 在快速集成前,请先安装 PaddleX 的 wheel 包,详细请参考 [PaddleX本地安装教程](../../../installation/installation.md)

完成wheel包的安装后,几行代码即可完成关键点检测模块的推理,可以任意切换该模块下的模型,您也可以将关键点检测模块中的模型推理集成到您的项目中。运行以下代码前,请您下载[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/keypoint_detection_002.jpg)到本地。

```python
from paddlex import create_model

model_name = "PP-TinyPose_128x96"

model = create_model(model_name)
output = model.predict("keypoint_detection_002.jpg", batch_size=1)

for res in output:
res.print(json_format=False)
res.save_to_img("./output/")
res.save_to_json("./output/res.json")

```

关于更多 PaddleX 的单模型推理的 API 的使用方法,可以参考[PaddleX单模型Python脚本使用说明](../../instructions/model_python_API.md)。


## 四、二次开发
如果你追求更高精度的现有模型,可以使用PaddleX的二次开发能力,开发更好的关键点检测模型。在使用PaddleX开发关键点检测模型之前,请务必安装PaddleX的PaddleDetection插件,安装过程可以参考 [PaddleX本地安装教程](../../../installation/installation.md)。

### 4.1 数据准备
在进行模型训练前,需要准备相应任务模块的数据集。PaddleX 针对每一个模块提供了数据校验功能,**只有通过数据校验的数据才可以进行模型训练**。此外,PaddleX为每一个模块都提供了demo数据集,您可以基于官方提供的 Demo 数据完成后续的开发。若您希望用私有数据集进行后续的模型训练,可以参考[PaddleX关键点检测任务模块数据标注教程](../../../data_annotations/cv_modules/keypoint_detection.md)。

#### 4.1.1 Demo 数据下载
您可以参考下面的命令将 Demo 数据集下载到指定文件夹:

```bash
cd /path/to/paddlex
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/keypoint_coco_examples.tar -P ./dataset
tar -xf ./dataset/keypoint_coco_examples.tar -C ./dataset/
```
#### 4.1.2 数据校验
一行命令即可完成数据校验:

```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/keypoint_coco_examples
```
执行上述命令后,PaddleX 会对数据集进行校验,并统计数据集的基本信息,命令运行成功后会在log中打印出`Check dataset passed !`信息。校验结果文件保存在`./output/check_dataset_result.json`,同时相关产出会保存在当前目录的`./output/check_dataset`目录下,产出目录中包括可视化的示例样本图片和样本分布直方图。

<details>
<summary>👉 <b>校验结果详情(点击展开)</b></summary>


校验结果文件具体内容为:

```bash
{
"done_flag": true,
"check_pass": true,
"attributes": {
"num_classes": 1,
"train_samples": 500,
"train_sample_paths": [
"check_dataset/demo_img/000000560108.jpg",
"check_dataset/demo_img/000000434662.jpg",
"check_dataset/demo_img/000000540556.jpg",
...
],
"val_samples": 100,
"val_sample_paths": [
"check_dataset/demo_img/000000463730.jpg",
"check_dataset/demo_img/000000085329.jpg",
"check_dataset/demo_img/000000459153.jpg",
...
]
},
"analysis": {
"histogram": "check_dataset/histogram.png"
},
"dataset_path": "keypoint_coco_examples",
"show_type": "image",
"dataset_type": "KeypointTopDownCocoDetDataset"
}
```
上述校验结果中,`check_pass` 为 `True` 表示数据集格式符合要求,其他部分指标的说明如下:

* `attributes.num_classes`:该数据集类别数为 1;
* `attributes.train_samples`:该数据集训练集样本数量为500;
* `attributes.val_samples`:该数据集验证集样本数量为 100;
* `attributes.train_sample_paths`:该数据集训练集样本可视化图片相对路径列表;
* `attributes.val_sample_paths`:该数据集验证集样本可视化图片相对路径列表;


数据集校验还对数据集中所有类别的样本数量分布情况进行了分析,并绘制了分布直方图(histogram.png):

![](https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/modules/keypoint_det/01.png)
</details>

#### 4.1.3 数据集格式转换/数据集划分(可选)
在您完成数据校验之后,可以通过**修改配置文件**或是**追加超参数**的方式对数据集的格式进行转换,也可以对数据集的训练/验证比例进行重新划分。

<details>
<summary>👉 <b>格式转换/数据集划分详情(点击展开)</b></summary>



**(1)数据集格式转换**

关键点检测不支持数据格式转换。

**(2)数据集划分**

数据集划分的参数可以通过修改配置文件中 `CheckDataset` 下的字段进行设置,配置文件中部分参数的示例说明如下:

* `CheckDataset`:
* `split`:
* `enable`: 是否进行重新划分数据集,为 `True` 时进行数据集格式转换,默认为 `False`;
* `train_percent`: 如果重新划分数据集,则需要设置训练集的百分比,类型为0-100之间的任意整数,需要保证与 `val_percent` 的值之和为100;


例如,您想重新划分数据集为 训练集占比90%、验证集占比10%,则需将配置文件修改为:

```bash
......
CheckDataset:
......
split:
enable: True
train_percent: 90
val_percent: 10
......
```
随后执行命令:

```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/keypoint_coco_examples
```
数据划分执行之后,原有标注文件会被在原路径下重命名为 `xxx.bak`。

以上参数同样支持通过追加命令行参数的方式进行设置:

```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/keypoint_coco_examples \
-o CheckDataset.split.enable=True \
-o CheckDataset.split.train_percent=90 \
-o CheckDataset.split.val_percent=10
```
</details>



### 4.2 模型训练
一条命令即可完成模型的训练,以此处`PP-TinyPose_128x96`的训练为例:

```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=train \
-o Global.dataset_dir=./dataset/keypoint_coco_examples
```
需要如下几步:

* 指定模型的`.yaml` 配置文件路径(此处为`PP-TinyPose_128x96.yaml`,训练其他模型时,需要的指定相应的配置文件,模型和配置的文件的对应关系,可以查阅[PaddleX模型列表(CPU/GPU)](../../../support_list/models_list.md))
* 指定模式为模型训练:`-o Global.mode=train`
* 指定训练数据集路径:`-o Global.dataset_dir`
其他相关参数均可通过修改`.yaml`配置文件中的`Global`和`Train`下的字段来进行设置,也可以通过在命令行中追加参数来进行调整。如指定前 2 卡 gpu 训练:`-o Global.device=gpu:0,1`;设置训练轮次数为 10:`-o Train.epochs_iters=10`。更多可修改的参数及其详细解释,可以查阅模型对应任务模块的配置文件说明[PaddleX通用模型配置文件参数说明](../../instructions/config_parameters_common.md)。

<details>
<summary>👉 <b>更多说明(点击展开)</b></summary>


* 模型训练过程中,PaddleX 会自动保存模型权重文件,默认为`output`,如需指定保存路径,可通过配置文件中 `-o Global.output` 字段进行设置。
* PaddleX 对您屏蔽了动态图权重和静态图权重的概念。在模型训练的过程中,会同时产出动态图和静态图的权重,在模型推理时,默认选择静态图权重推理。
* 在完成模型训练后,所有产出保存在指定的输出目录(默认为`./output/`)下,通常有以下产出:

* `train_result.json`:训练结果记录文件,记录了训练任务是否正常完成,以及产出的权重指标、相关文件路径等;
* `train.log`:训练日志文件,记录了训练过程中的模型指标变化、loss 变化等;
* `config.yaml`:训练配置文件,记录了本次训练的超参数的配置;
* `.pdparams`、`.pdema`、`.pdopt.pdstate`、`.pdiparams`、`.pdmodel`:模型权重相关文件,包括网络参数、优化器、EMA、静态图网络参数、静态图网络结构等;
</details>

## **4.3 模型评估**
在完成模型训练后,可以对指定的模型权重文件在验证集上进行评估,验证模型精度。使用 PaddleX 进行模型评估,一条命令即可完成模型的评估:

```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=evaluate \
-o Global.dataset_dir=./dataset/keypoint_coco_examples
```
与模型训练类似,需要如下几步:

* 指定模型的`.yaml` 配置文件路径(此处为`PP-TinyPose_128x96.yaml`)
* 指定模式为模型评估:`-o Global.mode=evaluate`
* 指定验证数据集路径:`-o Global.dataset_dir`
其他相关参数均可通过修改`.yaml`配置文件中的`Global`和`Evaluate`下的字段来进行设置,详细请参考[PaddleX通用模型配置文件参数说明](../../instructions/config_parameters_common.md)。

<details>
<summary>👉 <b>更多说明(点击展开)</b></summary>


在模型评估时,需要指定模型权重文件路径,每个配置文件中都内置了默认的权重保存路径,如需要改变,只需要通过追加命令行参数的形式进行设置即可,如`-o Evaluate.weight_path=./output/best_model/best_model/model.pdparams`。

在完成模型评估后,会产出`evaluate_result.json,其记录了`评估的结果,具体来说,记录了评估任务是否正常完成,以及模型的评估指标,包含 AP;

</details>

### **4.4 模型推理**
在完成模型的训练和评估后,即可使用训练好的模型权重进行推理预测。在PaddleX中实现模型推理预测可以通过两种方式:命令行和wheel 包。

#### 4.4.1 模型推理
* 通过命令行的方式进行推理预测,只需如下一条命令。运行以下代码前,请您下载[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/keypoint_detection_002.jpg)到本地。
```bash
python main.py -c paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml \
-o Global.mode=predict \
-o Predict.model_dir="./output/best_model/inference" \
-o Predict.input="keypoint_detection_002.jpg"
```
与模型训练和评估类似,需要如下几步:

* 指定模型的`.yaml` 配置文件路径(此处为`PP-TinyPose_128x96.yaml`)
* 指定模式为模型推理预测:`-o Global.mode=predict`
* 指定模型权重路径:`-o Predict.model_dir="./output/best_model/inference"`
* 指定输入数据路径:`-o Predict.input="..."`
其他相关参数均可通过修改`.yaml`配置文件中的`Global`和`Predict`下的字段来进行设置,详细请参考[PaddleX通用模型配置文件参数说明](../../instructions/config_parameters_common.md)。

#### 4.4.2 模型集成

模型可以直接集成到 PaddleX 产线中,也可以直接集成到您自己的项目中。

1.**产线集成**

关键点检测模块可以集成的PaddleX产线有[**人体关键点检测**](../../../pipeline_usage/tutorials/cv_pipelines/human_keypoint_detection.md),只需要替换模型路径即可完成相关产线的关键点检测模块的模型更新。在产线集成中,你可以使用高性能部署和服务化部署来部署你得到的模型。

2.**模块集成**

您产出的权重可以直接集成到关键点检测模块中,可以参考[快速集成](#三快速集成)的 Python 示例代码,只需要将模型替换为你训练的到的模型路径即可。
9 changes: 9 additions & 0 deletions docs/support_list/models_list.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,15 @@ PaddleX incorporates multiple pipelines, each containing several modules, and ea
</table>
<b>Note: The above accuracy metrics are evaluated on the </b>[MVTec AD](https://www.mvtec.com/company/research/datasets/mvtec-ad)<b> dataset using the average anomaly score.</b>

## [Keypoint Detection Module](../module_usage/tutorials//cv_modules/keypoint_detection_en.md)

| Model Name|Method|Input Size|AP(0.5:0.95)|GPU Inference Time (ms)|CPU Inference Time (ms)|Model Size|YAML File|
|-|-|-|-|-|-|-|-|
| PP-TinyPose_128x96 | Top-Down| 128*96 | 58.4 | | | 4.9 M | [PP-TinyPose_128x96.yaml](../../paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml) |
| PP-TinyPose_256x192 | Top-Down| 128*96 | 68.3 | | | 4.9 M | [PP-TinyPose_128x96.yaml](../../paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml) |

**Note: The above accuracy metrics are based on the COCO dataset with AP(0.5:0.95), and the detection boxes are obtained from ground truth annotations. All model GPU inference times are measured on machines with NVIDIA Tesla T4 GPUs, using FP32 precision. The CPU inference speeds are based on an Intel® Xeon® Gold 5117 CPU @ 2.00GHz, with 8 threads and FP32 precision.**

## [Semantic Segmentation Module](../module_usage/tutorials/cv_modules/semantic_segmentation.en.md)
<table>
<thead>
Expand Down
9 changes: 9 additions & 0 deletions docs/support_list/models_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,15 @@ PaddleX 内置了多条产线,每条产线都包含了若干模块,每个模
</table>
<b>注:以上精度指标为 </b>[MVTec AD](https://www.mvtec.com/company/research/datasets/mvtec-ad)<b> 验证集 平均异常分数。</b>

## [关键点检测模块](../module_usage/tutorials//cv_modules/keypoint_detection.md)

| 模型|方案|输入尺寸|AP(0.5:0.95)|GPU推理耗时(ms|CPU推理耗时 (ms)|模型存储大小|yaml文件|
|-|-|-|-|-|-|-|-|
| PP-TinyPose_128x96 | Top-Down| 128*96 | 58.4 | | | 4.9 M | [PP-TinyPose_128x96.yaml](../../paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml) |
| PP-TinyPose_256x192 | Top-Down| 128*96 | 68.3 | | | 4.9 M | [PP-TinyPose_128x96.yaml](../../paddlex/configs/keypoint_detection/PP-TinyPose_128x96.yaml) |

**注:以上精度指标为COCO数据集 AP(0.5:0.95),所依赖的检测框为ground truth标注得到。所有模型 GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为8,精度类型为 FP32。**

## [语义分割模块](../module_usage/tutorials/cv_modules/semantic_segmentation.md)
<table>
<thead>
Expand Down
Loading