diff --git a/deploy/configs/PULC/vehicle_attr/inference_vehicle_attr.yaml b/deploy/configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml similarity index 91% rename from deploy/configs/PULC/vehicle_attr/inference_vehicle_attr.yaml rename to deploy/configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml index f47a73ab3..e1ef7d307 100644 --- a/deploy/configs/PULC/vehicle_attr/inference_vehicle_attr.yaml +++ b/deploy/configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml @@ -1,6 +1,6 @@ Global: infer_imgs: "./images/PULC/vehicle_attr/0002_c002_00030670_0.jpg" - inference_model_dir: "./models/vehicle_attr_infer" + inference_model_dir: "./models/vehicle_attribute_infer" batch_size: 1 use_gpu: True enable_mkldnn: True diff --git a/deploy/images/PULC/vehicle_attr/0002_c002_00030670_0.jpg b/deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg similarity index 100% rename from deploy/images/PULC/vehicle_attr/0002_c002_00030670_0.jpg rename to deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg diff --git a/deploy/images/PULC/vehicle_attr/0014_c012_00040750_0.jpg b/deploy/images/PULC/vehicle_attribute/0014_c012_00040750_0.jpg similarity index 100% rename from deploy/images/PULC/vehicle_attr/0014_c012_00040750_0.jpg rename to deploy/images/PULC/vehicle_attribute/0014_c012_00040750_0.jpg diff --git a/docs/zh_CN/PULC/PULC_traffic_sign.md b/docs/zh_CN/PULC/PULC_traffic_sign.md index 8c75d95dd..9d85c8834 100644 --- a/docs/zh_CN/PULC/PULC_traffic_sign.md +++ b/docs/zh_CN/PULC/PULC_traffic_sign.md @@ -39,19 +39,19 @@ 该案例提供了用户使用 PaddleClas 的超轻量图像分类方案(PULC,Practical Ultra Lightweight Classification)快速构建轻量级、高精度、可落地的交通标志分类模型。该模型可以广泛应用于自动驾驶、道路监控等场景。 -下表列出了不同交通标志分类模型的相关指标,前两行展现了使用 SwinTranformer_tiny 和 MobileNetV3_large_x1_0 作为 backbone 训练得到的模型的相关指标,第三行至第六行依次展现了替换 backbone 为 PPLCNet_x1_0、使用 SSLD 预训练模型、使用 SSLD 预训练模型 + EDA 策略、使用 SSLD 预训练模型 + EDA 策略 + SKL-UGI 知识蒸馏策略训练得到的模型的相关指标。 +下表列出了不同交通标志分类模型的相关指标,前两行展现了使用 SwinTranformer_tiny 和 MobileNetV3_small_x0_35 作为 backbone 训练得到的模型的相关指标,第三行至第六行依次展现了替换 backbone 为 PPLCNet_x1_0、使用 SSLD 预训练模型、使用 SSLD 预训练模型 + EDA 策略、使用 SSLD 预训练模型 + EDA 策略 + SKL-UGI 知识蒸馏策略训练得到的模型的相关指标。 | 模型 | Top-1 Acc(%) | 延时(ms) | 存储(M) | 策略 | |-------|-----------|----------|---------------|---------------| | SwinTranformer_tiny | 98.11 | 89.45 | 111 | 使用ImageNet预训练模型 | -| MobileNetV3_large_x1_0 | 97.79 | 4.81 | 23 | 使用ImageNet预训练模型 | +| MobileNetV3_small_x0_35 | 93.88 | 3.01 | 3.9 | 使用ImageNet预训练模型 | | PPLCNet_x1_0 | 97.78 | 2.10 | 8.2 | 使用ImageNet预训练模型 | | PPLCNet_x1_0 | 97.84 | 2.10 | 8.2 | 使用SSLD预训练模型 | | PPLCNet_x1_0 | 98.14 | 2.10 | 8.2 | 使用SSLD预训练模型+EDA策略| | PPLCNet_x1_0 | 98.35 | 2.10 | 8.2 | 使用SSLD预训练模型+EDA策略+SKL-UGI知识蒸馏策略| -从表中可以看出,backbone 为 SwinTranformer_tiny 时精度较高,但是推理速度较慢。将 backbone 替换为轻量级模型 MobileNetV3_large_x1_0 后,速度可以大幅提升,但是精度下降明显。将 backbone 替换为 PPLCNet_x1_0 时,精度低0.01%,但是速度提升 1 倍左右。在此基础上,使用 SSLD 预训练模型后,在不改变推理速度的前提下,精度可以提升约 0.06%,进一步地,当融合EDA策略后,精度可以再提升 0.3%,最后,在使用 SKL-UGI 知识蒸馏后,精度可以继续提升 0.21%。此时,PPLCNet_x1_0 的精度超越了SwinTranformer_tiny,速度快 41 倍。关于 PULC 的训练方法和推理部署方法将在下面详细介绍。 +从表中可以看出,backbone 为 SwinTranformer_tiny 时精度较高,但是推理速度较慢。将 backbone 替换为轻量级模型 MobileNetV3_small_x0_35 后,速度可以大幅提升,但是精度下降明显。将 backbone 替换为 PPLCNet_x1_0 时,精度低3.9%,同时速度提升 43% 左右。在此基础上,使用 SSLD 预训练模型后,在不改变推理速度的前提下,精度可以提升约 0.06%,进一步地,当融合EDA策略后,精度可以再提升 0.3%,最后,在使用 SKL-UGI 知识蒸馏后,精度可以继续提升 0.21%。此时,PPLCNet_x1_0 的精度超越了 SwinTranformer_tiny,速度快 41 倍。关于 PULC 的训练方法和推理部署方法将在下面详细介绍。 **备注:** @@ -62,8 +62,48 @@ ## 2. 模型快速体验 - (pip方式,待补充) + +### 2.1 安装 paddleclas + +使用如下命令快速安装 paddlepaddle, paddleclas + +```bash +pip3 install paddlepaddle paddleclas +``` + + +### 2.2 预测 + +* 使用命令行快速预测 + +```bash +paddleclas --model_name traffic_sign --infer_imgs PaddleClas/deploy/images/PULC/traffic_sign/100999_83928.jpg +``` + +结果如下: +``` +>>> result +class_ids: [182, 179, 162, 128, 24], scores: [0.98623, 0.01255, 0.00022, 0.00021, 0.00012], label_names: ['pl110', 'pl100', 'pl120', 'p26', 'pm10'], filename: PaddleClas/deploy/images/PULC/traffic_sign/100999_83928.jpg +``` + +**备注**: 更换其他预测的数据时,只需要改变 `--infer_imgs=xx` 中的字段即可,支持传入整个文件夹。 + + +* 在 Python 代码中预测 +```python +import paddleclas +model = paddleclas.PaddleClas(model_name="traffic_sign") +result = model.predict(input_data="PaddleClas/deploy/images/PULC/traffic_sign/100999_83928.jpg") +print(next(result)) +``` + +**备注**:`model.predict()` 为可迭代对象(`generator`),因此需要使用 `next()` 函数或 `for` 循环对其迭代调用。每次调用将以 `batch_size` 为单位进行一次预测,并返回预测结果, 默认 `batch_size` 为 1,如果需要更改 `batch_size`,实例化模型时,需要指定 `batch_size`,如 `model = paddleclas.PaddleClas(model_name="person_exists", batch_size=2)`, 使用默认的代码返回结果示例如下: + +``` +result +[{'class_ids': [182, 179, 162, 128, 24], 'scores': [0.98623, 0.01255, 0.00022, 0.00021, 0.00012], 'label_names': ['pl110', 'pl100', 'pl120', 'p26', 'pm10'], 'filename': 'PaddleClas/deploy/images/PULC/traffic_sign/100999_83928.jpg'}] +``` diff --git a/docs/zh_CN/PULC/PULC_vehicle_attr.md b/docs/zh_CN/PULC/PULC_vehicle_attribute.md similarity index 73% rename from docs/zh_CN/PULC/PULC_vehicle_attr.md rename to docs/zh_CN/PULC/PULC_vehicle_attribute.md index c7087cfc7..23eec4c07 100644 --- a/docs/zh_CN/PULC/PULC_vehicle_attr.md +++ b/docs/zh_CN/PULC/PULC_vehicle_attribute.md @@ -39,20 +39,20 @@ 该案例提供了用户使用 PaddleClas 的超轻量图像分类方案(PULC,Practical Ultra Lightweight Classification)快速构建轻量级、高精度、可落地的车辆属性识别模型。该模型可以广泛应用于车辆识别、道路监控等场景。 -下表列出了不同车辆属性识别模型的相关指标,前两行展现了使用 Res2Net200_vd_26w_4s 和 MobileNetV3_large_x1_0 作为 backbone 训练得到的模型的相关指标,第三行至第六行依次展现了替换 backbone 为 PPLCNet_x1_0、使用 SSLD 预训练模型、使用 SSLD 预训练模型 + EDA 策略、使用 SSLD 预训练模型 + EDA 策略 + SKL-UGI 知识蒸馏策略训练得到的模型的相关指标。 +下表列出了不同车辆属性识别模型的相关指标,前两行展现了使用 Res2Net200_vd_26w_4s 和 MobileNetV3_small_x0_35 作为 backbone 训练得到的模型的相关指标,第三行至第六行依次展现了替换 backbone 为 PPLCNet_x1_0、使用 SSLD 预训练模型、使用 SSLD 预训练模型 + EDA 策略、使用 SSLD 预训练模型 + EDA 策略 + SKL-UGI 知识蒸馏策略训练得到的模型的相关指标。 | 模型 | ma(%) | 延时(ms) | 存储(M) | 策略 | |-------|-----------|----------|---------------|---------------| | Res2Net200_vd_26w_4s | 91.36 | 79.46 | 293 | 使用ImageNet预训练模型 | | ResNet50 | 89.98 | 12.83 | 92 | 使用ImageNet预训练模型 | -| MobileNetV3_large_x1_0 | 89.77 | 5.09 | 23 | 使用ImageNet预训练模型 | +| MobileNetV3_small_x0_35 | 87.41 | 2.91 | 2.8 | 使用ImageNet预训练模型 | | PPLCNet_x1_0 | 89.57 | 2.36 | 8.2 | 使用ImageNet预训练模型 | | PPLCNet_x1_0 | 90.07 | 2.36 | 8.2 | 使用SSLD预训练模型 | | PPLCNet_x1_0 | 90.59 | 2.36 | 8.2 | 使用SSLD预训练模型+EDA策略| | PPLCNet_x1_0 | 90.81 | 2.36 | 8.2 | 使用SSLD预训练模型+EDA策略+SKL-UGI知识蒸馏策略| -从表中可以看出,backbone 为 Res2Net200_vd_26w_4s 时精度较高,但是推理速度较慢。将 backbone 替换为轻量级模型 MobileNetV3_large_x1_0 后,速度可以大幅提升,但是精度下降明显。将 backbone 替换为 PPLCNet_x1_0 时,精度低0.2%,但是速度提升 1 倍左右。在此基础上,使用 SSLD 预训练模型后,在不改变推理速度的前提下,精度可以提升约 0.5%,进一步地,当融合EDA策略后,精度可以再提升 0.52%,最后,在使用 SKL-UGI 知识蒸馏后,精度可以继续提升 0.23%。此时,PPLCNet_x1_0 的精度与 Res2Net200_vd_26w_4s 仅相差0.55%,但是速度快32倍。关于 PULC 的训练方法和推理部署方法将在下面详细介绍。 +从表中可以看出,backbone 为 Res2Net200_vd_26w_4s 时精度较高,但是推理速度较慢。将 backbone 替换为轻量级模型 MobileNetV3_small_x0_35 后,速度可以大幅提升,但是精度下降明显。将 backbone 替换为 PPLCNet_x1_0 时,精度提升 2.16%,同时速度也提升 23% 左右。在此基础上,使用 SSLD 预训练模型后,在不改变推理速度的前提下,精度可以提升约 0.5%,进一步地,当融合EDA策略后,精度可以再提升 0.52%,最后,在使用 SKL-UGI 知识蒸馏后,精度可以继续提升 0.23%。此时,PPLCNet_x1_0 的精度与 Res2Net200_vd_26w_4s 仅相差0.55%,但是速度快32倍。关于 PULC 的训练方法和推理部署方法将在下面详细介绍。 **备注:** @@ -63,8 +63,48 @@ ## 2. 模型快速体验 + + +### 2.1 安装 paddleclas + +使用如下命令快速安装 paddlepaddle, paddleclas + +```bash +pip3 install paddlepaddle paddleclas ``` -(pip方式,待补充) + + +### 2.2 预测 + +* 使用命令行快速预测 + +```bash +paddleclas --model_name vehicle_attribute --infer_imgs PaddleClas/deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg +``` + +结果如下: +``` +>>> result +attributes: Color: (yellow, prob: 0.9893476963043213), Type: (hatchback, prob: 0.9734097719192505), output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], filename: PaddleClas/deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg +ppcls INFO: Predict complete! +``` + +**备注**: 更换其他预测的数据时,只需要改变 `--infer_imgs=xx` 中的字段即可,支持传入整个文件夹。 + + +* 在 Python 代码中预测 +```python +import paddleclas +model = paddleclas.PaddleClas(model_name="vehicle_attribute") +result = model.predict(input_data="PaddleClas/deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg") +print(next(result)) +``` + +**备注**:`model.predict()` 为可迭代对象(`generator`),因此需要使用 `next()` 函数或 `for` 循环对其迭代调用。每次调用将以 `batch_size` 为单位进行一次预测,并返回预测结果, 默认 `batch_size` 为 1,如果需要更改 `batch_size`,实例化模型时,需要指定 `batch_size`,如 `model = paddleclas.PaddleClas(model_name="person_exists", batch_size=2)`, 使用默认的代码返回结果示例如下: + +``` +result +[{'attributes': 'Color: (yellow, prob: 0.9893476963043213), Type: (hatchback, prob: 0.9734097719192505)', 'output': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 'filename': 'PaddleClas/deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg'}] ``` @@ -94,7 +134,7 @@ 部分数据可视化如下所示。
- +
首先从[VeRi数据集官网](https://www.v7labs.com/open-datasets/veri-dataset)中申请并下载数据,放在PaddleClas的`dataset`目录下,数据集目录名为`VeRi`,使用下面的命令进入该文件夹。 @@ -172,17 +212,17 @@ VeRi ### 3.3 模型训练 -在 `ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml` 中提供了基于该场景的训练配置,可以通过如下脚本启动训练: +在 `ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml` 中提供了基于该场景的训练配置,可以通过如下脚本启动训练: ```shell export CUDA_VISIBLE_DEVICES=0,1,2,3 python3 -m paddle.distributed.launch \ --gpus="0,1,2,3" \ tools/train.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml ``` -验证集的最佳指标在 `90.07%` 左右(数据集较小,一般有0.3%左右的波动)。 +验证集的最佳指标在 `90.59%` 左右(数据集较小,一般有0.3%左右的波动)。 @@ -193,7 +233,7 @@ python3 -m paddle.distributed.launch \ ```bash python3 tools/eval.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml \ + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml \ -o Global.pretrained_model="output/PPLCNet_x1_0/best_model" ``` @@ -207,21 +247,21 @@ python3 tools/eval.py \ ```bash python3 tools/infer.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml \ + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml \ -o Global.pretrained_model=output/DistillationModel/best_model ``` 输出结果如下: ``` -[{'attr': 'Color: (yellow, prob: 0.9893478155136108), Type: (hatchback, prob: 0.9734100103378296)', 'pred': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 'file_name': './deploy/images/PULC/vehicle_attr/0002_c002_00030670_0.jpg'}] +[{'attr': 'Color: (yellow, prob: 0.9893478155136108), Type: (hatchback, prob: 0.9734100103378296)', 'pred': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 'file_name': './deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg'}] ``` **备注:** * 这里`-o Global.pretrained_model="output/PPLCNet_x1_0/best_model"` 指定了当前最佳权重所在的路径,如果指定其他权重,只需替换对应的路径即可。 -* 默认是对 `./deploy/images/PULC/vehicle_attr/0002_c002_00030670_0.jpg` 进行预测,此处也可以通过增加字段 `-o Infer.infer_imgs=xxx` 对其他图片预测。 +* 默认是对 `./deploy/images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg` 进行预测,此处也可以通过增加字段 `-o Infer.infer_imgs=xxx` 对其他图片预测。 @@ -237,14 +277,14 @@ SKL-UGI 知识蒸馏是 PaddleClas 提出的一种简单有效的知识蒸馏方 #### 4.1.1 教师模型训练 -复用 `ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml` 中的超参数,训练教师模型,训练脚本如下: +复用 `ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml` 中的超参数,训练教师模型,训练脚本如下: ```shell export CUDA_VISIBLE_DEVICES=0,1,2,3 python3 -m paddle.distributed.launch \ --gpus="0,1,2,3" \ tools/train.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml \ + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml \ -o Arch.name=ResNet101_vd ``` @@ -254,14 +294,14 @@ python3 -m paddle.distributed.launch \ #### 4.1.2 蒸馏训练 -配置文件`ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_distillation.yaml`提供了`SKL-UGI知识蒸馏策略`的配置。该配置将`ResNet101_vd`当作教师模型,`PPLCNet_x1_0`当作学生模型。训练脚本如下: +配置文件`ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_distillation.yaml`提供了`SKL-UGI知识蒸馏策略`的配置。该配置将`ResNet101_vd`当作教师模型,`PPLCNet_x1_0`当作学生模型。训练脚本如下: ```shell export CUDA_VISIBLE_DEVICES=0,1,2,3 python3 -m paddle.distributed.launch \ --gpus="0,1,2,3" \ tools/train.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_distillation.yaml \ + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_distillation.yaml \ -o Arch.models.0.Teacher.pretrained=output/ResNet101_vd/best_model ``` @@ -296,14 +336,14 @@ Paddle Inference 是飞桨的原生推理库, 作用于服务器端和云端 ```bash python3 tools/export_model.py \ - -c ./ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml \ + -c ./ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml \ -o Global.pretrained_model=output/DistillationModel/best_model_student \ - -o Global.save_inference_dir=deploy/models/PPLCNet_x1_0_vehicle_attr_infer + -o Global.save_inference_dir=deploy/models/PPLCNet_x1_0_vehicle_attribute_infer ``` -执行完该脚本后会在 `deploy/models/` 下生成 `PPLCNet_x1_0_vehicle_attr_infer` 文件夹,`models` 文件夹下应有如下文件结构: +执行完该脚本后会在 `deploy/models/` 下生成 `PPLCNet_x1_0_vehicle_attributeibute_infer` 文件夹,`models` 文件夹下应有如下文件结构: ``` -├── PPLCNet_x1_0_vehicle_attr_infer +├── PPLCNet_x1_0_vehicle_attribute_infer │ ├── inference.pdiparams │ ├── inference.pdiparams.info │ └── inference.pdmodel @@ -320,13 +360,13 @@ python3 tools/export_model.py \ ``` cd deploy/models # 下载 inference 模型并解压 -wget https://paddleclas.bj.bcebos.com/models/PULC/vehicle_attr_infer.tar && tar -xf vehicle_attr_infer.tar +wget https://paddleclas.bj.bcebos.com/models/PULC/vehicle_attribute_infer.tar && tar -xf vehicle_attribute_infer.tar ``` 解压完毕后,`models` 文件夹下应有如下文件结构: ``` -├── vehicle_attr_infer +├── vehicle_attribute_infer │ ├── inference.pdiparams │ ├── inference.pdiparams.info │ └── inference.pdmodel @@ -347,13 +387,13 @@ wget https://paddleclas.bj.bcebos.com/models/PULC/vehicle_attr_infer.tar && tar cd ../ ``` -运行下面的命令,对图像 `./images/PULC/vehicle_attr/0002_c002_00030670_0.jpg` 进行车辆属性识别。 +运行下面的命令,对图像 `./images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg` 进行车辆属性识别。 ```shell # 使用下面的命令使用 GPU 进行预测 -python3.7 python/predict_cls.py -c configs/PULC/vehicle_attr/inference_vehicle_attr.yaml -o Global.use_gpu=True +python3.7 python/predict_cls.py -c configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml -o Global.use_gpu=True # 使用下面的命令使用 CPU 进行预测 -python3.7 python/predict_cls.py -c configs/PULC/vehicle_attr/inference_vehicle_attr.yaml -o Global.use_gpu=False +python3.7 python/predict_cls.py -c configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml -o Global.use_gpu=False ``` 输出结果如下。 @@ -371,7 +411,7 @@ predict output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ```shell # 使用下面的命令使用 GPU 进行预测,如果希望使用 CPU 预测,可以在命令后面添加 -o Global.use_gpu=False -python3.7 python/predict_cls.py -c configs/PULC/vehicle_attr/inference_vehicle_attr.yaml -o Global.infer_imgs="./images/PULC/vehicle_attr/" +python3.7 python/predict_cls.py -c configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml -o Global.infer_imgs="./images/PULC/vehicle_attribute/" ``` 终端中会输出该文件夹内所有图像的属性识别结果,如下所示。 diff --git a/docs/zh_CN/advanced_tutorials/knowledge_distillation.md b/docs/zh_CN/advanced_tutorials/knowledge_distillation.md index e895a412e..6224e82a7 100644 --- a/docs/zh_CN/advanced_tutorials/knowledge_distillation.md +++ b/docs/zh_CN/advanced_tutorials/knowledge_distillation.md @@ -11,8 +11,9 @@ - [1.2 PaddleClas支持的知识蒸馏算法](#1.2) - [1.2.1 SSLD](#1.2.1) - [1.2.2 DML](#1.2.2) - - [1.2.3 AFD](#1.2.3) - - [1.2.4 DKD](#1.2.4) + - [1.2.3 UDML](#1.2.3) + - [1.2.4 AFD](#1.2.4) + - [1.2.5 DKD](#1.2.5) - [2. 使用方法](#2) - [2.1 环境配置](#2.1) - [2.2 数据准备](#2.2) @@ -196,9 +197,80 @@ Loss: -#### 1.2.3 AFD +#### 1.2.3 UDML -##### 1.2.3.1 AFD 算法介绍 +##### 1.2.3.1 UDML 算法介绍 + +论文信息: + +UDML 是百度飞桨视觉团队提出的无需依赖教师模型的知识蒸馏算法,它基于DML进行改进,在蒸馏的过程中,除了考虑两个模型的输出信息,也考虑两个模型的中间层特征信息,从而进一步提升知识蒸馏的精度。更多关于UDML的说明与应用,请参考[PP-ShiTu论文](https://arxiv.org/abs/2111.00775)以及[PP-OCRv3论文](https://arxiv.org/abs/2109.03144)。 + + + +在ImageNet1k公开数据集上,效果如下所示。 + +| 策略 | 骨干网络 | 配置文件 | Top-1 acc | 下载链接 | +| --- | --- | --- | --- | --- | +| baseline | PPLCNet_x2_5 | [PPLCNet_x2_5.yaml](../../../ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_5.yaml) | 74.93% | - | +| UDML | PPLCNet_x2_5 | [PPLCNet_x2_5_dml.yaml](../../../ppcls/configs/ImageNet/Distillation/PPLCNet_x2_5_udml.yaml) | 76.74%(**+1.81%**) | - | + + +##### 1.2.3.2 UDML 配置 + + +```yaml +Arch: + name: "DistillationModel" + class_num: &class_num 1000 + # if not null, its lengths should be same as models + pretrained_list: + # if not null, its lengths should be same as models + freeze_params_list: + - False + - False + models: + - Teacher: + name: PPLCNet_x2_5 + class_num: *class_num + pretrained: False + # return_patterns表示除了返回输出的logits,也会返回对应名称的中间层feature map + return_patterns: ["blocks3", "blocks4", "blocks5", "blocks6"] + - Student: + name: PPLCNet_x2_5 + class_num: *class_num + pretrained: False + return_patterns: ["blocks3", "blocks4", "blocks5", "blocks6"] + +# loss function config for traing/eval process +Loss: + Train: + - DistillationGTCELoss: + weight: 1.0 + key: logits + model_names: ["Student", "Teacher"] + - DistillationDMLLoss: + weight: 1.0 + key: logits + model_name_pairs: + - ["Student", "Teacher"] + - DistillationDistanceLoss: # 基于蒸馏结果的距离loss,这里默认使用l2 loss计算block5之间的损失函数 + weight: 1.0 + key: "blocks5" + model_name_pairs: + - ["Student", "Teacher"] + Eval: + - CELoss: + weight: 1.0 +``` + +**注意(:** 上述在网络中指定`return_patterns`,返回中间层特征的功能是基于TheseusLayer,更多关于TheseusLayer的使用说明,请参考:[TheseusLayer 使用说明](./theseus_layer.md)。 + + + + +#### 1.2.4 AFD + +##### 1.2.4.1 AFD 算法介绍 论文信息: @@ -220,7 +292,7 @@ AFD提出在蒸馏的过程中,利用基于注意力的元网络学习特征 注意:这里为了与论文的训练配置保持对齐,设置训练的迭代轮数为100epoch,因此baseline精度低于PaddleClas中开源出的模型精度(71.0%) -##### 1.2.3.2 AFD 配置 +##### 1.2.4.2 AFD 配置 AFD配置如下所示。在模型构建Arch字段中,需要同时定义学生模型与教师模型,固定教师模型的权重。这里需要对从教师模型获取的特征进行变换,进而与学生模型进行损失函数的计算。在损失函数Loss字段中,需要定义`DistillationKLDivLoss`(学生与教师之间的KL-Div loss)、`AFDLoss`(学生与教师之间的AFD loss)以及`DistillationGTCELoss`(学生与教师关于真值标签的CE loss),作为训练的损失函数。 @@ -305,11 +377,11 @@ Loss: **注意(:** 上述在网络中指定`return_patterns`,返回中间层特征的功能是基于TheseusLayer,更多关于TheseusLayer的使用说明,请参考:[TheseusLayer 使用说明](./theseus_layer.md)。 - + -#### 1.2.4 DKD +#### 1.2.5 DKD -##### 1.2.4.1 DKD 算法介绍 +##### 1.2.5.1 DKD 算法介绍 论文信息: @@ -330,7 +402,7 @@ DKD将蒸馏中常用的 KD Loss 进行了解耦成为Target Class Knowledge Dis | AFD | ResNet18 | [resnet34_distill_resnet18_dkd.yaml](../../../ppcls/configs/ImageNet/Distillation/resnet34_distill_resnet18_dkd.yaml) | 72.59%(**+1.79%**) | - | -##### 1.2.4.2 DKD 配置 +##### 1.2.5.2 DKD 配置 DKD 配置如下所示。在模型构建Arch字段中,需要同时定义学生模型与教师模型,教师模型固定参数,且需要加载预训练模型。在损失函数Loss字段中,需要定义`DistillationDKDLoss`(学生与教师之间的DKD loss)以及`DistillationGTCELoss`(学生与教师关于真值标签的CE loss),作为训练的损失函数。 diff --git a/ppcls/configs/ImageNet/Distillation/PPLCNet_x2_5_udml.yaml b/ppcls/configs/ImageNet/Distillation/PPLCNet_x2_5_udml.yaml new file mode 100644 index 000000000..43b64888d --- /dev/null +++ b/ppcls/configs/ImageNet/Distillation/PPLCNet_x2_5_udml.yaml @@ -0,0 +1,167 @@ +# global configs +Global: + checkpoints: null + pretrained_model: null + output_dir: ./output_lcnet_x2_5_udml + device: gpu + save_interval: 1 + eval_during_train: True + eval_interval: 1 + epochs: 100 + print_batch_step: 10 + use_visualdl: False + # used for static mode and model export + image_shape: [3, 224, 224] + save_inference_dir: ./inference + +AMP: + scale_loss: 128.0 + use_dynamic_loss_scaling: True + # O1: mixed fp16 + level: O1 + +# model architecture +Arch: + name: "DistillationModel" + class_num: &class_num 1000 + # if not null, its lengths should be same as models + pretrained_list: + # if not null, its lengths should be same as models + freeze_params_list: + - False + - False + models: + - Teacher: + name: PPLCNet_x2_5 + class_num: *class_num + pretrained: False + return_patterns: ["blocks3", "blocks4", "blocks5", "blocks6"] + - Student: + name: PPLCNet_x2_5 + class_num: *class_num + pretrained: False + return_patterns: ["blocks3", "blocks4", "blocks5", "blocks6"] + +# loss function config for traing/eval process +Loss: + Train: + - DistillationGTCELoss: + weight: 1.0 + key: logits + model_names: ["Student", "Teacher"] + - DistillationDMLLoss: + weight: 1.0 + key: logits + model_name_pairs: + - ["Student", "Teacher"] + - DistillationDistanceLoss: + weight: 1.0 + key: "blocks5" + model_name_pairs: + - ["Student", "Teacher"] + Eval: + - CELoss: + weight: 1.0 + + +Optimizer: + name: Momentum + momentum: 0.9 + lr: + name: Cosine + learning_rate: 0.4 + warmup_epoch: 5 + regularizer: + name: 'L2' + coeff: 0.00004 + + +# data loader for train and eval +DataLoader: + Train: + dataset: + name: ImageNetDataset + image_root: ./dataset/ILSVRC2012/ + cls_label_path: ./dataset/ILSVRC2012/train_list.txt + transform_ops: + - DecodeImage: + to_rgb: True + channel_first: False + - RandCropImage: + size: 224 + - RandFlipImage: + flip_code: 1 + - NormalizeImage: + scale: 1.0/255.0 + mean: [0.485, 0.456, 0.406] + std: [0.229, 0.224, 0.225] + order: '' + + sampler: + name: DistributedBatchSampler + batch_size: 128 + drop_last: False + shuffle: True + loader: + num_workers: 8 + use_shared_memory: True + + Eval: + dataset: + name: ImageNetDataset + image_root: ./dataset/ILSVRC2012/ + cls_label_path: ./dataset/ILSVRC2012/val_list.txt + transform_ops: + - DecodeImage: + to_rgb: True + channel_first: False + - ResizeImage: + resize_short: 256 + - CropImage: + size: 224 + - NormalizeImage: + scale: 1.0/255.0 + mean: [0.485, 0.456, 0.406] + std: [0.229, 0.224, 0.225] + order: '' + sampler: + name: DistributedBatchSampler + batch_size: 256 + drop_last: False + shuffle: False + loader: + num_workers: 8 + use_shared_memory: True + +Infer: + infer_imgs: docs/images/inference_deployment/whl_demo.jpg + batch_size: 10 + transforms: + - DecodeImage: + to_rgb: True + channel_first: False + - ResizeImage: + resize_short: 256 + - CropImage: + size: 224 + - NormalizeImage: + scale: 1.0/255.0 + mean: [0.485, 0.456, 0.406] + std: [0.229, 0.224, 0.225] + order: '' + - ToCHWImage: + PostProcess: + name: Topk + topk: 5 + class_id_map_file: ppcls/utils/imagenet1k_label_list.txt + +Metric: + Train: + - DistillationTopkAcc: + model_key: "Student" + topk: [1, 5] + Eval: + - DistillationTopkAcc: + model_key: "Student" + topk: [1, 5] + diff --git a/ppcls/configs/PULC/traffic_sign/MobileNetV3_large_x1_0.yaml b/ppcls/configs/PULC/traffic_sign/MobileNetV3_samll_x0_35.yaml similarity index 98% rename from ppcls/configs/PULC/traffic_sign/MobileNetV3_large_x1_0.yaml rename to ppcls/configs/PULC/traffic_sign/MobileNetV3_samll_x0_35.yaml index e76db0479..503675898 100644 --- a/ppcls/configs/PULC/traffic_sign/MobileNetV3_large_x1_0.yaml +++ b/ppcls/configs/PULC/traffic_sign/MobileNetV3_samll_x0_35.yaml @@ -16,7 +16,7 @@ Global: # model architecture Arch: - name: MobileNetV3_large_x1_0 + name: MobileNetV3_small_x0_35 class_num: 232 pretrained: True @@ -40,7 +40,7 @@ Optimizer: warmup_epoch: 5 regularizer: name: 'L2' - coeff: 0.00002 + coeff: 0.00001 # data loader for train and eval diff --git a/ppcls/configs/PULC/vehicle_attr/MobileNetV3_large_x1_0.yaml b/ppcls/configs/PULC/vehicle_attribute/MobileNetV3_small_x0_35.yaml similarity index 98% rename from ppcls/configs/PULC/vehicle_attr/MobileNetV3_large_x1_0.yaml rename to ppcls/configs/PULC/vehicle_attribute/MobileNetV3_small_x0_35.yaml index 52f5dd8e8..a35bc6114 100644 --- a/ppcls/configs/PULC/vehicle_attr/MobileNetV3_large_x1_0.yaml +++ b/ppcls/configs/PULC/vehicle_attribute/MobileNetV3_small_x0_35.yaml @@ -17,7 +17,7 @@ Global: # model architecture Arch: - name: "MobileNetV3_large_x1_0" + name: "MobileNetV3_small_x0_35" pretrained: True class_num: 19 infer_add_softmax: False diff --git a/ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml b/ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0.yaml rename to ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0.yaml diff --git a/ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_distillation.yaml b/ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_distillation.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_distillation.yaml rename to ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_distillation.yaml diff --git a/ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_search.yaml b/ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_search.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/PPLCNet_x1_0_search.yaml rename to ppcls/configs/PULC/vehicle_attribute/PPLCNet_x1_0_search.yaml diff --git a/ppcls/configs/PULC/vehicle_attr/Res2Net200_vd_26w_4s.yaml b/ppcls/configs/PULC/vehicle_attribute/Res2Net200_vd_26w_4s.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/Res2Net200_vd_26w_4s.yaml rename to ppcls/configs/PULC/vehicle_attribute/Res2Net200_vd_26w_4s.yaml diff --git a/ppcls/configs/PULC/vehicle_attr/ResNet50.yaml b/ppcls/configs/PULC/vehicle_attribute/ResNet50.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/ResNet50.yaml rename to ppcls/configs/PULC/vehicle_attribute/ResNet50.yaml diff --git a/ppcls/configs/PULC/vehicle_attr/search.yaml b/ppcls/configs/PULC/vehicle_attribute/search.yaml similarity index 100% rename from ppcls/configs/PULC/vehicle_attr/search.yaml rename to ppcls/configs/PULC/vehicle_attribute/search.yaml