未验证 提交 44e69a8d 编写于 作者: C ceci3 提交者: GitHub

Update figure in README (#1117) (#1125)

Co-authored-by: Nwhs <wanghaoshuang@baidu.com>
上级 d98c7738
......@@ -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)
......@@ -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模型暂不支持精度测试。
......@@ -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/'
```
......
......@@ -66,16 +66,16 @@ def reader_wrapper(reader, input_list):
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()
......@@ -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,
......@@ -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']
......@@ -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,
......
......@@ -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' \
......@@ -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```
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
......@@ -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
......
# 单卡启动
export CUDA_VISIBLE_DEVEICES=0
python run.py \
--model_dir='MobileNetV1_infer' \
--model_filename='inference.pdmodel' \
......
......@@ -62,7 +62,7 @@ pip install paddleslim
安装paddlenlp:
```shell
pip install paddlenlp
```
```
注:安装PaddleNLP的目的是为了下载PaddleNLP中的数据集和Tokenizer。
......@@ -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/' \
......@@ -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. 压缩配置介绍
......@@ -184,4 +185,3 @@ Quantization:
- [Paddle Lite部署](https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md)
## 6. FAQ
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
......@@ -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:
......@@ -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
......@@ -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:
......@@ -125,8 +130,6 @@ def convert_example(example,
else:
return example['input_ids'], example['token_type_ids']
def create_data_holder(task_name):
"""
......@@ -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'))
......@@ -239,7 +242,7 @@ if __name__ == '__main__':
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
......@@ -256,8 +259,8 @@ if __name__ == '__main__':
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)
......
......@@ -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
```
......@@ -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' \
......@@ -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' \
......
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml
TrainConfig:
epochs: 14
......@@ -8,4 +8,4 @@ TrainConfig:
optim_args:
weight_decay: 0.0005
optimizer: SGD
\ No newline at end of file
......@@ -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
......
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml
Distillation:
distill_lambda: 1.0
......@@ -26,4 +26,4 @@ TrainConfig:
learning_rate: 0.0005
optimizer: SGD
optim_args:
weight_decay: 4.0e-05
\ No newline at end of file
weight_decay: 4.0e-05
Global:
reader_config: configs/pp_humanseg_lite.yml
reader_config: configs/pp_humanseg_lite.yaml
Distillation:
distill_lambda: 1.0
......@@ -31,4 +31,4 @@ TrainConfig:
optim_args:
weight_decay: 0.0005
optimizer: SGD
\ No newline at end of file
......@@ -148,8 +148,8 @@ if __name__ == '__main__':
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
......@@ -167,7 +167,7 @@ if __name__ == '__main__':
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,
......
......@@ -27,7 +27,7 @@ __all__ = [
# config tester to test the loss of quant_post
hpo_config_tester = {
"ptq_algo": ["avg"],
"ptq_algo": ["avg", "mse", "KL"],
"weight_quantize_type": ['channel_wise_abs_max', 'abs_max'],
"bias_correct": [False],
"batch_num": [5],
......@@ -72,7 +72,8 @@ EXPERIENCE_STRATEGY_WITHOUT_LOSS = [
]
MAGIC_SPARSE_RATIO = 0.75
### TODO: 0.03 threshold maybe not suitable, need to check
MAGIC_EMD_DISTANCE = 0.03
MAGIC_MAX_EMD_DISTANCE = 0.03
MAGIC_MIN_EMD_DISTANCE = 0.01
DEFAULT_TRANSFORMER_STRATEGY = 'prune_0.25_int8'
DEFAULT_STRATEGY = 'origin_int8'
......@@ -224,9 +225,13 @@ def prepare_strategy(model_dir,
return strategy_config
def get_final_quant_config(ptq_loss, mode='DistilQuant'):
def get_final_quant_config(ptq_loss):
""" transform quantization tester config to real quantization config """
if mode == 'HPO':
### if emd loss less than MAGIC_MIN_EMD_DISTANCE, final compress.
if ptq_loss < MAGIC_MIN_EMD_DISTANCE:
return None
### if emd loss less than MAGIC_MAX_EMD_DISTANCE, select quant_post & hpo.
elif ptq_loss < MAGIC_MAX_EMD_DISTANCE:
quant_config = Quantization(**default_quant_config)
hpo_config = HyperParameterOptimization(**default_hpo_config)
configs = [{
......@@ -234,7 +239,8 @@ def get_final_quant_config(ptq_loss, mode='DistilQuant'):
'HyperParameterOptimization': hpo_config
}]
if mode == 'DistilQuant':
### if emd loss greater than MAGIC_MAX_EMD_DISTANCE, select qat & dist.
else:
quant_config = Quantization(**default_quant_config)
dis_config = Distillation()
configs = [{'Quantization': quant_config, 'Distillation': dis_config}]
......
......@@ -357,26 +357,27 @@ class AutoCompression:
).lower() == 'linux':
ptq_loss = quant_post_hpo.g_min_emd_loss
final_quant_config = get_final_quant_config(
ptq_loss, mode='DistilQuant')
quant_strategy, quant_config = self._prepare_strategy(
final_quant_config)
self.single_strategy_compress(quant_strategy[0], quant_config[0],
strategy_idx)
final_quant_config = get_final_quant_config(ptq_loss)
if final_quant_config is not None:
quant_strategy, quant_config = self._prepare_strategy(
final_quant_config)
self.single_strategy_compress(quant_strategy[0],
quant_config[0], strategy_idx)
tmp_model_path = os.path.join(
self.save_dir, 'strategy_{}'.format(str(strategy_idx + 1)))
final_model_path = os.path.join(self.final_dir)
if not os.path.exists(final_model_path):
os.makedirs(final_model_path)
tmp_model_file = os.path.join(tmp_model_path, 'model.pdmodel')
tmp_params_file = os.path.join(tmp_model_path, 'model.pdiparams')
final_model_file = os.path.join(final_model_path, 'model.pdmodel')
final_params_file = os.path.join(final_model_path, 'model.pdiparams')
shutil.move(tmp_model_file, final_model_file)
shutil.move(tmp_params_file, final_params_file)
_logger.info(
"==> Finished the ACT process and the final model is saved in:{}".
format(final_model_path))
tmp_model_file = os.path.join(tmp_model_path, self.model_filename)
tmp_params_file = os.path.join(tmp_model_path, self.params_filename)
final_model_file = os.path.join(final_model_path, self.model_filename)
final_params_file = os.path.join(final_model_path, self.params_filename)
if paddle.distributed.get_rank() == 0:
shutil.move(tmp_model_file, final_model_file)
shutil.move(tmp_params_file, final_params_file)
_logger.info(
"==> Finished the ACT process and the final model is saved in:{}".
format(final_model_path))
os._exit(0)
def single_strategy_compress(self, strategy, config, strategy_idx):
......@@ -569,5 +570,5 @@ class AutoCompression:
target_vars=test_program_info.fetch_targets,
executor=self._exe,
main_program=test_program,
model_filename='model.pdmodel',
params_filename='model.pdiparams')
model_filename=self.model_filename,
params_filename=self.params_filename)
......@@ -25,14 +25,16 @@ def load_config(config_path):
cfg = yaml.load(f, Loader=yaml.FullLoader)
f.close()
global_config = {}
if 'Global' in cfg:
for g_key, g_value in cfg["Global"].items():
global_config[g_key] = g_value
cfg.pop('Global')
compress_config = {}
for key, value in cfg.items():
if key == "Global":
for g_key, g_value in cfg["Global"].items():
compress_config[g_key] = g_value
else:
default_key = eval(key)(**value)
compress_config[key] = default_key
default_key = eval(key)(**value)
compress_config[key] = default_key
if compress_config.get('TrainConfig') != None:
train_config = compress_config.pop('TrainConfig')
......@@ -42,7 +44,7 @@ def load_config(config_path):
if len(compress_config) == 0:
compress_config = None
return compress_config, train_config
return compress_config, train_config, global_config
def save_config(config, config_path):
......
......@@ -14,4 +14,4 @@
# limitations under the License.
""" PaddleSlim version string """
__all__ = ["slim_version"]
slim_version = "1.0.0"
slim_version = "2.3.0"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册