--- comments: true --- # 语义分割模块使用教程 ## 一、概述 语义分割是计算机视觉中的一种技术,它通过对图像中每个像素进行分类,将图像划分为不同的语义区域,每个区域对应一个具体的类别。这种技术能够生成精细的分割图,清晰地展现图像中的对象及其边界,为图像分析和理解提供有力支持。 ## 二、支持模型列表
| 模型名称 | Model Download Link | mloU(%) | GPU推理耗时(ms) | CPU推理耗时 (ms) | 模型存储大小(M) |
|---|---|---|---|---|---|
| OCRNet_HRNet-W48 | Inference Model/Trained Model | 82.15 | 78.9976 | 2226.95 | 249.8 M |
| PP-LiteSeg-T | Inference Model/Trained Model | 73.10 | 7.6827 | 138.683 | 28.5 M |
| 模型名称 | Model Download Link | mloU(%) | GPU推理耗时(ms) | CPU推理耗时 (ms) | 模型存储大小(M) |
|---|---|---|---|---|---|
| Deeplabv3_Plus-R50 | Inference Model/Trained Model | 80.36 | 61.0531 | 1513.58 | 94.9 M |
| Deeplabv3_Plus-R101 | Inference Model/Trained Model | 81.10 | 100.026 | 2460.71 | 162.5 M |
| Deeplabv3-R50 | Inference Model/Trained Model | 79.90 | 82.2631 | 1735.83 | 138.3 M |
| Deeplabv3-R101 | Inference Model/Trained Model | 80.85 | 121.492 | 2685.51 | 205.9 M |
| OCRNet_HRNet-W18 | Inference Model/Trained Model | 80.67 | 48.2335 | 906.385 | 43.1 M |
| OCRNet_HRNet-W48 | Inference Model/Trained Model | 82.15 | 78.9976 | 2226.95 | 249.8 M |
| PP-LiteSeg-T | Inference Model/Trained Model | 73.10 | 7.6827 | 138.683 | 28.5 M |
| PP-LiteSeg-B | Inference Model/Trained Model | 75.25 | 10.9935 | 194.727 | 47.0 M |
| SegFormer-B0 (slice) | Inference Model/Trained Model | 76.73 | 11.1946 | 268.929 | 13.2 M |
| SegFormer-B1 (slice) | Inference Model/Trained Model | 78.35 | 17.9998 | 403.393 | 48.5 M |
| SegFormer-B2 (slice) | Inference Model/Trained Model | 81.60 | 48.0371 | 1248.52 | 96.9 M |
| SegFormer-B3 (slice) | Inference Model/Trained Model | 82.47 | 64.341 | 1666.35 | 167.3 M |
| SegFormer-B4 (slice) | Inference Model/Trained Model | 82.38 | 82.4336 | 1995.42 | 226.7 M |
| SegFormer-B5 (slice) | Inference Model/Trained Model | 82.58 | 97.3717 | 2420.19 | 229.7 M |
以上模型精度指标测量自Cityscapes数据集。GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为 8,精度类型为 FP32。
| 模型名称 | Model Download Link | mloU(%) | GPU推理耗时(ms) | CPU推理耗时 | 模型存储大小(M) |
|---|---|---|---|---|---|
| SeaFormer_base | Inference Model/Trained Model | 40.92 | 24.4073 | 397.574 | 30.8 M |
| SeaFormer_large | Inference Model/Trained Model | 43.66 | 27.8123 | 550.464 | 49.8 M |
| SeaFormer_small | Inference Model/Trained Model | 38.73 | 19.2295 | 358.343 | 14.3 M |
| SeaFormer_tiny | Inference Model/Trained Model | 34.58 | 13.9496 | 330.132 | 6.1M |
| MaskFormer_small | Inference Model/Trained Model | 49.70 | |||
| MaskFormer_tiny | Inference Model/Trained Model | 46.69 |
以上模型的精度指标测量自ADE20k数据集。GPU 推理耗时基于 NVIDIA Tesla T4 机器,精度类型为 FP32, CPU 推理速度基于 Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz,线程数为 8,精度类型为 FP32。
相关方法、参数等说明如下:
* `create_model`实例化通用语义分割模型(此处以`PP-LiteSeg-T`为例),具体说明如下:
| 参数 | 参数说明 | 参数类型 | 可选项 | 默认值 |
|---|---|---|---|---|
model_name |
模型名称 | str |
无 | 无 |
model_dir |
模型存储路径 | str |
无 | 无 |
target_size |
模型预测时分辨率 | int/tuple |
None/-1/int/tuple |
None |
| 参数 | 参数说明 | 参数类型 | 可选项 | 默认值 |
|---|---|---|---|---|
input |
待预测数据,支持多种输入类型 | Python Var/str/dict/list |
|
无 |
batch_size |
批大小 | int |
任意整数 | 1 |
target_size |
推理时图像的尺寸(W, H) | int/tuple |
|
None |
| 方法 | 方法说明 | 参数 | 参数类型 | 参数说明 | 默认值 |
|---|---|---|---|---|---|
print() |
打印结果到终端 | format_json |
bool |
是否对输出内容进行使用 JSON 缩进格式化 |
True |
indent |
int |
指定缩进级别,以美化输出的 JSON 数据,使其更具可读性,仅当 format_json 为 True 时有效 |
4 | ||
ensure_ascii |
bool |
控制是否将非 ASCII 字符转义为 Unicode。设置为 True 时,所有非 ASCII 字符将被转义;False 则保留原始字符,仅当format_json为True时有效 |
False |
||
save_to_json() |
将结果保存为json格式的文件 | save_path |
str |
保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致 | 无 |
indent |
int |
指定缩进级别,以美化输出的 JSON 数据,使其更具可读性,仅当 format_json 为 True 时有效 |
4 | ||
ensure_ascii |
bool |
控制是否将非 ASCII 字符转义为 Unicode。设置为 True 时,所有非 ASCII 字符将被转义;False 则保留原始字符,仅当format_json为True时有效 |
False |
||
save_to_img() |
将结果保存为图像格式的文件 | save_path |
str |
保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致 | 无 |
| 属性 | 属性说明 |
|---|---|
json |
获取预测的json格式的结果 |
img |
获取格式为dict的可视化图像 |
校验结果文件具体内容为:
{
"done_flag": true,
"check_pass": true,
"attributes": {
"train_sample_paths": [
"check_dataset/demo_img/P0005.jpg",
"check_dataset/demo_img/P0050.jpg"
],
"train_samples": 267,
"val_sample_paths": [
"check_dataset/demo_img/N0139.jpg",
"check_dataset/demo_img/P0137.jpg"
],
"val_samples": 76,
"num_classes": 2
},
"analysis": {
"histogram": "check_dataset/histogram.png"
},
"dataset_path": "./dataset/seg_optic_examples",
"show_type": "image",
"dataset_type": "SegDataset"
}
上述校验结果中,check_pass 为 True 表示数据集格式符合要求,其他部分指标的说明如下:
attributes.num_classes:该数据集类别数为 2;attributes.train_samples:该数据集训练集样本数量为 267;attributes.val_samples:该数据集验证集样本数量为 76;attributes.train_sample_paths:该数据集训练集样本可视化图片相对路径列表;attributes.val_sample_paths:该数据集验证集样本可视化图片相对路径列表;数据集校验还对数据集中所有类别的样本数量分布情况进行了分析,并绘制了分布直方图(histogram.png):

