提交 405a4686 编写于 作者: K Kaipeng Deng 提交者: Bai Yifan

disable guess_language in codehighlite (#43)

上级 8ba6f926
......@@ -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
......
......@@ -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()
......
......@@ -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<str>)** - 需要被裁剪的卷积层的参数的名称列表。可以通过以下方式查看模型中所有参数的名称:
```
```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<str>)** - 待分析的卷积层的参数的名称列表。可以通过以下方式查看模型中所有参数的名称:
```
```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
......
......@@ -3,7 +3,7 @@
## 量化配置
通过字典配置量化参数
```
```python
TENSORRT_OP_TYPES = [
'mul', 'conv2d', 'pool2d', 'depthwise_conv2d', 'elementwise_add',
'leaky_relu'
......
......@@ -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
```
......
......@@ -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
```
......
......@@ -7,13 +7,13 @@
### 1. 配置搜索空间
详细的搜索空间配置可以参考<a href='../../../paddleslim/nas/nas_api.md'>神经网络搜索API文档</a>
```
```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)
```
......@@ -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
```
......
......@@ -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],
......
......@@ -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'}
......
......@@ -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%``.
......@@ -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)
```
......
......@@ -29,7 +29,6 @@ extra_css:
markdown_extensions:
- admonition
- codehilite:
guess_lang: true
linenums: true
- toc:
permalink: "#"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册