Quellcode durchsuchen

add ad practise tuturial (#2465)

* add ad practise tuturial

* add ad practise tuturial

* add ad practise tuturial
Sunflower7788 vor 1 Jahr
Ursprung
Commit
9b684930bb

+ 266 - 0
docs/practical_tutorials/anomaly_detection_tutorial.md

@@ -0,0 +1,266 @@
+简体中文 | [English](anomaly_detection_tutorial_en.md)
+
+# PaddleX 3.0 图像异常检测产线———食品外观质检教程
+
+PaddleX 提供了丰富的模型产线,模型产线由一个或多个模型组合实现,每个模型产线都能够解决特定的场景任务问题。PaddleX 所提供的模型产线均支持快速体验,如果效果不及预期,也同样支持使用私有数据微调模型,并且 PaddleX 提供了 Python API,方便将产线集成到个人项目中。在使用之前,您首先需要安装 PaddleX, 安装方式请参考[ PaddleX 安装](../installation/installation.md)。此处以一个食品外观质检的任务为例子,介绍模型产线工具的使用流程。
+
+## 1. 选择产线
+
+首先,需要根据您的任务场景,选择对应的 PaddleX 产线,此处为食品外观质检,需要了解到这个任务属于异常检测任务,对应 PaddleX 的通用异常检测产线。如果无法确定任务和产线的对应关系,您可以在 PaddleX 支持的[模型产线列表](../support_list/pipelines_list.md)中了解相关产线的能力介绍。
+
+
+## 2. 快速体验
+
+PaddleX 提供了以下快速体验的方式,可以直接通过 PaddleX wheel 包在本地体验。
+
+  - 本地体验方式:
+    ```bash
+    paddlex --pipeline anomaly_detection \
+        --input https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png \
+        --save_path output
+    ```
+
+
+  快速体验产出推理结果示例:
+  <center>
+
+  <img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/image_anomaly_detection/01.png" width=600>
+
+  </center>
+
+当体验完该产线之后,需要确定产线是否符合预期(包含精度、速度等),产线包含的模型是否需要继续微调,如果模型的速度或者精度不符合预期,则需要根据模型选择选择可替换的模型继续测试,确定效果是否满意。如果最终效果均不满意,则需要微调模型。本教程希望产出检测出食品榛子外观异常的模型,显然默认的权重(铁丝网 数据集训练产出的权重)无法满足要求,需要采集和标注数据,然后进行训练微调。
+
+## 3. 选择模型
+
+PaddleX 提供了 1 个端到端的高精度异常检测模型,具体可参考 [模型列表](../support_list/models_list.md),其中部分模型的benchmark如下:
+
+|模型名称|Avg(%)|GPU推理耗时(ms)|CPU推理耗时(ms)|模型存储大小|yaml 文件|
+|-|-|-|-|-|-|
+|STFPM|96.2|-|-|21.5 M|[STFPM.yaml](../../paddlex/configs/anomaly_detection/STFPM.yaml)|
+
+**注:以上精度指标为 **[MVTec AD](https://www.mvtec.com/company/research/datasets/mvtec-ad)** 验证集 平均异常分数。**
+
+
+## 4. 数据准备和校验
+### 4.1 数据准备
+
+本教程采用 `食品外观质检数据集` 作为示例数据集,可通过以下命令获取示例数据集。如果您使用自备的已标注数据集,需要按照 PaddleX 的格式要求对自备数据集进行调整,以满足 PaddleX 的数据格式要求。关于数据格式介绍,您可以参考 [PaddleX 分割任务模块数据标注教程](../data_annotations/cv_modules/semantic_segmentation.md)。
+
+数据集获取命令:
+```bash
+cd /path/to/paddlex
+wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/anomaly_detection_hazelnut.tar -P ./dataset
+tar -xf ./dataset/anomaly_detection_hazelnut.tar -C ./dataset/
+```
+
+### 4.2 数据集校验
+
+在对数据集校验时,只需一行命令:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=check_dataset \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut
+```
+
+执行上述命令后,PaddleX 会对数据集进行校验,并统计数据集的基本信息。命令运行成功后会在 log 中打印出 `Check dataset passed !` 信息,同时相关产出会保存在当前目录的 `./output/check_dataset` 目录下,产出目录中包括可视化的示例样本图片和样本分布直方图。校验结果文件保存在 `./output/check_dataset_result.json`,校验结果文件具体内容为
+```
+{
+  "done_flag": true,
+  "check_pass": true,
+  "attributes": {
+    "train_sample_paths": [
+      "check_dataset\/demo_img\/294.png",
+      "check_dataset\/demo_img\/260.png",
+      "check_dataset\/demo_img\/297.png",
+      "check_dataset\/demo_img\/170.png",
+      "check_dataset\/demo_img\/068.png",
+      "check_dataset\/demo_img\/212.png",
+      "check_dataset\/demo_img\/204.png",
+      "check_dataset\/demo_img\/233.png",
+      "check_dataset\/demo_img\/367.png",
+      "check_dataset\/demo_img\/383.png"
+    ],
+    "train_samples": 391,
+    "val_sample_paths": [
+      "check_dataset\/demo_img\/012.png",
+      "check_dataset\/demo_img\/017.png",
+      "check_dataset\/demo_img\/006.png",
+      "check_dataset\/demo_img\/013.png",
+      "check_dataset\/demo_img\/014.png",
+      "check_dataset\/demo_img\/010.png",
+      "check_dataset\/demo_img\/007.png",
+      "check_dataset\/demo_img\/001.png",
+      "check_dataset\/demo_img\/002.png",
+      "check_dataset\/demo_img\/009.png"
+    ],
+    "val_samples": 70,
+    "num_classes": 1
+  },
+  "analysis": {
+    "histogram": "check_dataset\/histogram.png"
+  },
+  "dataset_path": ".\/dataset\/hazelnut",
+  "show_type": "image",
+  "dataset_type": "SegDataset"
+}
+```
+上述校验结果中,check_pass 为 True 表示数据集格式符合要求,其他部分指标的说明如下:
+
+- attributes.num_classes:该数据集类别数为 1,此处类别数量为后续训练需要传入的类别数量;
+- attributes.train_samples:该数据集训练集样本数量为 391;
+- attributes.val_samples:该数据集验证集样本数量为 70;
+- attributes.train_sample_paths:该数据集训练集样本可视化图片相对路径列表;
+- attributes.val_sample_paths:该数据集验证集样本可视化图片相对路径列表;
+
+另外,数据集校验还对数据集中所有类别的样本数量分布情况进行了分析,并绘制了分布直方图(histogram.png):
+<center>
+
+<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/anomaly_detection/02.png" width=600>
+
+</center>
+
+**注**:只有通过数据校验的数据才可以训练和评估。
+
+
+### 4.3 数据集格式转换/数据集划分(非必选)
+
+如需对数据集格式进行转换或是重新划分数据集,可通过修改配置文件或是追加超参数的方式进行设置。
+
+数据集校验相关的参数可以通过修改配置文件中 `CheckDataset` 下的字段进行设置,配置文件中部分参数的示例说明如下:
+
+* `CheckDataset`:
+    * `convert`:
+        * `enable`: 是否进行数据集格式转换,为 `True` 时进行数据集格式转换,默认为 `False`;
+        * `src_dataset_type`: 如果进行数据集格式转换,则需设置源数据集格式,数据可选源格式为 `LabelMe` 和 `VOC`;
+    * `split`:
+        * `enable`: 是否进行重新划分数据集,为 `True` 时进行数据集格式转换,默认为 `False`;
+        * `train_percent`: 如果重新划分数据集,则需要设置训练集的百分比,类型为 0-100 之间的任意整数,需要保证和 `val_percent` 值加和为 100;
+        * `val_percent`: 如果重新划分数据集,则需要设置验证集的百分比,类型为 0-100 之间的任意整数,需要保证和 `train_percent` 值加和为 100;
+
+数据转换和数据划分支持同时开启,对于数据划分原有标注文件会被在原路径下重命名为 `xxx.bak`,以上参数同样支持通过追加命令行参数的方式进行设置,例如重新划分数据集并设置训练集与验证集比例:`-o CheckDataset.split.enable=True -o CheckDataset.split.train_percent=80 -o CheckDataset.split.val_percent=20`。
+
+## 5. 模型训练和评估
+### 5.1 模型训练
+
+在训练之前,请确保您已经对数据集进行了校验。完成 PaddleX 模型的训练,只需如下一条命令:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=train \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut \
+    -o Train.epochs_iters=4000
+```
+
+在 PaddleX 中模型训练支持:修改训练超参数、单机单卡/多卡训练等功能,只需修改配置文件或追加命令行参数。
+
+PaddleX 中每个模型都提供了模型开发的配置文件,用于设置相关参数。模型训练相关的参数可以通过修改配置文件中 `Train` 下的字段进行设置,配置文件中部分参数的示例说明如下:
+
+* `Global`:
+    * `mode`:模式,支持数据校验(`check_dataset`)、模型训练(`train`)、模型评估(`evaluate`);
+    * `device`:训练设备,可选`cpu`、`gpu`、`xpu`、`npu`、`mlu`,除 cpu 外,多卡训练可指定卡号,如:`gpu:0,1,2,3`;
+* `Train`:训练超参数设置;
+    * `epochs_iters`:训练迭代次数数设置;
+    * `learning_rate`:训练学习率设置;
+
+更多超参数介绍,请参考 [PaddleX 通用模型配置文件参数说明](../module_usage/instructions/config_parameters_common.md)。
+
+**注:**
+- 以上参数可以通过追加令行参数的形式进行设置,如指定模式为模型训练:`-o Global.mode=train`;指定前 2 卡 gpu 训练:`-o Global.device=gpu:0,1`;设置训练迭代次数为 5000:`-o Train.epochs_iters=5000`。
+- 模型训练过程中,PaddleX 会自动保存模型权重文件,默认为`output`,如需指定保存路径,可通过配置文件中 `-o Global.output` 字段
+- PaddleX 对您屏蔽了动态图权重和静态图权重的概念。在模型训练的过程中,会同时产出动态图和静态图的权重,在模型推理时,默认选择静态图权重推理。
+
+**训练产出解释:**
+
+在完成模型训练后,所有产出保存在指定的输出目录(默认为`./output/`)下,通常有以下产出:
+
+* train_result.json:训练结果记录文件,记录了训练任务是否正常完成,以及产出的权重指标、相关文件路径等;
+* train.log:训练日志文件,记录了训练过程中的模型指标变化、loss 变化等;
+* config.yaml:训练配置文件,记录了本次训练的超参数的配置;
+* .pdparams、.pdema、.pdopt.pdstate、.pdiparams、.pdmodel:模型权重相关文件,包括网络参数、优化器、EMA、静态图网络参数、静态图网络结构等;
+
+### 5.2 模型评估
+
+在完成模型训练后,可以对指定的模型权重文件在验证集上进行评估,验证模型精度。使用 PaddleX 进行模型评估,只需一行命令:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=evaluate \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut
+```
+
+与模型训练类似,模型评估支持修改配置文件或追加命令行参数的方式设置。
+
+**注:** 在模型评估时,需要指定模型权重文件路径,每个配置文件中都内置了默认的权重保存路径,如需要改变,只需要通过追加命令行参数的形式进行设置即可,如`-o Evaluate.weight_path=./output/best_model/model.pdparams`。
+
+### 5.3 模型调优
+
+在学习了模型训练和评估后,我们可以通过调整超参数来提升模型的精度。通过合理调整训练轮数,您可以控制模型的训练深度,避免过拟合或欠拟合;而学习率的设置则关乎模型收敛的速度和稳定性。因此,在优化模型性能时,务必审慎考虑这两个参数的取值,并根据实际情况进行灵活调整,以获得最佳的训练效果。
+
+推荐在调试参数时遵循控制变量法:
+
+1. 首先固定训练迭代次数为 4000,批大小为 1。
+2. 基于 STFPM 模型启动三个实验,学习率分别为:0.01,0.1,0.4。
+3. 可以发现实验三精度最高的配置为学习率为 0.4,在该训练超参数基础上,增加训练论次数到5000,可以看到达到更优的精度。
+
+学习率探寻实验结果:
+<center>
+
+| 实验  | 迭代次数 | 学习率 | batch\_size | 训练环境 | mIoU |
+|-------|------|--------|----------|-------|----------|
+| 实验一 | 4000 | 0\.01 | 1        | 4卡   | 0\.9646   |
+| 实验二 | 4000 | 0\.1 | 1        | 4卡   |0\.9707|
+| 实验三 | 4000 | 0\.4  | 1        | 4卡   | **0\.9797**   |
+
+</center>
+
+改变 epoch 实验结果:
+<center>
+
+| 实验                | 迭代次数 | 学习率 | batch\_size| 训练环境 | mIoU |
+|--------------------|---------|-------|------------|------|----------|
+| 实验三              | 4000    | 0\.4 | 1         | 4卡   | 0\.9797   |
+| 实验三增大训练迭代次数 | 5000   | 0\.4 | 1         | 4卡   | **0\.9826**|
+</center>
+
+**注:本教程为4卡教程,如果您只有1张GPU,可通过调整训练卡数完成本次实验,但最终指标未必和上述指标对齐,属正常情况。**
+
+## 6. 产线测试
+
+将产线中的模型替换为微调后的模型进行测试,如:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=predict \
+    -o Predict.model_dir="output/best_model/inference" \
+    -o Predict.input="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png"
+```
+
+通过上述可在`./output`下生成预测结果,其中`uad_hazelnut.png`的预测结果如下:
+<center>
+
+<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/anomaly_detection/03.png" width="600"/>
+
+</center>
+
+## 7. 开发集成/部署
+如果通用异常检测产线可以达到您对产线推理速度和精度的要求,您可以直接进行开发集成/部署。
+1. 直接将训练好的模型应用在您的 Python 项目中,可以参考如下示例代码,并将`paddlex/pipelines/anomaly_detection.yaml`配置文件中的`Pipeline.model`修改为自己的模型路径`output/best_model/inference`:
+```python
+from paddlex import create_pipeline
+pipeline = create_pipeline(pipeline="paddlex/pipelines/anomaly_detection.yaml")
+output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png")
+for res in output:
+    res.print() # 打印预测的结构化输出
+    res.save_to_img("./output/") # 保存结果可视化图像
+    res.save_to_json("./output/") # 保存预测的结构化输出
+```
+更多参数请参考 [异常检测产线使用教程](../pipeline_usage/tutorials/cv_pipelines/image_anomaly_detection.md)。
+
+2. 此外,PaddleX 也提供了其他三种部署方式,详细说明如下:
+
+* 高性能部署:在实际生产环境中,许多应用对部署策略的性能指标(尤其是响应速度)有着较严苛的标准,以确保系统的高效运行与用户体验的流畅性。为此,PaddleX 提供高性能推理插件,旨在对模型推理及前后处理进行深度性能优化,实现端到端流程的显著提速,详细的高性能部署流程请参考 [PaddleX 高性能推理指南](../pipeline_deploy/high_performance_inference.md)。
+* 服务化部署:服务化部署是实际生产环境中常见的一种部署形式。通过将推理功能封装为服务,客户端可以通过网络请求来访问这些服务,以获取推理结果。PaddleX 支持用户以低成本实现产线的服务化部署,详细的服务化部署流程请参考 [PaddleX 服务化部署指南](../pipeline_deploy/service_deploy.md)。
+* 端侧部署:端侧部署是一种将计算和数据处理功能放在用户设备本身上的方式,设备可以直接处理数据,而不需要依赖远程的服务器。PaddleX 支持将模型部署在 Android 等端侧设备上,详细的端侧部署流程请参考 [PaddleX端侧部署指南](../pipeline_deploy/edge_deploy.md)。
+
+您可以根据需要选择合适的方式部署模型产线,进而进行后续的 AI 应用集成。

+ 268 - 0
docs/practical_tutorials/anomaly_detection_tutorial_en.md

@@ -0,0 +1,268 @@
+[简体中文](anomaly_detection_tutorial.md) | English
+
+# PaddleX 3.0 Image Anomaly Detection Pipeline — Food Appearance Quality Inspection Tutorial
+
+PaddleX offers a rich set of pipelines, each consisting of one or more models that can solve specific scenario tasks. All PaddleX pipelines support quick trials, and if the results do not meet expectations, fine-tuning the models with private data is also supported. PaddleX provides Python APIs for easy integration into personal projects. Before use, you need to install PaddleX. For installation instructions, refer to [PaddleX Installation](../installation/installation.en.md). This tutorial introduces the usage of the pipeline tool with an example of a Food Appearance Quality Inspection task.
+
+## 1. Select a Pipeline
+
+First, choose the corresponding PaddleX pipeline based on your task scenario. For Food Appearance Quality Inspection, this falls under the category of anomaly detection tasks, corresponding to PaddleX's Image Anomaly Detection Pipeline. If you are unsure about the correspondence between tasks and pipelines, you can refer to the [Pipeline List](../support_list/pipelines_list.en.md) for an overview of pipeline capabilities.
+
+## 2. Quick Start
+
+PaddleX offers 1 ways to experience the pipeline: one is through the PaddleX wheel package locally.
+
+- Local Experience:
+  ```bash
+  paddlex --pipeline anomaly_detection \
+      --input https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png \
+      --save_path output
+  ```
+
+
+Quick trial output example:
+<center>
+
+<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/image_anomaly_detection/01.png" width=600>
+
+</center>
+
+After experiencing the pipeline, determine if it meets your expectations (including accuracy, speed, etc.). If the model's speed or accuracy does not meet your requirements, you can select alternative models for further testing. If the final results are unsatisfactory, you may need to fine-tune the model. This tutorial aims to produce a model that segments lane lines, and the default weights (trained on the Cityscapes dataset) cannot meet this requirement. Therefore, you need to collect and annotate data for training and fine-tuning.
+
+## 3. Choose a Model
+
+PaddleX provides 1 end-to-end anomaly detection models. For details, refer to the [Model List](../support_list/models_list.en.md). Some model benchmarks are as follows:
+
+| Model List          | Avg (%) | GPU Inference Time (ms) | CPU Inference Time (ms) | Model Size (M) | yaml file |
+|-|-|-|-|-|-|
+|STFPM|96.2|-|-|21.5 M|[STFPM.yaml](../../paddlex/configs/anomaly_detection/STFPM.yaml)|
+
+> **Note: The above accuracy metrics are measured on the [MVTec AD](https://www.mvtec.com/company/research/datasets/mvtec-ad) dataset.**
+
+## 4. Data Preparation and Verification
+### 4.1 Data Preparation
+
+This tutorial uses the "Food Appearance Quality Inspection Dataset" as an example dataset. You can obtain the example dataset using the following commands. If you use your own annotated dataset, you need to adjust it according to PaddleX's format requirements to meet PaddleX's data format specifications. For an introduction to data formats, you can refer to [PaddleX anomaly detection Task Module Data Annotation Tutorial](../data_annotations/cv_modules/semantic_segmentation.en.md).
+
+Dataset acquisition commands:
+```bash
+cd /path/to/paddlex
+wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/anomaly_detection_hazelnut.tar -P ./dataset
+tar -xf ./dataset/anomaly_detection_hazelnut.tar -C ./dataset/
+```
+
+### 4.2 Dataset Verification
+
+To verify the dataset, simply use the following command:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=check_dataset \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut
+```
+
+After executing the above command, PaddleX will verify the dataset and collect basic information about it. Upon successful execution, the log will print "Check dataset passed !" information, and relevant outputs will be saved in the current directory's `./output/check_dataset` directory, including visualized sample images and sample distribution histograms. The verification result file is saved in `./output/check_dataset_result.json`, and the specific content of the verification result file is
+
+```
+{
+  "done_flag": true,
+  "check_pass": true,
+  "attributes": {
+    "train_sample_paths": [
+      "check_dataset\/demo_img\/294.png",
+      "check_dataset\/demo_img\/260.png",
+      "check_dataset\/demo_img\/297.png",
+      "check_dataset\/demo_img\/170.png",
+      "check_dataset\/demo_img\/068.png",
+      "check_dataset\/demo_img\/212.png",
+      "check_dataset\/demo_img\/204.png",
+      "check_dataset\/demo_img\/233.png",
+      "check_dataset\/demo_img\/367.png",
+      "check_dataset\/demo_img\/383.png"
+    ],
+    "train_samples": 391,
+    "val_sample_paths": [
+      "check_dataset\/demo_img\/012.png",
+      "check_dataset\/demo_img\/017.png",
+      "check_dataset\/demo_img\/006.png",
+      "check_dataset\/demo_img\/013.png",
+      "check_dataset\/demo_img\/014.png",
+      "check_dataset\/demo_img\/010.png",
+      "check_dataset\/demo_img\/007.png",
+      "check_dataset\/demo_img\/001.png",
+      "check_dataset\/demo_img\/002.png",
+      "check_dataset\/demo_img\/009.png"
+    ],
+    "val_samples": 70,
+    "num_classes": 1
+  },
+  "analysis": {
+    "histogram": "check_dataset\/histogram.png"
+  },
+  "dataset_path": ".\/dataset\/hazelnut",
+  "show_type": "image",
+  "dataset_type": "SegDataset"
+}
+```
+
+In the verification results above, `check_pass` being `True` indicates that the dataset format meets the requirements. Explanations for other indicators are as follows:
+
+- `attributes.num_classes`: The number of classes in this dataset is 1, which is the number of classes that need to be passed for subsequent training;
+- `attributes.train_samples`: The number of samples in the training set of this dataset is 391;
+- `attributes.val_samples`: The number of samples in the validation set of this dataset is 70;
+- `attributes.train_sample_paths`: A list of relative paths to the visualization images of samples in the training set of this dataset;
+- `attributes.val_sample_paths`: A list of relative paths to the visualization images of samples in the validation set of this dataset;
+
+Additionally, the dataset verification also analyzes the sample distribution across all classes and plots a histogram (`histogram.png`):
+<center>
+
+<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/image_anomaly_detection/02.png" width=600>
+
+</center>
+
+**Note**: Only data that passes verification can be used for training and evaluation.
+
+
+### 4.3 Dataset Format Conversion / Dataset Splitting (Optional)
+
+If you need to convert the dataset format or re-split the dataset, you can set it by modifying the configuration file or appending hyperparameters.
+
+Parameters related to dataset verification can be set by modifying the fields under `CheckDataset` in the configuration file. Examples of some parameters in the configuration file are as follows:
+
+* `CheckDataset`:
+    * `convert`:
+        * `enable`: Whether to convert the dataset format. Set to `True` to enable dataset format conversion, default is `False`;
+        * `src_dataset_type`: If dataset format conversion is enabled, the source dataset format must be set. Available source formats are `LabelMe` and `VOC`;
+    * `split`:
+        * `enable`: Whether to re-split the dataset. Set to `True` to enable dataset splitting, default is `False`;
+        * `train_percent`: If dataset splitting is enabled, the percentage of the training set must be set. The type is any integer between 0-100, and the sum with `val_percent` must be 100;
+        * `val_percent`: If dataset splitting is enabled, the percentage of the validation set must be set. The type is any integer between 0-100, and the sum with `train_percent` must be 100;
+
+Data conversion and splitting can be enabled simultaneously. For data splitting, the original annotation files will be renamed to `xxx.bak` in their original paths. These parameters also support being set by appending command-line arguments, for example, to re-split the dataset and set the training and validation set ratios: `-o CheckDataset.split.enable=True -o CheckDataset.split.train_percent=80 -o CheckDataset.split.val_percent=20`.
+
+## 5. Model Training and Evaluation
+
+### 5.1 Model Training
+
+Before training, ensure that you have validated your dataset. To complete the training of a PaddleX model, simply use the following command:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=train \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut \
+    -o Train.epochs_iters=4000
+```
+
+PaddleX supports modifying training hyperparameters, single/multi-GPU training, and more, simply by modifying the configuration file or appending command line arguments.
+
+Each model in PaddleX provides a configuration file for model development, which is used to set relevant parameters. Model training-related parameters can be set by modifying the `Train` fields in the configuration file. Some example parameter descriptions in the configuration file are as follows:
+
+* `Global`:
+    * `mode`: Mode, supports dataset validation (`check_dataset`), model training (`train`), and model evaluation (`evaluate`);
+    * `device`: Training device, options include `cpu`, `gpu`, `xpu`, `npu`, `mlu`. For multi-GPU training, specify card numbers, e.g., `gpu:0,1,2,3`;
+* `Train`: Training hyperparameter settings;
+    * `epochs_iters`: Number of training iterations;
+    * `learning_rate`: Training learning rate;
+
+For more hyperparameter introductions, refer to [PaddleX General Model Configuration File Parameter Explanation](../module_usage/instructions/config_parameters_common.en.md).
+
+**Note**:
+- The above parameters can be set by appending command line arguments, e.g., specifying the mode as model training: `-o Global.mode=train`; specifying the first two GPUs for training: `-o Global.device=gpu:0,1`; setting the number of training iterations to 5000: `-o Train.epochs_iters=5000`.
+- During model training, PaddleX automatically saves model weight files, with the default being `output`. To specify a save path, use the `-o Global.output` field in the configuration file.
+- PaddleX shields you from the concepts of dynamic graph weights and static graph weights. During model training, both dynamic and static graph weights are produced, and static graph weights are selected by default for model inference.
+
+**Training Outputs Explanation**:
+
+After completing model training, all outputs are saved in the specified output directory (default is `./output/`), typically including the following:
+
+* train_result.json: Training result record file, recording whether the training task completed normally, as well as the output weight metrics, relevant file paths, etc.;
+* train.log: Training log file, recording changes in model metrics, loss, etc. during training;
+* config.yaml: Training configuration file, recording the hyperparameter configuration for this training session;
+* .pdparams, .pdema, .pdopt.pdstate, .pdiparams, .pdmodel: Model weight-related files, including network parameters, optimizer, EMA, static graph network parameters, static graph network structure, etc.;
+
+### 5.2 Model Evaluation
+
+After completing model training, you can evaluate the specified model weight file on the validation set to verify the model's accuracy. To evaluate a model using PaddleX, simply use the following command:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=evaluate \
+    -o Global.dataset_dir=./dataset/anomaly_detection_hazelnut
+```
+
+Similar to model training, model evaluation supports setting parameters by modifying the configuration file or appending command line arguments.
+
+**Note**: When evaluating a model, you need to specify the model weight file path. Each configuration file has a default weight save path. If you need to change it, simply set it by appending a command line argument, e.g., `-o Evaluate.weight_path=./output/best_model/model.pdparams`.
+
+### 5.3 Model Optimization
+
+After learning about model training and evaluation, we can enhance model accuracy by adjusting hyperparameters. By carefully tuning the number of training epochs, you can control the depth of model training, avoiding overfitting or underfitting. Meanwhile, the setting of the learning rate is crucial to the speed and stability of model convergence. Therefore, when optimizing model performance, it is essential to consider the values of these two parameters prudently and adjust them flexibly based on actual conditions to achieve the best training results.
+
+It is recommended to follow the method of controlled variables when debugging parameters:
+
+1. First, fix the number of training iterations at 4000 and the batch size at 1.
+2. Initiate three experiments based on the STFPM model, with learning rates of: 0.01, 0.1, 0.4.
+3. It can be observed that the configuration with the highest accuracy in Experiment 3 is a learning rate of 0.4. Based on this training hyperparameter, change the number of training epochs and observe the accuracy results of different iterations, finding that the optimal accuracy is basically achieved at 5000 iterations.
+
+Learning Rate Exploration Results:
+<center>
+
+| Experiment | Iterations | Learning Rate | batch\_size | Training Environment | mIoU |
+|-----------|------------|-------------|-----------|--------------------|------|
+| Experiment 1 | 4000 | 0\.01 | 1        | 4    | 0\.9646   |
+| Experiment 2 |4000 | 0\.1 | 1        | 4    |0\.9707|
+| Experiment 3 | 4000 | 0\.4  | 1        | 4   | **0\.9797**   |
+
+</center>
+
+Changing Epoch Results:
+<center>
+
+| Experiment                   | Iterations | Learning Rate | batch\_size | Training Environment | mIoU |
+|--------------------|---------|-------|------------|------|----------|
+| Experiment 3                 | 4000    | 0\.4 | 1         | 4   | 0\.9797   |
+| Experiment 3 with more epochs  | 5000   | 0\.4 | 1         | 4   | **0\.9826**|
+
+</center>
+
+**Note: This tutorial is designed for 4 GPUs. If you have only 1 GPU, you can adjust the number of training GPUs to complete the experiment, but the final metrics may not align with the above indicators, which is normal.**
+
+## 6. Production Line Testing
+
+Replace the model in the production line with the fine-tuned model for testing, for example:
+
+```bash
+python main.py -c paddlex/configs/anomaly_detection/STFPM.yaml \
+    -o Global.mode=predict \
+    -o Predict.model_dir="output/best_model/inference" \
+    -o Predict.input="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png"
+```
+
+The prediction results will be generated under `./output`, where the prediction result for `uad_hazelnut.png` is shown below:
+<center>
+
+<img src="https://raw.githubusercontent.com/cuicheng01/PaddleX_doc_images/main/images/practical_tutorials/image_anomaly_detection/03.png" width="600"/>
+
+</center>
+
+## 7. Development Integration/Deployment
+If the anomaly detection pipeline meets your requirements for inference speed and accuracy in the production line, you can proceed directly with development integration/deployment.
+1. Directly apply the trained model in your Python project by referring to the following sample code, and modify the `Pipeline.model` in the `paddlex/pipelines/anomaly_detection.yaml` configuration file to your own model path `output/best_model/inference`:
+```python
+from paddlex import create_pipeline
+pipeline = create_pipeline(pipeline="paddlex/pipelines/anomaly_detection.yaml")
+output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_hazelnut.png")
+for res in output:
+    res.print() # Print the structured output of the prediction
+    res.save_to_img("./output/") # Save the visualized image of the result
+    res.save_to_json("./output/") # Save the structured output of the prediction
+```
+For more parameters, please refer to [Anomaly Detection Pipeline Usage Tutorial](../pipeline_usage/tutorials/cv_pipelines/image_anomaly_detection.en.md).
+
+2. Additionally, PaddleX offers three other deployment methods, detailed as follows:
+
+* high-performance inference: In actual production environments, many applications have stringent standards for deployment strategy performance metrics (especially response speed) to ensure efficient system operation and smooth user experience. To this end, PaddleX provides high-performance inference plugins aimed at deeply optimizing model inference and pre/post-processing for significant end-to-end process acceleration. For detailed high-performance inference procedures, please refer to the [PaddleX High-Performance Inference Guide](../pipeline_deploy/high_performance_inference.en.md).
+* Service-Oriented Deployment: Service-oriented deployment is a common deployment form in actual production environments. By encapsulating inference functions as services, clients can access these services through network requests to obtain inference results. PaddleX supports users in achieving cost-effective service-oriented deployment of production lines. For detailed service-oriented deployment procedures, please refer to the [PaddleX Service-Oriented Deployment Guide](../pipeline_deploy/service_deploy.en.md).
+* Edge Deployment: Edge deployment is a method that places computing and data processing capabilities directly on user devices, allowing devices to process data without relying on remote servers. PaddleX supports deploying models on edge devices such as Android. For detailed edge deployment procedures, please refer to the [PaddleX Edge Deployment Guide](../pipeline_deploy/edge_deploy.en.md).
+
+You can select the appropriate deployment method for your model pipeline according to your needs, and proceed with subsequent AI application integration.

+ 13 - 7
paddlex/modules/anomaly_detection/dataset_checker/dataset_src/analyse_dataset.py

@@ -37,14 +37,20 @@ def anaylse_dataset(dataset_dir, output):
                 _, ann_file = line.split(" ")
                 ann_file = osp.join(dataset_dir, ann_file)
                 ann = np.array(ImageOps.exif_transpose(Image.open(ann_file)), "uint8")
-
-                for idx in set(ann.reshape([-1]).tolist()):
-                    if idx == 255:
-                        continue
-                    if idx not in label2count[tag]:
-                        label2count[tag][idx] = 1
+                if tag == "train":
+                    if 0 not in label2count[tag]:
+                        label2count[tag][0] = 1
                     else:
-                        label2count[tag][idx] += 1
+                        label2count[tag][0] += 1
+
+                if tag == "val":
+                    for idx in set(ann.reshape([-1]).tolist()):
+                        if idx == 255:
+                            continue
+                        if idx not in label2count[tag]:
+                            label2count[tag][idx] = 1
+                        else:
+                            label2count[tag][idx] += 1
             if label2count[tag].get(0, None) is None:
                 label2count[tag][0] = 0
 

+ 2 - 1
paddlex/modules/anomaly_detection/dataset_checker/dataset_src/check_dataset.py

@@ -65,7 +65,8 @@ def check_dataset(dataset_dir, output, sample_num=10):
                     f"The shape of {img_file}:{img.shape[:2]} and "
                     f"{ann_file}:{ann.shape} must be the same!"
                 )
-                class_ids = class_ids | set(ann.reshape([-1]).tolist())
+                if tag == "val":
+                    class_ids = class_ids | set(ann.reshape([-1]).tolist())
                 if i < sample_num:
                     vis_img = visualize(img, ann)
                     vis_img = Image.fromarray(vis_img)