在您完成数据校验之后,可以通过修改配置文件或是追加超参数的方式对数据集的格式进行转换,也可以对数据集的训练/验证比例进行重新划分。
(1)数据集格式转换
语义分割支持 LabelMe 格式的数据集转换为要求的格式。
数据集校验相关的参数可以通过修改配置文件中 CheckDataset 下的字段进行设置,配置文件中部分参数的示例说明如下:
CheckDataset:convert:enable: 是否进行数据集格式转换,支持LabelMe 格式的数据集转换,默认为 False;src_dataset_type: 如果进行数据集格式转换,则需设置源数据集格式,默认为 null,支持的源数据集格式是LabelMe;
例如,您想转换 LabelMe 格式的数据集,提供了一个LabelMe 格式的示例数据集,如下:wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/seg_dataset_to_convert.tar -P ./dataset
tar -xf ./dataset/seg_dataset_to_convert.tar -C ./dataset/
下载后,需要修改 paddlex/configs/modules/semantic_segmentation/PP-LiteSeg-T.yaml配置如下:
......
CheckDataset:
......
convert:
enable: True
src_dataset_type: LabelMe
......
随后执行命令:
python main.py -c paddlex/configs/modules/semantic_segmentation/PP-LiteSeg-T.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/seg_dataset_to_convert
当然,以上参数同样支持通过追加命令行参数的方式进行设置,以 LabelMe 格式的数据集为例:
python main.py -c paddlex/configs/modules/semantic_segmentation/PP-LiteSeg-T.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/seg_dataset_to_convert \
-o CheckDataset.convert.enable=True \
-o CheckDataset.convert.src_dataset_type=LabelMe
(2)数据集划分
数据集划分的参数可以通过修改配置文件中 CheckDataset 下的字段进行设置,配置文件中部分参数的示例说明如下:
例如,您想重新划分数据集为 训练集占比90%、验证集占比10%,则需将配置文件修改为:
......
CheckDataset:
......
split:
enable: True
train_percent: 90
val_percent: 10
......
随后执行命令:
python main.py -c paddlex/configs/modules/semantic_segmentation/PP-LiteSeg-T.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/seg_optic_examples
数据划分执行之后,原有标注文件会被在原路径下重命名为 xxx.bak。
以上参数同样支持通过追加命令行参数的方式进行设置:
python main.py -c paddlex/configs/modules/semantic_segmentation/PP-LiteSeg-T.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/seg_optic_examples \
-o CheckDataset.split.enable=True \
-o CheckDataset.split.train_percent=90 \
-o CheckDataset.split.val_percent=10
output,如需指定保存路径,可通过配置文件中 -o Global.output 字段进行设置。在完成模型训练后,所有产出保存在指定的输出目录(默认为./output/)下,通常有以下产出:
train_result.json:训练结果记录文件,记录了训练任务是否正常完成,以及产出的权重指标、相关文件路径等;
train.log:训练日志文件,记录了训练过程中的模型指标变化、loss 变化等;config.yaml:训练配置文件,记录了本次训练的超参数的配置;.pdparams、.pdema、.pdopt.pdstate、.pdiparams、.pdmodel:模型权重相关文件,包括网络参数、优化器、EMA、静态图网络参数、静态图网络结构等;在模型评估时,需要指定模型权重文件路径,每个配置文件中都内置了默认的权重保存路径,如需要改变,只需要通过追加命令行参数的形式进行设置即可,如-o Evaluate.weight_path=``./output/best_model/best_model.pdparams。
在完成模型评估后,通常有以下产出:
在完成模型评估后,会产出evaluate_result.json,其记录了评估的结果,具体来说,记录了评估任务是否正常完成,以及模型的评估指标,包含 Top1 Acc