diff --git a/docs/docs/api/analysis_api.md b/docs/docs/api/analysis_api.md index 2880e42a289fd80e1356ce0f3f18bc212661d33e..62e2bc97900ffe1038f149eb2e64ca5083abca89 100644 --- a/docs/docs/api/analysis_api.md +++ b/docs/docs/api/analysis_api.md @@ -19,7 +19,7 @@ paddleslim.analysis.flops(program, detail=False) [源代码](https://github.com/ **示例:** -``` +```python import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr from paddleslim.analysis import flops @@ -90,7 +90,7 @@ paddleslim.analysis.model_size(program) [源代码](https://github.com/PaddlePad **示例:** -``` +```python import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr from paddleslim.analysis import model_size diff --git a/docs/docs/api/nas_api.md b/docs/docs/api/nas_api.md index d0145380b030a5e759bc57a2186ff46e40c8dcbb..573afd7e211f5e4644f61348bb1c6b9e82768844 100644 --- a/docs/docs/api/nas_api.md +++ b/docs/docs/api/nas_api.md @@ -33,7 +33,7 @@ paddleslim.nas.SANAS(configs, server_addr=("", 8881), init_temperature=100, redu 一个SANAS类的实例 **示例代码:** -``` +```python from paddleslim.nas import SANAS config = [('MobileNetV2Space')] sanas = SANAS(config=config) @@ -54,7 +54,7 @@ tokens是一个列表,token映射到搜索空间转换成相应的网络结构 根据传入的token得到一个模型结构实例。 **示例代码:** -``` +```python import paddle.fluid as fluid input = fluid.data(name='input', shape=[None, 3, 32, 32], dtype='float32') archs = sanas.token2arch(tokens) @@ -70,7 +70,7 @@ paddleslim.nas.SANAS.next_archs() 返回模型结构实例的列表,形式为list。 **示例代码:** -``` +```python import paddle.fluid as fluid input = fluid.data(name='input', shape=[None, 3, 32, 32], dtype='float32') archs = sanas.next_archs() diff --git a/docs/docs/api/prune_api.md b/docs/docs/api/prune_api.md index 2b659a17ead7737d293a72941136a411c3378160..0f72ef54aee35b1097b09b3eaf934a35645e1af4 100644 --- a/docs/docs/api/prune_api.md +++ b/docs/docs/api/prune_api.md @@ -12,7 +12,7 @@ paddleslim.prune.Pruner(criterion="l1_norm")[源代码](https://github.com/Paddl **示例代码:** -``` +```python from paddleslim.prune import Pruner pruner = Pruner() ``` @@ -28,7 +28,7 @@ paddleslim.prune.Pruner.prune(program, scope, params, ratios, place=None, lazy=F - **scope(paddle.fluid.Scope)** - 要裁剪的权重所在的`scope`,Paddle中用`scope`实例存放模型参数和运行时变量的值。Scope中的参数值会被`inplace`的裁剪。更多介绍请参考[Scope概念介绍]() - **params(list)** - 需要被裁剪的卷积层的参数的名称列表。可以通过以下方式查看模型中所有参数的名称: -``` +```python for block in program.blocks: for param in block.all_parameters(): print("param: {}; shape: {}".format(param.name, param.shape)) @@ -57,7 +57,7 @@ for block in program.blocks: **示例:** 点击[AIStudio](https://aistudio.baidu.com/aistudio/projectDetail/200786)执行以下示例代码。 -``` +```python import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr @@ -149,7 +149,7 @@ paddleslim.prune.sensitivity(program, place, param_names, eval_func, sensitiviti - **param_names(list)** - 待分析的卷积层的参数的名称列表。可以通过以下方式查看模型中所有参数的名称: -``` +```python for block in program.blocks: for param in block.all_parameters(): print("param: {}; shape: {}".format(param.name, param.shape)) @@ -165,7 +165,7 @@ for block in program.blocks: - **sensitivities(dict)** - 存放敏感度信息的dict,其格式为: -``` +```python {"weight_0": {0.1: 0.22, 0.2: 0.33 @@ -183,7 +183,7 @@ for block in program.blocks: 点击[AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/201401)运行以下示例代码。 -``` +```python import paddle import numpy as np import paddle.fluid as fluid @@ -291,7 +291,7 @@ paddleslim.prune.merge_sensitive(sensitivities)[源代码](https://github.com/Pa - **sensitivities(dict)** - 合并后的敏感度信息。其格式为: -``` +```bash {"weight_0": {0.1: 0.22, 0.2: 0.33 diff --git a/docs/docs/api/quantization_api.md b/docs/docs/api/quantization_api.md index 1169308841d33c50ff8f6fef47a74eee9d202e0a..a6ba5ab21a066c11f4077b882b5a5aa7f50d969a 100644 --- a/docs/docs/api/quantization_api.md +++ b/docs/docs/api/quantization_api.md @@ -3,7 +3,7 @@ ## 量化配置 通过字典配置量化参数 -``` +```python TENSORRT_OP_TYPES = [ 'mul', 'conv2d', 'pool2d', 'depthwise_conv2d', 'elementwise_add', 'leaky_relu' diff --git a/docs/docs/index.md b/docs/docs/index.md index 1c0f22919759f6bf2d0d8f980e18079ff1f3a220..57fb6d19666a6512df1c00a6609bf86a5646f9fa 100644 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -32,7 +32,7 @@ PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领 - 安装develop版本 -``` +```bash git clone https://github.com/PaddlePaddle/PaddleSlim.git cd PaddleSlim python setup.py install @@ -40,7 +40,7 @@ python setup.py install - 安装官方发布的最新版本 -``` +```bash pip install paddleslim -i https://pypi.org/simple ``` diff --git a/docs/docs/table_latency.md b/docs/docs/table_latency.md index cc8d524b0999eed2d3c030f87a3f6d0cd4c246a5..125e2ad87712f1e5bbaec847ec15c2840b25ae40 100644 --- a/docs/docs/table_latency.md +++ b/docs/docs/table_latency.md @@ -32,7 +32,7 @@ **格式** -``` +```text op_type,flag_bias,flag_relu,n_in,c_in,h_in,w_in,c_out,groups,kernel,padding,stride,dilation\tlatency ``` @@ -57,7 +57,7 @@ op_type,flag_bias,flag_relu,n_in,c_in,h_in,w_in,c_out,groups,kernel,padding,stri **格式** -``` +```text op_type,n_in,c_in,h_in,w_in\tlatency ``` @@ -74,7 +74,7 @@ op_type,n_in,c_in,h_in,w_in\tlatency **格式** -``` +```text op_type,active_type,n_in,c_in,h_in,w_in\tlatency ``` @@ -92,7 +92,7 @@ op_type,active_type,n_in,c_in,h_in,w_in\tlatency **格式** -``` +```text op_type,n_in,c_in,h_in,w_in\tlatency ``` @@ -109,7 +109,7 @@ op_type,n_in,c_in,h_in,w_in\tlatency **格式** -``` +```text op_type,flag_global_pooling,n_in,c_in,h_in,w_in,kernel,padding,stride,ceil_mode,pool_type\tlatency ``` @@ -132,7 +132,7 @@ op_type,flag_global_pooling,n_in,c_in,h_in,w_in,kernel,padding,stride,ceil_mode, **格式** -``` +```text op_type,axis,n_in,c_in,h_in,w_in\tlatency ``` diff --git a/docs/docs/tutorials/nas_demo.md b/docs/docs/tutorials/nas_demo.md index f28dbd71dba67703935598699935d9a91fd60c3f..e6ae97bc1a86f20eaebf7f62c3558dbace34208d 100644 --- a/docs/docs/tutorials/nas_demo.md +++ b/docs/docs/tutorials/nas_demo.md @@ -7,13 +7,13 @@ ### 1. 配置搜索空间 详细的搜索空间配置可以参考神经网络搜索API文档。 -``` +```python config = [('MobileNetV2Space')] ``` ### 2. 利用搜索空间初始化SANAS实例 -``` +```python from paddleslim.nas import SANAS sa_nas = SANAS( @@ -27,12 +27,12 @@ sa_nas = SANAS( ``` ### 3. 根据实例化的NAS得到当前的网络结构 -``` +```python archs = sa_nas.next_archs() ``` ### 4. 根据得到的网络结构和输入构造训练和测试program -``` +```python import paddle.fluid as fluid train_program = fluid.Program() @@ -57,7 +57,7 @@ with fluid.program_guard(train_program, startup_program): ``` ### 5. 根据构造的训练program添加限制条件 -``` +```python from paddleslim.analysis import flops if flops(train_program) > 321208544: @@ -65,6 +65,6 @@ if flops(train_program) > 321208544: ``` ### 6. 回传score -``` +```python sa_nas.reward(score) ``` diff --git a/docs/docs/tutorials/pruning_demo.md b/docs/docs/tutorials/pruning_demo.md index 1c97bff599d1b484669601d0611fb5d9bd6f3718..4f6b3c86082b113f7913b012e933fdb30390d6f5 100755 --- a/docs/docs/tutorials/pruning_demo.md +++ b/docs/docs/tutorials/pruning_demo.md @@ -17,7 +17,7 @@ 不同模型的参数命名不同,在剪裁前需要确定待裁卷积层的参数名称。可通过以下方法列出所有参数名: -``` +```python for param in program.global_block().all_parameters(): print("param name: {}; shape: {}".format(param.name, param.shape)) ``` @@ -28,7 +28,7 @@ for param in program.global_block().all_parameters(): 通过以下命令启动裁剪任务: -``` +```python export CUDA_VISIBLE_DEVICES=0 python train.py ``` diff --git a/docs/docs/tutorials/quant_aware_demo.md b/docs/docs/tutorials/quant_aware_demo.md index 5fae50c5ff752c36863bfa57a9a9f08135b90f00..5f80d7367ad28393141d22ef004e192f23db8611 100644 --- a/docs/docs/tutorials/quant_aware_demo.md +++ b/docs/docs/tutorials/quant_aware_demo.md @@ -10,7 +10,7 @@ ### 1. 配置量化参数 -``` +```python quant_config = { 'weight_quantize_type': 'abs_max', 'activation_quantize_type': 'moving_average_abs_max', @@ -27,7 +27,7 @@ quant_config = { ### 2. 对训练和测试program插入可训练量化op -``` +```python val_program = quant_aware(val_program, place, quant_config, scope=None, for_test=True) compiled_train_prog = quant_aware(train_prog, place, quant_config, scope=None, for_test=False) @@ -35,7 +35,7 @@ compiled_train_prog = quant_aware(train_prog, place, quant_config, scope=None, f ### 3.关掉指定build策略 -``` +```python build_strategy = fluid.BuildStrategy() build_strategy.fuse_all_reduce_ops = False build_strategy.sync_batch_norm = False @@ -48,7 +48,7 @@ compiled_train_prog = compiled_train_prog.with_data_parallel( ### 4. freeze program -``` +```python float_program, int8_program = convert(val_program, place, quant_config, @@ -58,7 +58,7 @@ float_program, int8_program = convert(val_program, ### 5.保存预测模型 -``` +```python fluid.io.save_inference_model( dirname=float_path, feeded_var_names=[image.name], diff --git a/docs/docs/tutorials/quant_embedding_demo.md b/docs/docs/tutorials/quant_embedding_demo.md index 422ef5b6ecbf96a356dfb6e8943d2863f6da5e23..da206d6dc7d1f7e5f8357c6b136c56a249b51604 100755 --- a/docs/docs/tutorials/quant_embedding_demo.md +++ b/docs/docs/tutorials/quant_embedding_demo.md @@ -81,7 +81,7 @@ mv text data/ 词典格式: 词<空格>词频。注意低频词用'UNK'表示 可以按格式自建词典,如果自建词典跳过第一步。 -``` +```text the 1061396 of 593677 and 416629 @@ -126,7 +126,7 @@ sh cluster_train.sh ``` 本示例中按照单机多线程训练的命令进行训练,训练完毕后,可看到在当前文件夹下保存模型的路径为: ``v1_cpu5_b100_lr1dir``, 运行 ``ls v1_cpu5_b100_lr1dir``可看到该文件夹下保存了训练的10个epoch的模型文件。 -``` +```text pass-0 pass-1 pass-2 pass-3 pass-4 pass-5 pass-6 pass-7 pass-8 pass-9 ``` @@ -145,7 +145,7 @@ wget https://paddlerec.bj.bcebos.com/word2vec/test_mid_dir.tar python infer.py --infer_epoch --test_dir data/test_mid_dir --dict_path data/test_build_dict_word_to_id_ --batch_size 20000 --model_dir v1_cpu5_b100_lr1dir/ --start_index 0 --last_index 9 ``` 运行该预测命令, 可看到如下输出 -``` +```text ('start index: ', 0, ' last_index:', 9) ('vocab_size:', 63642) step:1 249 @@ -173,7 +173,7 @@ epoch:9 acc:0.153 ## 量化``基于skip-gram的word2vector模型`` 量化配置为: -``` +```python config = { 'params_name': 'emb', 'quantize_type': 'abs_max' @@ -188,7 +188,7 @@ python infer.py --infer_epoch --test_dir data/test_mid_dir --dict_path data/test 运行输出为: -``` +```text ('start index: ', 0, ' last_index:', 9) ('vocab_size:', 63642) quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'} diff --git a/docs/docs/tutorials/quant_post_demo.md b/docs/docs/tutorials/quant_post_demo.md index 72cd68781d6de71aca19d3b34f1daf187494f371..5e794a90baeb3f20f6a42322b1ac4fbe689bad5d 100755 --- a/docs/docs/tutorials/quant_post_demo.md +++ b/docs/docs/tutorials/quant_post_demo.md @@ -25,14 +25,14 @@ ### 导出模型 通过运行以下命令可将模型转化为离线量化接口可用的模型: -``` +```bash python export_model.py --model "MobileNet" --pretrained_model ./pretrain/MobileNetV1_pretrained --data imagenet ``` 转化之后的模型存储在``inference_model/MobileNet/``文件夹下,可看到该文件夹下有``'model'``, ``'weights'``两个文件。 ### 离线量化 接下来对导出的模型文件进行离线量化,离线量化的脚本为[quant_post.py](./quant_post.py),脚本中使用接口``paddleslim.quant.quant_post``对模型进行离线量化。运行命令为: -``` +```bash python quant_post.py --model_path ./inference_model/MobileNet --save_path ./quant_model_train/MobileNet --model_filename model --params_filename weights ``` @@ -51,22 +51,22 @@ python quant_post.py --model_path ./inference_model/MobileNet --save_path ./quan 使用[eval.py](./eval.py)脚本对量化前后的模型进行测试,得到模型的分类精度进行对比。 首先测试量化前的模型的精度,运行以下命令: -``` +```bash python eval.py --model_path ./inference_model/MobileNet --model_name model --params_name weights ``` 精度输出为: -``` +```text top1_acc/top5_acc= [0.70913923 0.89548034] ``` 使用以下命令测试离线量化后的模型的精度: -``` +```bash python eval.py --model_path ./quant_model_train/MobileNet ``` 精度输出为 -``` +```text top1_acc/top5_acc= [0.70141864 0.89086477] ``` 从以上精度对比可以看出,对``mobilenet``在``imagenet``上的分类模型进行离线量化后 ``top1``精度损失为``0.77%``, ``top5``精度损失为``0.46%``. diff --git a/docs/docs/tutorials/sensitivity_demo.md b/docs/docs/tutorials/sensitivity_demo.md index 920c98ef34523f98577265022b3b2888dbeb61c9..b8b2688f2236b5ec1e98a5a560fe6cbca9942923 100644 --- a/docs/docs/tutorials/sensitivity_demo.md +++ b/docs/docs/tutorials/sensitivity_demo.md @@ -18,7 +18,7 @@ 在路径`PaddleSlim/demo/sensitive`下执行以下代码运行示例: -``` +```bash export CUDA_VISIBLE_DEVICES=0 python train.py --model "MobileNetV1" ``` @@ -37,7 +37,7 @@ python train.py --model "MobileNetV1" 代码如下: -``` +```python # 进程1 sensitivity( val_program, @@ -48,7 +48,7 @@ sensitivity( pruned_ratios=[0.1, 0.2, 0.3, 0.4]) ``` -``` +```python # 进程2 sensitivity( val_program, @@ -64,7 +64,7 @@ sensitivity( 如果用户通过上一节多进程的方式生成了多个存储敏感度信息的文件,可以通过`paddleslim.prune.merge_sensitive`将其合并,合并后的敏感度信息存储在一个`dict`中。代码如下: -``` +```python sens = merge_sensitive(["./sensitivities_0.data", "./sensitivities_1.data"]) ``` @@ -72,7 +72,7 @@ sens = merge_sensitive(["./sensitivities_0.data", "./sensitivities_1.data"]) 调用`paddleslim.prune.get_ratios_by_loss`接口计算一组剪裁率。 -``` +```python ratios = get_ratios_by_loss(sens, 0.01) ``` diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 8113103ff942d92c1c4f2ee174d1dcb22630edf6..eb11f37c849bcc8f3ee0c5316c4b8664e4817f07 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -29,7 +29,6 @@ extra_css: markdown_extensions: - admonition - codehilite: - guess_lang: true linenums: true - toc: permalink: "#"