Skip to content

Commit

Permalink
Add more classification demo for ACT (PaddlePaddle#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelXu7 authored Jun 24, 2022
1 parent 9a91814 commit 2d4964b
Show file tree
Hide file tree
Showing 25 changed files with 1,097 additions and 109 deletions.
70 changes: 37 additions & 33 deletions demo/auto_compression/image_classification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,40 @@
本示例将以图像分类模型MobileNetV1为例,介绍如何使用PaddleClas中Inference部署模型进行自动压缩。本示例使用的自动压缩策略为量化训练和蒸馏。

## 2. Benchmark
- PaddlePaddle MobileNetV1模型

| 模型 | 策略 | Top-1 Acc | 耗时(ms) threads=4 |
|:------:|:------:|:------:|:------:|
| MobileNetV1 | Base模型 | 70.90 | 39.041 |
| MobileNetV1 | 量化+蒸馏 | 70.49 | 29.238|
### PaddleClas模型

- 测试环境:`SDM710 2*A75(2.2GHz) 6*A55(1.7GHz)`

- TensorFlow MobileNetV1模型
| 模型 | 策略 | Top-1 Acc | GPU 耗时(ms) | ARM CPU 耗时(ms) |
|:------:|:------:|:------:|:------:|:------:|
| MobileNetV1 | Baseline | 70.90 | - | 33.15 |
| MobileNetV1 | 量化+蒸馏 | 70.49 | - | 13.64 |
| ResNet50_vd | Baseline | 79.12 | 3.19 | - |
| ResNet50_vd | 量化+蒸馏 | 78.55 | 0.92 | - |
| ShuffleNetV2_x1_0 | Baseline | 68.65 | - | 10.43 |
| ShuffleNetV2_x1_0 | 量化+蒸馏 | 67.78 | - | 5.51 |
| SqueezeNet1_0_infer | Baseline | 59.60 | - | 35.98 |
| SqueezeNet1_0_infer | 量化+蒸馏 | 59.13 | - | 16.96 |
| PPLCNetV2_base | Baseline | 76.86 | - | 36.50 |
| PPLCNetV2_base | 量化+蒸馏 | 76.43 | - | 15.79 |
| PPHGNet_tiny | Baseline | 79.59 | 2.82 | - |
| PPHGNet_tiny | 量化+蒸馏 | 79.19 | 0.98 | - |
| EfficientNetB0 | Baseline | 77.02 | 1.95 | - |
| EfficientNetB0 | 量化+蒸馏 | 73.61 | 1.44 | - |
| GhostNet_x1_0 | Baseline | 74.02 | 2.93 | - |
| GhostNet_x1_0 | 量化+蒸馏 | 71.11 | 1.03 | - |
| InceptionV3 | Baseline | 79.14 | 4.79 | - |
| InceptionV3 | 量化+蒸馏 | 73.16 | 1.47 | - |
| MobileNetV3_large_x1_0 | Baseline | 75.32 | - | 16.62 |
| MobileNetV3_large_x1_0 | 量化+蒸馏 | 68.84 | - | 9.85 |

- ARM CPU 测试环境:`SDM865(4xA77+4xA55)`
- Nvidia GPU 测试环境:
- 硬件:NVIDIA Tesla T4 单卡
- 软件:CUDA 11.2, cuDNN 8.0, TensorRT 8.4
- 测试配置:batch_size: 1, image size: 224


### TensorFlow MobileNetV1模型

| 模型 | 策略 | Top-1 Acc | 耗时(ms) threads=1 | Inference模型 |
|:------:|:------:|:------:|:------:|:------:|
Expand All @@ -35,14 +59,8 @@
- 测试环境:`骁龙865 4*A77 4*A55`

说明:
- MobileNetV1模型源自[tensorflow/models](http://download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz),通过[X2Paddle](https://github.com/PaddlePaddle/X2Paddle)工具转换MobileNetV1预测模型步骤:

(1) 安装X2Paddle的1.3.6以上版本;(pip install x2paddle)

(2) 转换模型:
x2paddle --framework=tensorflow --model=tf_model.pb --save_dir=pd_model
- MobileNetV1模型源自[tensorflow/models](http://download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz)

即可得到MobileNetV1模型的预测模型(`model.pdmodel``model.pdiparams`)。如想快速体验,可直接下载上方表格中MobileNetV1的Base预测模型。

## 3. 自动压缩流程

Expand Down Expand Up @@ -90,24 +108,11 @@ tar -xf MobileNetV1_infer.tar
```shell
# 单卡启动
export CUDA_VISIBLE_DEVICES=0
python run.py \
--model_dir='MobileNetV1_infer' \
--model_filename='inference.pdmodel' \
--params_filename='inference.pdiparams' \
--save_dir='./output' \
--batch_size=128 \
--config_path='./configs/mobilenetv1_qat_dis.yaml'\
--data_dir='ILSVRC2012'

# 多卡启动
python -m paddle.distributed.launch run.py \
--model_dir='MobileNetV1_infer' \
--model_filename='inference.pdmodel' \
--params_filename='inference.pdiparams' \
--save_dir='./output' \
--batch_size=128 \
--config_path='./configs/mobilenetv1_qat_dis.yaml'\
--data_dir='ILSVRC2012'
export CUDA_VISIBLE_DEVICES=0,1,2,3

python run.py --save_dir='./save_quant_mobilev1/' --config_path='./configs/MobileNetV1/qat_dis.yaml'

```


Expand All @@ -118,4 +123,3 @@ python -m paddle.distributed.launch run.py \
- [Paddle Lite部署](https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md)

## 5.FAQ
[1.] 如果遇到报错 ```ValueError: var inputs not in this block``` ,则说明模型中的输入变量的名字不是 ```inputs``` ,可以先用netron可视化查看输入变量的名称,然后修改 ```run.py``` 中的第35行中 ``` yield {"inputs": imgs}``````yield {${input_tensor_name}: imgs}```。一般PaddleClas产出部署模型的输入名字如果不是 ```inputs```,则是 ```x```
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Global:
input_name: x
model_dir: EfficientNetB0_infer
model_filename: inference.pdmodel
params_filename: inference.pdiparams
batch_size: 32
data_dir: /ILSVRC2012

Distillation:
alpha: 1.0
loss: l2
node:
- softmax_1.tmp_0
ChannelPrune:
pruned_ratio: 0.25
prune_params_name:
- _blocks.0._se_reduce_weights
- _blocks.0._se_expand_weights
- _blocks.0._project_conv_weights
- _blocks.1._expand_conv_weights
- _blocks.1._se_reduce_weights
- _blocks.1._se_expand_weights
- _blocks.1._project_conv_weights
- _blocks.2._expand_conv_weights
- _blocks.2._se_reduce_weights
- _blocks.2._se_expand_weights
- _blocks.2._project_conv_weights
- _blocks.3._expand_conv_weights
- _blocks.3._se_reduce_weights
- _blocks.3._se_expand_weights
- _blocks.3._project_conv_weights
- _blocks.4._expand_conv_weights
- _blocks.4._se_reduce_weights
- _blocks.4._se_expand_weights
- _blocks.4._project_conv_weights
- _blocks.5._expand_conv_weights
- _blocks.5._se_reduce_weights
- _blocks.5._se_expand_weights
- _blocks.5._project_conv_weights
- _blocks.6._expand_conv_weights
- _blocks.6._se_reduce_weights
- _blocks.6._se_expand_weights
- _blocks.6._project_conv_weights
- _blocks.7._expand_conv_weights
- _blocks.7._se_reduce_weights
- _blocks.7._se_expand_weights
- _blocks.7._project_conv_weights
- _blocks.8._expand_conv_weights
- _blocks.8._se_reduce_weights
- _blocks.8._se_expand_weights
- _blocks.8._project_conv_weights
- _blocks.9._expand_conv_weights
- _blocks.9._se_reduce_weights
- _blocks.9._se_expand_weights
- _blocks.9._project_conv_weights
- _blocks.10._expand_conv_weights
- _blocks.10._se_reduce_weights
- _blocks.10._se_expand_weights
- _blocks.10._project_conv_weights
- _blocks.11._expand_conv_weights
- _blocks.11._se_reduce_weights
- _blocks.11._se_expand_weights
- _blocks.11._project_conv_weights
- _blocks.12._expand_conv_weights
- _blocks.12._se_reduce_weights
- _blocks.12._se_expand_weights
- _blocks.12._project_conv_weights
- _blocks.13._expand_conv_weights
- _blocks.13._se_reduce_weights
- _blocks.13._se_expand_weights
- _blocks.13._project_conv_weights
- _blocks.14._expand_conv_weights
- _blocks.14._se_reduce_weights
- _blocks.14._se_expand_weights
- _blocks.14._project_conv_weights
- _blocks.15._expand_conv_weights
- _blocks.15._se_reduce_weights
- _blocks.15._se_expand_weights
- _blocks.15._project_conv_weights
- _conv_head_weights
criterion: l1_norm
TrainConfig:
epochs: 1
eval_iter: 500
learning_rate:
type: CosineAnnealingDecay
learning_rate: 0.015
T_max: 500
optimizer_builder:
optimizer:
type: Momentum
weight_decay: 0.00002
origin_metric: 0.7738
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Global:
input_name: x
model_dir: EfficientNetB0_infer
model_filename: inference.pdmodel
params_filename: inference.pdiparams
batch_size: 32
data_dir: /ILSVRC2012

Distillation:
alpha: 1.0
loss: l2
node:
- softmax_1.tmp_0
Quantization:
use_pact: true
activation_bits: 8
is_full_quantize: false
activation_quantize_type: range_abs_max
weight_quantize_type: channel_wise_abs_max
not_quant_pattern:
- skip_quant
quantize_op_types:
- conv2d
- depthwise_conv2d
weight_bits: 8
TrainConfig:
epochs: 1
eval_iter: 500
learning_rate:
type: CosineAnnealingDecay
learning_rate: 0.015
T_max: 5000
optimizer_builder:
optimizer:
type: Momentum
weight_decay: 0.00002
origin_metric: 0.7738
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Global:
input_name: inputs
model_dir: GhostNet_x1_0_infer
model_filename: inference.pdmodel
params_filename: inference.pdiparams
batch_size: 32
data_dir: /ILSVRC2012

Distillation:
alpha: 1.0
loss: l2
node:
- softmax_0.tmp_0
ChannelPrune:
pruned_ratio: 0.25
criterion: l1_norm
prune_params_name:
- conv1_weights
- _ghostbottleneck_0_ghost_module_1_primary_conv_weights
- _ghostbottleneck_0_ghost_module_2_primary_conv_weights
- _ghostbottleneck_1_ghost_module_1_primary_conv_weights
- _ghostbottleneck_1_ghost_module_2_primary_conv_weights
- _ghostbottleneck_1_shortcut_conv_weights
- _ghostbottleneck_2_ghost_module_1_primary_conv_weights
- _ghostbottleneck_2_ghost_module_2_primary_conv_weights
- _ghostbottleneck_3_ghost_module_1_primary_conv_weights
- _ghostbottleneck_3_ghost_module_2_primary_conv_weights
- _ghostbottleneck_3_shortcut_conv_weights
- _ghostbottleneck_4_ghost_module_1_primary_conv_weights
- _ghostbottleneck_4_ghost_module_2_primary_conv_weights
- _ghostbottleneck_5_ghost_module_1_primary_conv_weights
- _ghostbottleneck_5_ghost_module_2_primary_conv_weights
- _ghostbottleneck_5_shortcut_conv_weights
- _ghostbottleneck_6_ghost_module_1_primary_conv_weights
- _ghostbottleneck_6_ghost_module_2_primary_conv_weights
- _ghostbottleneck_7_ghost_module_1_primary_conv_weights
- _ghostbottleneck_7_ghost_module_2_primary_conv_weights
- _ghostbottleneck_8_ghost_module_1_primary_conv_weights
- _ghostbottleneck_8_ghost_module_2_primary_conv_weights
- _ghostbottleneck_9_ghost_module_1_primary_conv_weights
- _ghostbottleneck_9_ghost_module_2_primary_conv_weights
- _ghostbottleneck_9_shortcut_conv_weights
- _ghostbottleneck_10_ghost_module_1_primary_conv_weights
- _ghostbottleneck_10_ghost_module_2_primary_conv_weights
- _ghostbottleneck_11_ghost_module_1_primary_conv_weights
- _ghostbottleneck_11_ghost_module_2_primary_conv_weights
- _ghostbottleneck_11_shortcut_conv_weights
- _ghostbottleneck_12_ghost_module_1_primary_conv_weights
- _ghostbottleneck_12_ghost_module_2_primary_conv_weights
- _ghostbottleneck_13_ghost_module_1_primary_conv_weights
- _ghostbottleneck_13_ghost_module_2_primary_conv_weights
- _ghostbottleneck_14_ghost_module_1_primary_conv_weights
- _ghostbottleneck_14_ghost_module_2_primary_conv_weights
- _ghostbottleneck_15_ghost_module_1_primary_conv_weights
- _ghostbottleneck_15_ghost_module_2_primary_conv_weights
- conv_last_weights
- fc_0_weights
TrainConfig:
epochs: 1
eval_iter: 500
learning_rate:
type: CosineAnnealingDecay
learning_rate: 0.015
T_max: 5000
optimizer_builder:
optimizer:
type: Momentum
weight_decay: 0.00002
origin_metric: 0.7402
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Global:
input_name: inputs
model_dir: GhostNet_x1_0_infer
model_filename: inference.pdmodel
params_filename: inference.pdiparams
batch_size: 32
data_dir: /ILSVRC2012

Distillation:
alpha: 1.0
loss: l2
node:
- softmax_0.tmp_0
Quantization:
use_pact: true
activation_bits: 8
is_full_quantize: false
activation_quantize_type: range_abs_max
weight_quantize_type: channel_wise_abs_max
not_quant_pattern:
- skip_quant
quantize_op_types:
- conv2d
- depthwise_conv2d
weight_bits: 8
TrainConfig:
epochs: 1
eval_iter: 500
learning_rate:
type: CosineAnnealingDecay
learning_rate: 0.015
T_max: 5000
optimizer_builder:
optimizer:
type: Momentum
weight_decay: 0.00002
Loading

0 comments on commit 2d4964b

Please sign in to comment.