Skip to content

Commit

Permalink
Update figure in README (PaddlePaddle#1117) (PaddlePaddle#1125)
Browse files Browse the repository at this point in the history
Co-authored-by: whs <[email protected]>
  • Loading branch information
ceci3 and wanghaoshuang authored May 20, 2022
1 parent d98c773 commit 44e69a8
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 79 deletions.
8 changes: 5 additions & 3 deletions demo/auto_compression/detection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
| 模型 | 策略 | 输入尺寸 | mAP<sup>val<br>0.5:0.95 | 预测时延<sup><small>FP32</small><sup><br><sup>(ms) |预测时延<sup><small>FP32</small><sup><br><sup>(ms) | 预测时延<sup><small>INT8</small><sup><br><sup>(ms) | 配置文件 | Inference模型 |
| :-------- |:-------- |:--------: | :---------------------: | :----------------: | :----------------: | :---------------: | :-----------------------------: | :-----------------------------: |
| PP-YOLOE-l | Base模型 | 640*640 | 50.9 | 11.2 | 7.7ms | - | [config](https://github.com/PaddlePaddle/PaddleDetection/blob/develop/configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml) | [Model](https://bj.bcebos.com/v1/paddle-slim-models/detection/ppyoloe_crn_l_300e_coco.tar) |
| PP-YOLOE-l | 量化+蒸馏 | 640*640 | 50.6 | - | - | 6.7ms | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/auto_compression/detection/configs/ppyoloe_l_qat_dist.yaml) | [Model](https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_crn_l_300e_coco_quant.tar) |
| PP-YOLOE-l | 量化+蒸馏 | 640*640 | 50.6 | - | - | 6.7ms | [config](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml) | [Model](https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_crn_l_300e_coco_quant.tar) |

- mAP的指标均在COCO val2017数据集中评测得到。
- PP-YOLOE模型在Tesla V100的GPU环境下测试,测试脚本是[benchmark demo](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/deploy/python)
Expand Down Expand Up @@ -95,7 +95,8 @@ tar -xf ppyoloe_crn_l_300e_coco.tar

使用run.py脚本得到模型的mAP:
```
python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --eval=True
export CUDA_VISIBLE_DEVEICES=0
python run.py --config_path=./configs/ppyoloe_l_qat_dis.yaml --eval=True
```

**注意**:TinyPose模型暂不支持精度测试。
Expand All @@ -104,7 +105,8 @@ python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --eval=True

蒸馏量化自动压缩示例通过run.py脚本启动,会使用接口```paddleslim.auto_compression.AutoCompression```对模型进行自动压缩。配置config文件中模型路径、蒸馏、量化、和训练等部分的参数,配置完成后便可对模型进行量化和蒸馏。具体运行命令为:
```
python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --save_dir='./output/'
export CUDA_VISIBLE_DEVEICES=0
python run.py --config_path=./configs/ppyoloe_l_qat_dis.yaml --save_dir='./output/'
```


Expand Down
27 changes: 14 additions & 13 deletions demo/auto_compression/detection/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ def gen():
return gen


def eval(compress_config):
def eval(config):

place = paddle.CUDAPlace(0) if FLAGS.devices == 'gpu' else paddle.CPUPlace()
exe = paddle.static.Executor(place)

val_program, feed_target_names, fetch_targets = paddle.fluid.io.load_inference_model(
compress_config["model_dir"],
config["model_dir"],
exe,
model_filename=compress_config["model_filename"],
params_filename=compress_config["params_filename"], )
model_filename=config["model_filename"],
params_filename=config["params_filename"], )
clsid2catid = {v: k for k, v in dataset.catid2clsid.items()}

anno_file = dataset.get_anno()
Expand All @@ -85,7 +85,7 @@ def eval(compress_config):
data_all = {k: np.array(v) for k, v in data.items()}
data_input = {}
for k, v in data.items():
if k in compress_config['input_list']:
if k in config['input_list']:
data_input[k] = np.array(v)
outs = exe.run(val_program,
feed=data_input,
Expand Down Expand Up @@ -142,13 +142,14 @@ def eval_function(exe, compiled_test_program, test_feed_names, test_fetch_list):


def main():
compress_config, train_config = load_slim_config(FLAGS.config_path)
reader_cfg = load_config(compress_config['reader_config'])
compress_config, train_config, global_config = load_slim_config(
FLAGS.config_path)
reader_cfg = load_config(global_config['reader_config'])

train_loader = create('EvalReader')(reader_cfg['TrainDataset'],
reader_cfg['worker_num'],
return_list=True)
train_loader = reader_wrapper(train_loader, compress_config['input_list'])
train_loader = reader_wrapper(train_loader, global_config['input_list'])

global dataset
dataset = reader_cfg['EvalDataset']
Expand All @@ -158,18 +159,18 @@ def main():
return_list=True)

if FLAGS.eval:
eval(compress_config)
eval(global_config)
sys.exit(0)

if 'Evaluation' in compress_config.keys() and compress_config['Evaluation']:
if 'Evaluation' in global_config.keys() and global_config['Evaluation']:
eval_func = eval_function
else:
eval_func = None

ac = AutoCompression(
model_dir=compress_config["model_dir"],
model_filename=compress_config["model_filename"],
params_filename=compress_config["params_filename"],
model_dir=global_config["model_dir"],
model_filename=global_config["model_filename"],
params_filename=global_config["params_filename"],
save_dir=FLAGS.save_dir,
strategy_config=compress_config,
train_config=train_config,
Expand Down
2 changes: 2 additions & 0 deletions demo/auto_compression/image_classification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ tar -xf MobileNetV1_infer.tar

```shell
# 单卡启动
export CUDA_VISIBLE_DEVEICES=0
python run.py \
--model_dir='MobileNetV1_infer' \
--model_filename='inference.pdmodel' \
Expand Down Expand Up @@ -97,3 +98,4 @@ 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```
5 changes: 3 additions & 2 deletions demo/auto_compression/image_classification/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
sys.path[0] = os.path.join(os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
sys.path[0] = os.path.join(
os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
import argparse
import functools
from functools import partial
Expand Down Expand Up @@ -32,7 +33,7 @@ def reader_wrapper(reader):
def gen():
for i, data in enumerate(reader()):
imgs = np.float32([item[0] for item in data])
yield {"x": imgs}
yield {"inputs": imgs}

return gen

Expand Down
1 change: 1 addition & 0 deletions demo/auto_compression/image_classification/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 单卡启动
export CUDA_VISIBLE_DEVEICES=0
python run.py \
--model_dir='MobileNetV1_infer' \
--model_filename='inference.pdmodel' \
Expand Down
6 changes: 3 additions & 3 deletions demo/auto_compression/nlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pip install paddleslim
安装paddlenlp:
```shell
pip install paddlenlp
```
```

注:安装PaddleNLP的目的是为了下载PaddleNLP中的数据集和Tokenizer。

Expand All @@ -88,6 +88,7 @@ tar -zxvf afqmc.tar
数据集为CLUE,不同任务名称代表CLUE上不同的任务,可选择的任务名称有:afqmc, tnews, iflytek, ocnli, cmnli, cluewsc2020, csl。具体运行命令为
```shell
export CUDA_VISIBLE_DEVEICES=0
python run.py \
--model_type='ppminilm' \
--model_dir='./afqmc/' \
Expand All @@ -98,7 +99,7 @@ python run.py \
--batch_size=16 \
--max_seq_length=128 \
--task_name='afqmc' \
--config_path='./configs/afqmc.yaml'
--config_path='./configs/afqmc.yaml'
```

## 4. 压缩配置介绍
Expand Down Expand Up @@ -184,4 +185,3 @@ Quantization:
- [Paddle Lite部署](https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md)
## 6. FAQ
35 changes: 19 additions & 16 deletions demo/auto_compression/nlp/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
sys.path[0] = os.path.join(os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
sys.path[0] = os.path.join(
os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
import argparse
import functools
from functools import partial
Expand Down Expand Up @@ -58,7 +59,9 @@ def convert_example(example,
label_list,
max_seq_length=512,
is_test=False):
assert args.dataset in ['glue', 'clue'], "This demo only supports for dataset glue or clue"
assert args.dataset in [
'glue', 'clue'
], "This demo only supports for dataset glue or clue"
"""Convert a glue example into necessary features."""
if args.dataset == 'glue':
if not is_test:
Expand All @@ -74,13 +77,14 @@ def convert_example(example,
return example['input_ids'], example['token_type_ids'], label
else:
return example['input_ids'], example['token_type_ids']
else: #if args.dataset == 'clue':

else: #if args.dataset == 'clue':
if not is_test:
# `label_list == None` is for regression task
label_dtype = "int64" if label_list else "float32"
# Get the label
example['label'] = np.array(example["label"], dtype="int64").reshape((-1, 1))
example['label'] = np.array(
example["label"], dtype="int64").reshape((-1, 1))
label = example['label']
# Convert raw text to feature
if 'keyword' in example: # CSL
Expand All @@ -91,12 +95,13 @@ def convert_example(example,
'label': example['label']
}
elif 'target' in example: # wsc
text, query, pronoun, query_idx, pronoun_idx = example['text'], example[
'target']['span1_text'], example['target']['span2_text'], example[
'target']['span1_index'], example['target']['span2_index']
text, query, pronoun, query_idx, pronoun_idx = example[
'text'], example['target']['span1_text'], example['target'][
'span2_text'], example['target']['span1_index'], example[
'target']['span2_index']
text_list = list(text)
assert text[pronoun_idx:(pronoun_idx + len(pronoun)
)] == pronoun, "pronoun: {}".format(pronoun)
assert text[pronoun_idx:(pronoun_idx + len(
pronoun))] == pronoun, "pronoun: {}".format(pronoun)
assert text[query_idx:(query_idx + len(query)
)] == query, "query: {}".format(query)
if pronoun_idx > query_idx:
Expand Down Expand Up @@ -125,8 +130,6 @@ def convert_example(example,
else:
return example['input_ids'], example['token_type_ids']




def create_data_holder(task_name):
"""
Expand All @@ -148,7 +151,7 @@ def reader():
# Create the tokenizer and dataset
if args.model_type == 'bert':
tokenizer = BertTokenizer.from_pretrained(args.model_dir)
else: # ppminilm
else: # ppminilm
tokenizer = PPMiniLMTokenizer.from_pretrained(args.model_dir)
train_ds, dev_ds = load_dataset(
args.dataset, args.task_name, splits=('train', 'dev'))
Expand Down Expand Up @@ -239,7 +242,7 @@ def apply_decay_param_fun(name):
print_arguments(args)
paddle.enable_static()

compress_config, train_config = load_config(args.config_path)
compress_config, train_config, _ = load_config(args.config_path)
if train_config is not None and 'optim_args' in train_config:
train_config['optim_args'][
'apply_decay_param_fun'] = apply_decay_param_fun
Expand All @@ -256,8 +259,8 @@ def apply_decay_param_fun(name):
strategy_config=compress_config,
train_config=train_config,
train_dataloader=train_dataloader,
eval_callback=eval_function
if compress_config is None or 'HyperParameterOptimization' not in compress_config else
eval_callback=eval_function if compress_config is None or
'HyperParameterOptimization' not in compress_config else
eval_dataloader,
eval_dataloader=eval_dataloader)

Expand Down
7 changes: 5 additions & 2 deletions demo/auto_compression/semantic_segmentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pip install paddleseg
- 如果想快速体验,可直接下载PP-HumanSeg-Lite 的预测模型:

```shell
wegt https://paddleseg.bj.bcebos.com/dygraph/ppseg/ppseg_lite_portrait_398x224_with_softmax.tar.gz
wget https://paddleseg.bj.bcebos.com/dygraph/ppseg/ppseg_lite_portrait_398x224_with_softmax.tar.gz
tar -xzf ppseg_lite_portrait_398x224_with_softmax.tar.gz
```

Expand All @@ -87,16 +87,18 @@ tar -xzf ppseg_lite_portrait_398x224_with_softmax.tar.gz
当只设置训练参数,并传入``deploy_hardware``字段时,将自动搜索压缩策略进行压缩。以骁龙710(SD710)为部署硬件,进行自动压缩的运行命令如下:

```shell
export CUDA_VISIBLE_DEVICES=0
python run.py \
--model_dir='./ppseg_lite_portrait_398x224_with_softmax' \
--model_filename='model.pdmodel' \
--params_filename='model.pdiparams' \
--save_dir='./save_model' \
--config_path='configs/pp_humanseg_auto.yaml'
--config_path='configs/pp_humanseg_auto.yaml' \
--deploy_hardware='SD710'
```
- 自行配置稀疏参数进行非结构化稀疏和蒸馏训练,配置参数含义详见[自动压缩超参文档](https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md)。具体命令如下所示:
```shell
export CUDA_VISIBLE_DEVICES=0
python run.py \
--model_dir='./ppseg_lite_portrait_398x224_with_softmax' \
--model_filename='model.pdmodel' \
Expand All @@ -107,6 +109,7 @@ python run.py \

- 自行配置量化参数进行量化和蒸馏训练,配置参数含义详见[自动压缩超参文档](https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md)。具体命令如下所示:
```shell
export CUDA_VISIBLE_DEVICES=0
python run.py \
--model_dir='./ppseg_lite_portrait_398x224_with_softmax' \
--model_filename='model.pdmodel' \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml

TrainConfig:
epochs: 14
Expand All @@ -8,4 +8,4 @@ TrainConfig:
optim_args:
weight_decay: 0.0005
optimizer: SGD


Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ train_dataset:
val_dataset:
type: Dataset
dataset_root: data/humanseg
train_path: data/humanseg/val.txt
val_path: data/humanseg/val.txt
num_classes: 2
transforms:
- type: PaddingByAspectRatio
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml

Distillation:
distill_lambda: 1.0
Expand All @@ -26,4 +26,4 @@ TrainConfig:
learning_rate: 0.0005
optimizer: SGD
optim_args:
weight_decay: 4.0e-05
weight_decay: 4.0e-05
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml

Distillation:
distill_lambda: 1.0
Expand Down Expand Up @@ -31,4 +31,4 @@ TrainConfig:
optim_args:
weight_decay: 0.0005
optimizer: SGD


6 changes: 3 additions & 3 deletions demo/auto_compression/semantic_segmentation/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def gen():

args = parse_args()

compress_config, train_config = load_config(args.config_path)
cfg = Config(compress_config['reader_config'])
compress_config, train_config, global_config = load_config(args.config_path)
cfg = Config(global_config['reader_config'])

train_dataset = cfg.train_dataset
eval_dataset = cfg.val_dataset
Expand All @@ -167,7 +167,7 @@ def gen():
ac = AutoCompression(
model_dir=args.model_dir,
model_filename=args.model_filename,
params_filename=args.param_filename,
params_filename=args.params_filename,
save_dir=args.save_dir,
strategy_config=compress_config,
train_config=train_config,
Expand Down
Loading

0 comments on commit 44e69a8

Please sign in to comment.