未验证 提交 d80ed89f 编写于 作者: W whs 提交者: GitHub

Restructure document by sphinx. (#88)

上级 043d3bf4
# PaddleSlim文档构建与发布教程
## 1. 文档构成
PaddleSlim文档包含以下部分:
- 简介:概要介绍PaddleSlim功能。
- 安装:安装文档。
- 快速开始:各个策略示例,使用小数据,可快速完成执行。
- 高阶教程:包括在实际任务上的操作步骤、高级特性的使用教程。
- API文档:用户接口文档。
以上文档均包含中英两版,其中,**英文API文档根据代码注释自动生成**
文档文件结构如下:
```bash
docs
├── en
│   ├── api_en # 英文API文档,该目录下文件为自动生成
│   ├── conf.py # 英文文档编译配置文件
│   ├── index_en.rst # 英文文档总导航页
│   ├── index.rst # 中英文切换功能的辅助文件,无实际内容
│   ├── install_en.md # 安装文档
│   ├── intro_en.md # 简介
│   ├── Makefile # 英文文档编译文件
│   ├── model_zoo_en.md # 模型库
│   ├── quick_start # 快速开始
│   └── tutorials # 进阶教程
├── requirements.txt # 文档编译所需依赖
└── zh_cn
├── algo # 算法原理
├── api_cn # 中文API文档
├── conf.py # 中文文档编译配置文件
├── index_en.rst # 中英文切换功能的辅助文件,无实际内容
├── index.rst # 中文文档总导航页
├── install.md # 安装文档
├── intro.md # 简介
├── Makefile # 编译文件
├── model_zoo.md # 模型库
├── quick_start # 快速开始
└── tutorials # 进阶教程
```
## 2. 文档编译
编译文档前需要确保已正确安装PaddleSlim,且Python可正常执行`import paddleslim`
执行以下命令安装文档编译依赖工具库:
```
pip install -r requirements.txt
```
## 2.1 编译中文文档
进入路径`./docs/zh_cn`
执行以下命令清理编译结果:
```
make clean
```
执行以下命令编译生成`html`:
```
make html
```
以上命令生成`html`文件到路径`./build/html/`
## 2.2 编译英文文档
进入路径`PaddleSlim/docs/en`
编译文档前,需要先从代码注释生成API文档。
### 2.2.1 自动生成API
```
sphinx-apidoc -M -o api_en/ ../../paddleslim
```
如果有新增`package`,请将其天剑到`./api_en/index_en.rst`文件中。
### 2.2.2 编译文档
与2.1节步骤一样。
# 3. 发布页面到Github
回到路径`PaddleSlim/`
切换分支到`gh-pages`:
```
git checkout gh-pages
```
>注:直接切换到gh-pages分支可能会出现异常,可以尝试先切换到develop分支,再切到gh-pages分支。
```
rm docs/en/build/html/index.html
rm docs/zh_cn/build/html/index_en.html
cp -rf docs/en/build/html/* ./
cp -rf docs/zh_cn/build/html/* ./
```
执行以下命令,添加更新:
```
git add -u
```
如果有新增html页面,需要单独对其执行`git add`
提交commit,并push到github。
```
git commit -m "update pages"
git push origin gh-pages
```
# 4. 其它
英文API文档格式请参考:https://wanghaoshuang.github.io/PaddleSlim/api_en/paddleslim.analysis.html
# PaddleSlim API文档导航
## [模型分析](./analysis_api.md)
## [卷积通道剪裁](./prune_api.md)
## [蒸馏]()
- [单进程蒸馏](./single_distiller_api.md)
- [大规模可扩展知识蒸馏框架 Pantheon](./pantheon_api.md)
- [通道剪裁](./prune_api.md)
### [量化](./quantization_api.md)
- [量化训练](./quantization_api.md#量化训练API)
- [离线量化](./quantization_api.md#离线量化API)
- [embedding量化](./quantization_api.md#Embedding量化API)
## [小模型结构搜索]()
- [nas API](./nas_api.md)
- [SearchSpace](./search_space.md)
.codehilite code, .codehilite pre{color:#3F3F3F;background-color:#F7F7F7;
overflow: auto;
box-sizing: border-box;
padding: 0.01em 16px;
padding-top: 0.5em;
padding-right-value: 16px;
padding-bottom: 0.5em;
padding-left-value: 16px;
padding-left-ltr-source: physical;
padding-left-rtl-source: physical;
padding-right-ltr-source: physical;
padding-right-rtl-source: physical;
border-radius: 10px !important;
border-top-left-radius: 16px;
border-top-right-radius: 16px;
border-bottom-right-radius: 16px;
border-bottom-left-radius: 16px;
border: 2px solid #CCC !important;
border-top-width: 1px;
border-right-width-value: 1px;
border-right-width-ltr-source: physical;
border-right-width-rtl-source: physical;
border-bottom-width: 1px;
border-left-width-value: 1px;
border-left-width-ltr-source: physical;
border-left-width-rtl-source: physical;
border-top-style: solid;
border-right-style-value: solid;
border-right-style-ltr-source: physical;
border-right-style-rtl-source: physical;
border-bottom-style: solid;
border-left-style-value: solid;
border-left-style-ltr-source: physical;
border-left-style-rtl-source: physical;
border-top-color: #CCC;
border-right-color-value: #CCC;
border-right-color-ltr-source: physical;
border-right-color-rtl-source: physical;
border-bottom-color: #CCC;
border-left-color-value: #CCC;
border-left-color-ltr-source: physical;
border-left-color-rtl-source: physical;
-moz-border-top-colors: none;
-moz-border-right-colors: none;
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
border-image-source: none;
border-image-slice: 100% 100% 100% 100%;
border-image-width: 1 1 1 1;
border-image-outset: 0 0 0 0;
border-image-repeat: stretch stretch;}
.codehilite .hll { background-color: #ffffcc }
.codehilite .c { color: #999988; font-style: italic } /* Comment */
.codehilite .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.codehilite .k { color: #008800; font-weight: bold } /* Keyword */
.codehilite .o { color: #000000; font-weight: bold } /* Operator */
.codehilite .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.codehilite .cp { color: #999999; font-weight: bold; font-style: italic } /* Comment.Preproc */
.codehilite .c1 { color: #999988; font-style: italic } /* Comment.Single */
.codehilite .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.codehilite .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.codehilite .ge { color: #000000; font-style: italic } /* Generic.Emph */
.codehilite .gr { color: #aa0000 } /* Generic.Error */
.codehilite .gh { color: #999999 } /* Generic.Heading */
.codehilite .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.codehilite .go { color: #888888 } /* Generic.Output */
.codehilite .gp { color: #555555 } /* Generic.Prompt */
.codehilite .gs { font-weight: bold } /* Generic.Strong */
.codehilite .gu { color: #aaaaaa } /* Generic.Subheading */
.codehilite .gt { color: #aa0000 } /* Generic.Traceback */
.codehilite .kc { color: #000000; font-weight: bold } /* Keyword.Constant */
.codehilite .kd { color: #000000; font-weight: bold } /* Keyword.Declaration */
.codehilite .kn { color: #000000; font-weight: bold } /* Keyword.Namespace */
.codehilite .kp { color: #000000; font-weight: bold } /* Keyword.Pseudo */
.codehilite .kr { color: #000000; font-weight: bold } /* Keyword.Reserved */
.codehilite .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.codehilite .m { color: #009999 } /* Literal.Number */
.codehilite .s { color: #d01040 } /* Literal.String */
.codehilite .na { color: #008080 } /* Name.Attribute */
.codehilite .nb { color: #0086B3 } /* Name.Builtin */
.codehilite .nc { color: #445588; font-weight: bold } /* Name.Class */
.codehilite .no { color: #008080 } /* Name.Constant */
.codehilite .nd { color: #3c5d5d; font-weight: bold } /* Name.Decorator */
.codehilite .ni { color: #800080 } /* Name.Entity */
.codehilite .ne { color: #990000; font-weight: bold } /* Name.Exception */
.codehilite .nf { color: #990000; font-weight: bold } /* Name.Function */
.codehilite .nl { color: #990000; font-weight: bold } /* Name.Label */
.codehilite .nn { color: #555555 } /* Name.Namespace */
.codehilite .nt { color: #000080 } /* Name.Tag */
.codehilite .nv { color: #008080 } /* Name.Variable */
.codehilite .ow { color: #000000; font-weight: bold } /* Operator.Word */
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */
.codehilite .mf { color: #009999 } /* Literal.Number.Float */
.codehilite .mh { color: #009999 } /* Literal.Number.Hex */
.codehilite .mi { color: #009999 } /* Literal.Number.Integer */
.codehilite .mo { color: #009999 } /* Literal.Number.Oct */
.codehilite .sb { color: #d01040 } /* Literal.String.Backtick */
.codehilite .sc { color: #d01040 } /* Literal.String.Char */
.codehilite .sd { color: #d01040 } /* Literal.String.Doc */
.codehilite .s2 { color: #d01040 } /* Literal.String.Double */
.codehilite .se { color: #d01040 } /* Literal.String.Escape */
.codehilite .sh { color: #d01040 } /* Literal.String.Heredoc */
.codehilite .si { color: #d01040 } /* Literal.String.Interpol */
.codehilite .sx { color: #d01040 } /* Literal.String.Other */
.codehilite .sr { color: #009926 } /* Literal.String.Regex */
.codehilite .s1 { color: #d01040 } /* Literal.String.Single */
.codehilite .ss { color: #990073 } /* Literal.String.Symbol */
.codehilite .bp { color: #999999 } /* Name.Builtin.Pseudo */
.codehilite .vc { color: #008080 } /* Name.Variable.Class */
.codehilite .vg { color: #008080 } /* Name.Variable.Global */
.codehilite .vi { color: #008080 } /* Name.Variable.Instance */
.codehilite .il { color: #009999 } /* Literal.Number.Integer.Long */
## [蒸馏](../demo/distillation/distillation_demo.py)
蒸馏demo默认使用ResNet50作为teacher网络,MobileNet作为student网络,此外还支持将teacher和student换成[models目录](../demo/models)支持的任意模型。
demo中对teahcer模型和student模型的一层特征图添加了l2_loss的蒸馏损失函数,使用时也可根据需要选择fsp_loss, soft_label_loss以及自定义的loss函数。
训练默认使用的是cifar10数据集,piecewise_decay学习率衰减策略,momentum优化器进行120轮蒸馏训练。使用者也可以简单地用args参数切换为使用ImageNet数据集,cosine_decay学习率衰减策略等其他训练配置。
## 量化
### [量化训练demo文档](./quant_aware_demo.md)
### [离线量化demo文档](./quant_post_demo.md)
### [Embedding量化demo文档](./quant_embedding_demo.md)
## NAS
### [NAS示例](./nas_demo.md)
本示例将介绍如何使用PaddleSlim蒸馏接口来对模型进行蒸馏训练。
## 接口介绍
请参考[蒸馏API文档](https://paddlepaddle.github.io/PaddleSlim/api/single_distiller_api/)
## PaddleSlim蒸馏训练流程
一般情况下,模型参数量越多,结构越复杂,其性能越好,但运算量和资源消耗也越大。**知识蒸馏** 就是一种将大模型学习到的有用信息(Dark Knowledge)压缩进更小更快的模型,而获得可以匹敌大模型结果的方法。
在本示例中精度较高的大模型被称为teacher,精度稍逊但速度更快的小模型被称为student。
### 1. 定义student_program
```python
student_program = fluid.Program()
student_startup = fluid.Program()
with fluid.program_guard(student_program, student_startup):
image = fluid.data(
name='image', shape=[None] + [3, 224, 224], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
# student model definition
model = MobileNet()
out = model.net(input=image, class_dim=1000)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
```
### 2. 定义teacher_program
在定义好`teacher_program`后,可以一并加载训练好的pretrained_model。
`teacher_program`内需要加上`with fluid.unique_name.guard():`,保证teacher的变量命名不被`student_program`影响,从而能够正确地加载预训练参数。
```python
teacher_program = fluid.Program()
teacher_startup = fluid.Program()
with fluid.program_guard(teacher_program, teacher_startup):
with fluid.unique_name.guard():
image = fluid.data(
name='data', shape=[None] + [3, 224, 224], dtype='float32')
# teacher model definition
teacher_model = ResNet()
predict = teacher_model.net(image, class_dim=1000)
exe.run(teacher_startup)
def if_exist(var):
return os.path.exists(
os.path.join("./pretrained", var.name)
fluid.io.load_vars(
exe,
"./pretrained",
main_program=teacher_program,
predicate=if_exist)
```
### 3.选择特征图
定义好`student_program``teacher_program`后,我们需要从中两两对应地挑选出若干个特征图,留待后续为其添加知识蒸馏损失函数。
```python
# get all student variables
student_vars = []
for v in student_program.list_vars():
try:
student_vars.append((v.name, v.shape))
except:
pass
print("="*50+"student_model_vars"+"="*50)
print(student_vars)
# get all teacher variables
teacher_vars = []
for v in teacher_program.list_vars():
try:
teacher_vars.append((v.name, v.shape))
except:
pass
print("="*50+"teacher_model_vars"+"="*50)
print(teacher_vars)
```
### 4. 合并Program(merge)
PaddlePaddle使用Program来描述计算图,为了同时计算student和teacher两个Program,这里需要将其两者合并(merge)为一个Program。
merge过程操作较多,具体细节请参考[merge API文档](https://paddlepaddle.github.io/PaddleSlim/api/single_distiller_api/#merge)
```python
data_name_map = {'data': 'image'}
merge(teacher_program, student_program, data_name_map, place)
```
### 5.添加蒸馏loss
在添加蒸馏loss的过程中,可能还会引入部分变量(Variable),为了避免命名重复这里可以使用`with fluid.name_scope("distill"):`为新引入的变量加一个命名作用域。
另外需要注意的是,merge过程为`teacher_program`的变量统一加了名称前缀,默认是`"teacher_"`, 这里在添加`l2_loss`时也要为teacher的变量加上这个前缀。
```python
with fluid.program_guard(student_program, student_startup):
with fluid.name_scope("distill"):
distill_loss = l2_loss('teacher_bn5c_branch2b.output.1.tmp_3',
'depthwise_conv2d_11.tmp_0', student_program)
distill_weight = 1
loss = avg_cost + distill_loss * distill_weight
opt = create_optimizer()
opt.minimize(loss)
exe.run(student_startup)
```
至此,我们就得到了用于蒸馏训练的`student_program`,后面就可以使用一个普通program一样对其开始训练和评估。
# 网络结构搜索示例
本示例介绍如何使用网络结构搜索接口,搜索到一个更小或者精度更高的模型,该文档仅介绍paddleslim中SANAS的使用及如何利用SANAS得到模型结构,完整示例代码请参考sa_nas_mobilenetv2.py或者block_sa_nas_mobilenetv2.py。
## 接口介绍
请参考[神经网络搜索API介绍](https://paddlepaddle.github.io/PaddleSlim/api/nas_api/)
### 1. 配置搜索空间
详细的搜索空间配置可以参考[搜索空间介绍](https://paddlepaddle.github.io/PaddleSlim/search_space/)
```python
config = [('MobileNetV2Space')]
```
### 2. 利用搜索空间初始化SANAS实例
```python
from paddleslim.nas import SANAS
sa_nas = SANAS(
config,
server_addr=("", 8881),
init_temperature=10.24,
reduce_rate=0.85,
search_steps=300,
is_server=True)
```
### 3. 根据实例化的NAS得到当前的网络结构
```python
archs = sa_nas.next_archs()
```
### 4. 根据得到的网络结构和输入构造训练和测试program
```python
import paddle.fluid as fluid
train_program = fluid.Program()
test_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(train_program, startup_program):
data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
for arch in archs:
data = arch(data)
output = fluid.layers.fc(data, 10)
softmax_out = fluid.layers.softmax(input=output, use_cudnn=False)
cost = fluid.layers.cross_entropy(input=softmax_out, label=label)
avg_cost = fluid.layers.mean(cost)
acc_top1 = fluid.layers.accuracy(input=softmax_out, label=label, k=1)
test_program = train_program.clone(for_test=True)
sgd = fluid.optimizer.SGD(learning_rate=1e-3)
sgd.minimize(avg_cost)
```
### 5. 根据构造的训练program添加限制条件
```python
from paddleslim.analysis import flops
if flops(train_program) > 321208544:
continue
```
### 6. 回传score
```python
sa_nas.reward(score)
```
# One Shot NAS 示例
>该示例依赖Paddle1.7.0或Paddle develop版本。
该示例使用MNIST数据,介绍了如何使用PaddleSlim的OneShotNAS接口搜索出一个分类网络。OneShotNAS仅支持动态图,所以该示例完全使用Paddle动态图模式。
## 关键代码介绍
One-shot网络结构搜索策略包含以下步骤:
1. 定义超网络
2. 训练超网络
3. 基于超网络搜索子网络
4. 训练最佳子网络
以下按序介绍各个步骤的关键代码。
### 定义超级网络
按照动态图教程,定义一个分类网络模块,该模块包含4个子模块:`_simple_img_conv_pool_1`,`_simple_img_conv_pool_2`,`super_net``fc`,其中`super_net``SuperMnasnet`的一个实例。
在前向计算过程中,输入图像先后经过子模块`_simple_img_conv_pool_1``super_net``_simple_img_conv_pool_2``fc`的前向计算。
代码如下所示:
```
class MNIST(fluid.dygraph.Layer):
def __init__(self):
super(MNIST, self).__init__()
self._simple_img_conv_pool_1 = SimpleImgConv(1, 20, 2, act="relu")
self.arch = SuperMnasnet(
name_scope="super_net", input_channels=20, out_channels=20)
self._simple_img_conv_pool_2 = SimpleImgConv(20, 50, 2, act="relu")
self.pool_2_shape = 50 * 13 * 13
SIZE = 10
scale = (2.0 / (self.pool_2_shape**2 * SIZE))**0.5
self._fc = Linear(
self.pool_2_shape,
10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale)),
act="softmax")
def forward(self, inputs, label=None, tokens=None):
x = self._simple_img_conv_pool_1(inputs)
x = self.arch(x, tokens=tokens) # addddddd
x = self._simple_img_conv_pool_2(x)
x = fluid.layers.reshape(x, shape=[-1, self.pool_2_shape])
x = self._fc(x)
if label is not None:
acc = fluid.layers.accuracy(input=x, label=label)
return x, acc
else:
return x
```
动态图模块MNIST的forward函数接受一个参数`tokens`,用于指定在前向计算中使用的子网络,如果`tokens`为None,则随机选取一个子网络进行前向计算。
### 训练超级网络
网络训练的逻辑定义在`train_mnist`函数中,将`tokens`参数设置为None,进行超网络训练,即在每个batch选取一个超网络进行训练。
代码如下所示:
```
with fluid.dygraph.guard(place):
model = MNIST()
train_mnist(args, model)
```
### 搜索最佳子网络
使用PaddleSlim提供的`OneShotSearch`接口搜索最佳子网络。传入已定义且训练好的超网络实例`model`和一个用于评估子网络的回调函数`test_mnist`.
代码如下:
```
best_tokens = OneShotSearch(model, test_mnist)
```
### 训练最佳子网络
获得最佳的子网络的编码`best_tokens`后,调用之前定义的`train_mnist`方法进行子网络的训练。代码如下:
```
train_mnist(args, model, best_tokens)
```
## 启动示例
执行以下代码运行示例:
```
python train.py
```
执行`python train.py --help`查看更多可配置选项。
## FAQ
# 在线量化示例
本示例介绍如何使用在线量化接口,来对训练好的分类模型进行量化, 可以减少模型的存储空间和显存占用。
## 接口介绍
请参考 <a href='../../../paddleslim/quant/quantization_api_doc.md'>量化API文档</a>
## 分类模型的离线量化流程
### 1. 配置量化参数
```python
quant_config = {
'weight_quantize_type': 'abs_max',
'activation_quantize_type': 'moving_average_abs_max',
'weight_bits': 8,
'activation_bits': 8,
'not_quant_pattern': ['skip_quant'],
'quantize_op_types': ['conv2d', 'depthwise_conv2d', 'mul'],
'dtype': 'int8',
'window_size': 10000,
'moving_rate': 0.9,
'quant_weight_only': False
}
```
### 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)
```
### 3.关掉指定build策略
```python
build_strategy = fluid.BuildStrategy()
build_strategy.fuse_all_reduce_ops = False
build_strategy.sync_batch_norm = False
exec_strategy = fluid.ExecutionStrategy()
compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name,
build_strategy=build_strategy,
exec_strategy=exec_strategy)
```
### 4. freeze program
```python
float_program, int8_program = convert(val_program,
place,
quant_config,
scope=None,
save_int8=True)
```
### 5.保存预测模型
```python
fluid.io.save_inference_model(
dirname=float_path,
feeded_var_names=[image.name],
target_vars=[out], executor=exe,
main_program=float_program,
model_filename=float_path + '/model',
params_filename=float_path + '/params')
fluid.io.save_inference_model(
dirname=int8_path,
feeded_var_names=[image.name],
target_vars=[out], executor=exe,
main_program=int8_program,
model_filename=int8_path + '/model',
params_filename=int8_path + '/params')
```
# Embedding量化示例
本示例介绍如何使用Embedding量化的接口 [paddleslim.quant.quant_embedding]()``quant_embedding``接口将网络中的Embedding参数从``float32``类型量化到 ``8-bit``整数类型,在几乎不损失模型精度的情况下减少模型的存储空间和显存占用。
接口介绍请参考 <a href='../../../paddleslim/quant/quantization_api_doc.md'>量化API文档</a>
该接口对program的修改:
量化前:
<p align="center">
<img src="./image/before.png" height=200 width=100 hspace='10'/> <br />
<strong>图1:量化前的模型结构</strong>
</p>
量化后:
<p align="center">
<img src="./image/after.png" height=300 width=300 hspace='10'/> <br />
<strong>图2: 量化后的模型结构</strong>
</p>
以下将以 ``基于skip-gram的word2vector模型`` 为例来说明如何使用``quant_embedding``接口。首先介绍 ``基于skip-gram的word2vector模型`` 的正常训练和测试流程。
## 基于skip-gram的word2vector模型
以下是本例的简要目录结构及说明:
```text
.
├── cluster_train.py # 分布式训练函数
├── cluster_train.sh # 本地模拟多机脚本
├── train.py # 训练函数
├── infer.py # 预测脚本
├── net.py # 网络结构
├── preprocess.py # 预处理脚本,包括构建词典和预处理文本
├── reader.py # 训练阶段的文本读写
├── train.py # 训练函数
└── utils.py # 通用函数
```
### 介绍
本例实现了skip-gram模式的word2vector模型。
同时推荐用户参考[ IPython Notebook demo](https://aistudio.baidu.com/aistudio/projectDetail/124377)
### 数据下载
全量数据集使用的是来自1 Billion Word Language Model Benchmark的(http://www.statmt.org/lm-benchmark) 的数据集.
```bash
mkdir data
wget http://www.statmt.org/lm-benchmark/1-billion-word-language-modeling-benchmark-r13output.tar.gz
tar xzvf 1-billion-word-language-modeling-benchmark-r13output.tar.gz
mv 1-billion-word-language-modeling-benchmark-r13output/training-monolingual.tokenized.shuffled/ data/
```
备用数据地址下载命令如下
```bash
mkdir data
wget https://paddlerec.bj.bcebos.com/word2vec/1-billion-word-language-modeling-benchmark-r13output.tar
tar xvf 1-billion-word-language-modeling-benchmark-r13output.tar
mv 1-billion-word-language-modeling-benchmark-r13output/training-monolingual.tokenized.shuffled/ data/
```
为了方便快速验证,我们也提供了经典的text8样例数据集,包含1700w个词。 下载命令如下
```bash
mkdir data
wget https://paddlerec.bj.bcebos.com/word2vec/text.tar
tar xvf text.tar
mv text data/
```
### 数据预处理
以样例数据集为例进行预处理。全量数据集注意解压后以training-monolingual.tokenized.shuffled 目录为预处理目录,和样例数据集的text目录并列。
词典格式: 词<空格>词频。注意低频词用'UNK'表示
可以按格式自建词典,如果自建词典跳过第一步。
```text
the 1061396
of 593677
and 416629
one 411764
in 372201
a 325873
<UNK> 324608
to 316376
zero 264975
nine 250430
```
第一步根据英文语料生成词典,中文语料可以通过修改text_strip方法自定义处理方法。
```bash
python preprocess.py --build_dict --build_dict_corpus_dir data/text/ --dict_path data/test_build_dict
```
第二步根据词典将文本转成id, 同时进行downsample,按照概率过滤常见词, 同时生成word和id映射的文件,文件名为词典+"_word_to_id_"。
```bash
python preprocess.py --filter_corpus --dict_path data/test_build_dict --input_corpus_dir data/text --output_corpus_dir data/convert_text8 --min_count 5 --downsample 0.001
```
### 训练
具体的参数配置可运行
```bash
python train.py -h
```
单机多线程训练
```bash
OPENBLAS_NUM_THREADS=1 CPU_NUM=5 python train.py --train_data_dir data/convert_text8 --dict_path data/test_build_dict --num_passes 10 --batch_size 100 --model_output_dir v1_cpu5_b100_lr1dir --base_lr 1.0 --print_batch 1000 --with_speed --is_sparse
```
本地单机模拟多机训练
```bash
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
```
### 预测
测试集下载命令如下
```bash
#全量数据集测试集
wget https://paddlerec.bj.bcebos.com/word2vec/test_dir.tar
#样本数据集测试集
wget https://paddlerec.bj.bcebos.com/word2vec/test_mid_dir.tar
```
预测命令,注意词典名称需要加后缀"_word_to_id_", 此文件是预处理阶段生成的。
```bash
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
epoch:0 acc:0.014
step:1 590
epoch:1 acc:0.033
step:1 982
epoch:2 acc:0.055
step:1 1338
epoch:3 acc:0.075
step:1 1653
epoch:4 acc:0.093
step:1 1914
epoch:5 acc:0.107
step:1 2204
epoch:6 acc:0.124
step:1 2416
epoch:7 acc:0.136
step:1 2606
epoch:8 acc:0.146
step:1 2722
epoch:9 acc:0.153
```
## 量化``基于skip-gram的word2vector模型``
量化配置为:
```python
config = {
'params_name': 'emb',
'quantize_type': 'abs_max'
}
```
运行命令为:
```bash
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 --emb_quant True
```
运行输出为:
```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'}
step:1 253
epoch:0 acc:0.014
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 586
epoch:1 acc:0.033
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 970
epoch:2 acc:0.054
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 1364
epoch:3 acc:0.077
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 1642
epoch:4 acc:0.092
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 1936
epoch:5 acc:0.109
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 2216
epoch:6 acc:0.124
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 2419
epoch:7 acc:0.136
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 2603
epoch:8 acc:0.146
quant_embedding config {'quantize_type': 'abs_max', 'params_name': 'emb', 'quantize_bits': 8, 'dtype': 'int8'}
step:1 2719
epoch:9 acc:0.153
```
量化后的模型保存在``./output_quant``中,可看到量化后的参数``'emb.int8'``的大小为3.9M, 在``./v1_cpu5_b100_lr1dir``中可看到量化前的参数``'emb'``的大小为16M。
# 离线量化示例
本示例介绍如何使用离线量化接口``paddleslim.quant.quant_post``来对训练好的分类模型进行离线量化, 该接口无需对模型进行训练就可得到量化模型,减少模型的存储空间和显存占用。
## 接口介绍
请参考 <a href='../../../paddleslim/quant/quantization_api_doc.md'>量化API文档</a>
## 分类模型的离线量化流程
### 准备数据
在当前文件夹下创建``data``文件夹,将``imagenet``数据集解压在``data``文件夹下,解压后``data``文件夹下应包含以下文件:
- ``'train'``文件夹,训练图片
- ``'train_list.txt'``文件
- ``'val'``文件夹,验证图片
- ``'val_list.txt'``文件
### 准备需要量化的模型
因为离线量化接口只支持加载通过``fluid.io.save_inference_model``接口保存的模型,因此如果您的模型是通过其他接口保存的,那需要先将模型进行转化。本示例将以分类模型为例进行说明。
首先在[imagenet分类模型](https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/image_classification#%E5%B7%B2%E5%8F%91%E5%B8%83%E6%A8%A1%E5%9E%8B%E5%8F%8A%E5%85%B6%E6%80%A7%E8%83%BD)中下载训练好的``mobilenetv1``模型。
在当前文件夹下创建``'pretrain'``文件夹,将``mobilenetv1``模型在该文件夹下解压,解压后的目录为``pretrain/MobileNetV1_pretrained``
### 导出模型
通过运行以下命令可将模型转化为离线量化接口可用的模型:
```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
```
- ``model_path``: 需要量化的模型坐在的文件夹
- ``save_path``: 量化后的模型保存的路径
- ``model_filename``: 如果需要量化的模型的参数文件保存在一个文件中,则设置为该模型的模型文件名称,如果参数文件保存在多个文件中,则不需要设置。
- ``params_filename``: 如果需要量化的模型的参数文件保存在一个文件中,则设置为该模型的参数文件名称,如果参数文件保存在多个文件中,则不需要设置。
运行以上命令后,可在``${save_path}``下看到量化后的模型文件和参数文件。
> 使用的量化算法为``'KL'``, 使用训练集中的160张图片进行量化参数的校正。
### 测试精度
使用[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%``.
该示例介绍如何分析卷积网络中各卷积层的敏感度,以及如何根据计算出的敏感度选择一组合适的剪裁率。
该示例默认会自动下载并使用MNIST数据。支持以下模型:
- MobileNetV1
- MobileNetV2
- ResNet50
## 1. 接口介绍
该示例涉及以下接口:
- [paddleslim.prune.sensitivity](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#sensitivity)
- [paddleslim.prune.merge_sensitive](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#merge_sensitive)
- [paddleslim.prune.get_ratios_by_loss](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#get_ratios_by_losssensitivities-loss)
## 2. 运行示例
在路径`PaddleSlim/demo/sensitive`下执行以下代码运行示例:
```bash
export CUDA_VISIBLE_DEVICES=0
python train.py --model "MobileNetV1"
```
通过`python train.py --help`查看更多选项。
## 3. 重要步骤说明
### 3.1 计算敏感度
计算敏感度之前,用户需要搭建好用于测试的网络,以及实现评估模型精度的回调函数。
调用`paddleslim.prune.sensitivity`接口计算敏感度。敏感度信息会追加到`sensitivities_file`选项所指定的文件中,如果需要重新计算敏感度,需要先删除`sensitivities_file`文件。
如果模型评估速度较慢,可以通过多进程的方式加速敏感度计算过程。比如在进程1中设置`pruned_ratios=[0.1, 0.2, 0.3, 0.4]`,并将敏感度信息存放在文件`sensitivities_0.data`中,然后在进程2中设置`pruned_ratios=[0.5, 0.6, 0.7]`,并将敏感度信息存储在文件`sensitivities_1.data`中。这样每个进程只会计算指定剪切率下的敏感度信息。多进程可以运行在单机多卡,或多机多卡。
代码如下:
```python
# 进程1
sensitivity(
val_program,
place,
params,
test,
sensitivities_file="sensitivities_0.data",
pruned_ratios=[0.1, 0.2, 0.3, 0.4])
```
```python
# 进程2
sensitivity(
val_program,
place,
params,
test,
sensitivities_file="sensitivities_1.data",
pruned_ratios=[0.5, 0.6, 0.7])
```
### 3.2 合并敏感度
如果用户通过上一节多进程的方式生成了多个存储敏感度信息的文件,可以通过`paddleslim.prune.merge_sensitive`将其合并,合并后的敏感度信息存储在一个`dict`中。代码如下:
```python
sens = merge_sensitive(["./sensitivities_0.data", "./sensitivities_1.data"])
```
### 3.3 计算剪裁率
调用`paddleslim.prune.get_ratios_by_loss`接口计算一组剪裁率。
```python
ratios = get_ratios_by_loss(sens, 0.01)
```
其中,`0.01`为一个阈值,对于任意卷积层,其剪裁率为使精度损失低于阈值`0.01`的最大剪裁率。
用户在计算出一组剪裁率之后可以通过接口`paddleslim.prune.Pruner`剪裁网络,并用接口`paddleslim.analysis.flops`计算`FLOPs`。如果`FLOPs`不满足要求,调整阈值重新计算出一组剪裁率。
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SOURCEDIR = ./
BUILDDIR = build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
API Documents
========
.. toctree::
:maxdepth: 1
modules.rst
paddleslim.analysis.rst
paddleslim.common.rst
paddleslim.core.rst
paddleslim.dist.rst
paddleslim.models.rst
paddleslim.nas.one_shot.rst
paddleslim.nas.rst
paddleslim.nas.search_space.rst
paddleslim.pantheon.rst
paddleslim.prune.rst
paddleslim.quant.rst
paddleslim.rst
paddleslim
==========
.. toctree::
:maxdepth: 4
paddleslim
paddleslim.analysis package
===========================
.. automodule:: paddleslim.analysis
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.analysis.flops module
--------------------------------
.. automodule:: paddleslim.analysis.flops
:members:
:undoc-members:
:show-inheritance:
paddleslim.analysis.latency module
----------------------------------
.. automodule:: paddleslim.analysis.latency
:members:
:undoc-members:
:show-inheritance:
paddleslim.analysis.model\_size module
--------------------------------------
.. automodule:: paddleslim.analysis.model_size
:members:
:undoc-members:
:show-inheritance:
paddleslim.common package
=========================
.. automodule:: paddleslim.common
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.common.cached\_reader module
---------------------------------------
.. automodule:: paddleslim.common.cached_reader
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.controller module
-----------------------------------
.. automodule:: paddleslim.common.controller
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.controller\_client module
-------------------------------------------
.. automodule:: paddleslim.common.controller_client
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.controller\_server module
-------------------------------------------
.. automodule:: paddleslim.common.controller_server
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.lock module
-----------------------------
.. automodule:: paddleslim.common.lock
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.log\_helper module
------------------------------------
.. automodule:: paddleslim.common.log_helper
:members:
:undoc-members:
:show-inheritance:
paddleslim.common.sa\_controller module
---------------------------------------
.. automodule:: paddleslim.common.sa_controller
:members:
:undoc-members:
:show-inheritance:
paddleslim.core package
=======================
.. automodule:: paddleslim.core
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.core.graph\_wrapper module
-------------------------------------
.. automodule:: paddleslim.core.graph_wrapper
:members:
:undoc-members:
:show-inheritance:
paddleslim.core.registry module
-------------------------------
.. automodule:: paddleslim.core.registry
:members:
:undoc-members:
:show-inheritance:
paddleslim.dist package
=======================
.. automodule:: paddleslim.dist
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.dist.single\_distiller module
----------------------------------------
.. automodule:: paddleslim.dist.single_distiller
:members:
:undoc-members:
:show-inheritance:
paddleslim.models package
=========================
.. automodule:: paddleslim.models
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.models.classification\_models module
-----------------------------------------------
.. automodule:: paddleslim.models.classification_models
:members:
:undoc-members:
:show-inheritance:
paddleslim.models.mobilenet module
----------------------------------
.. automodule:: paddleslim.models.mobilenet
:members:
:undoc-members:
:show-inheritance:
paddleslim.models.mobilenet\_v2 module
--------------------------------------
.. automodule:: paddleslim.models.mobilenet_v2
:members:
:undoc-members:
:show-inheritance:
paddleslim.models.resnet module
-------------------------------
.. automodule:: paddleslim.models.resnet
:members:
:undoc-members:
:show-inheritance:
paddleslim.models.util module
-----------------------------
.. automodule:: paddleslim.models.util
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.one\_shot package
================================
.. automodule:: paddleslim.nas.one_shot
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.nas.one\_shot.one\_shot\_nas module
----------------------------------------------
.. automodule:: paddleslim.nas.one_shot.one_shot_nas
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.one\_shot.super\_mnasnet module
----------------------------------------------
.. automodule:: paddleslim.nas.one_shot.super_mnasnet
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas package
======================
.. automodule:: paddleslim.nas
:members:
:undoc-members:
:show-inheritance:
Subpackages
-----------
.. toctree::
paddleslim.nas.one_shot
paddleslim.nas.search_space
Submodules
----------
paddleslim.nas.sa\_nas module
-----------------------------
.. automodule:: paddleslim.nas.sa_nas
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space package
====================================
.. automodule:: paddleslim.nas.search_space
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.nas.search\_space.base\_layer module
-----------------------------------------------
.. automodule:: paddleslim.nas.search_space.base_layer
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.combine\_search\_space module
----------------------------------------------------------
.. automodule:: paddleslim.nas.search_space.combine_search_space
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.inception\_block module
----------------------------------------------------
.. automodule:: paddleslim.nas.search_space.inception_block
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.mobilenet\_block module
----------------------------------------------------
.. automodule:: paddleslim.nas.search_space.mobilenet_block
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.mobilenetv1 module
-----------------------------------------------
.. automodule:: paddleslim.nas.search_space.mobilenetv1
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.mobilenetv2 module
-----------------------------------------------
.. automodule:: paddleslim.nas.search_space.mobilenetv2
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.resnet module
------------------------------------------
.. automodule:: paddleslim.nas.search_space.resnet
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.resnet\_block module
-------------------------------------------------
.. automodule:: paddleslim.nas.search_space.resnet_block
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.search\_space\_base module
-------------------------------------------------------
.. automodule:: paddleslim.nas.search_space.search_space_base
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.search\_space\_factory module
----------------------------------------------------------
.. automodule:: paddleslim.nas.search_space.search_space_factory
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.search\_space\_registry module
-----------------------------------------------------------
.. automodule:: paddleslim.nas.search_space.search_space_registry
:members:
:undoc-members:
:show-inheritance:
paddleslim.nas.search\_space.utils module
-----------------------------------------
.. automodule:: paddleslim.nas.search_space.utils
:members:
:undoc-members:
:show-inheritance:
paddleslim.pantheon package
===========================
.. automodule:: paddleslim.pantheon
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.pantheon.student module
----------------------------------
.. automodule:: paddleslim.pantheon.student
:members:
:undoc-members:
:show-inheritance:
paddleslim.pantheon.teacher module
----------------------------------
.. automodule:: paddleslim.pantheon.teacher
:members:
:undoc-members:
:show-inheritance:
paddleslim.pantheon.utils module
--------------------------------
.. automodule:: paddleslim.pantheon.utils
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune package
========================
.. automodule:: paddleslim.prune
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.prune.auto\_pruner module
------------------------------------
.. automodule:: paddleslim.prune.auto_pruner
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune.prune\_io module
---------------------------------
.. automodule:: paddleslim.prune.prune_io
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune.prune\_walker module
-------------------------------------
.. automodule:: paddleslim.prune.prune_walker
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune.pruner module
------------------------------
.. automodule:: paddleslim.prune.pruner
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune.sensitive module
---------------------------------
.. automodule:: paddleslim.prune.sensitive
:members:
:undoc-members:
:show-inheritance:
paddleslim.prune.sensitive\_pruner module
-----------------------------------------
.. automodule:: paddleslim.prune.sensitive_pruner
:members:
:undoc-members:
:show-inheritance:
paddleslim.quant package
========================
.. automodule:: paddleslim.quant
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
paddleslim.quant.quant\_embedding module
----------------------------------------
.. automodule:: paddleslim.quant.quant_embedding
:members:
:undoc-members:
:show-inheritance:
paddleslim.quant.quanter module
-------------------------------
.. automodule:: paddleslim.quant.quanter
:members:
:undoc-members:
:show-inheritance:
paddleslim package
==================
.. automodule:: paddleslim
:members:
:undoc-members:
:show-inheritance:
Subpackages
-----------
.. toctree::
paddleslim.analysis
paddleslim.common
paddleslim.core
paddleslim.dist
paddleslim.models
paddleslim.nas
paddleslim.pantheon
paddleslim.prune
paddleslim.quant
Submodules
----------
paddleslim.version module
-------------------------
.. automodule:: paddleslim.version
:members:
:undoc-members:
:show-inheritance:
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../'))
# -- Project information -----------------------------------------------------
project = u'PaddleSlim'
copyright = u'2020, paddleslim'
author = u'paddleslim'
# The short X.Y version
version = u''
# The full version, including alpha/beta/rc tags
release = u'1.0'
# -- General configuration ---------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
'sphinx.ext.githubpages',
'sphinx.ext.napoleon',
'recommonmark',
'sphinx_markdown_tables',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
source_suffix = ['.rst', '.md']
#source_suffix = '.rst'
# The master toctree document.
master_doc = 'index_en'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = u'en_US'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}
# -- Options for HTMLHelp output ---------------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'PaddleSlimdoc'
# -- Options for LaTeX output ------------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [(master_doc, 'PaddleSlim.tex', u'PaddleSlim Documentation',
u'paddleslim', 'manual'), ]
# -- Options for manual page output ------------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, 'paddleslim', u'PaddleSlim Documentation', [author],
1)]
# -- Options for Texinfo output ----------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'PaddleSlim', u'PaddleSlim Documentation', author,
'PaddleSlim', 'One line description of project.', 'Miscellaneous'),
]
# -- Options for Epub output -------------------------------------------------
# Bibliographic Dublin Core info.
epub_title = project
# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''
# A unique identification for the text.
#
# epub_uid = ''
# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']
# -- Extension configuration -------------------------------------------------
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
中文文档
========
.. toctree::
:maxdepth: 1
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to use PaddleSlim.
========
.. toctree::
:maxdepth: 1
index
intro_en.md
install_en.md
quick_start/index_en
tutorials/index_en
api_en/index_en
model_zoo_en.md
# Install
安装PaddleSlim前,请确认已正确安装Paddle1.6版本或更新版本。Paddle安装请参考:[Paddle安装教程](https://www.paddlepaddle.org.cn/install/quick)
- 安装develop版本
```bash
git clone https://github.com/PaddlePaddle/PaddleSlim.git
cd PaddleSlim
python setup.py install
```
- 安装官方发布的最新版本
```bash
pip install paddleslim -i https://pypi.org/simple
```
- 安装历史版本
请点击[pypi.org](https://pypi.org/project/paddleslim/#history)查看可安装历史版本。
# PaddleSlim # Introduction
PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领域模型。在PaddleSlim中,不仅实现了目前主流的网络剪枝、量化、蒸馏三种压缩策略,还实现了超参数搜索和小模型网络结构搜索功能。在后续版本中,会添加更多的压缩策略,以及完善对NLP领域模型的支持。 PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领域模型。在PaddleSlim中,不仅实现了目前主流的网络剪枝、量化、蒸馏三种压缩策略,还实现了超参数搜索和小模型网络结构搜索功能。在后续版本中,会添加更多的压缩策略,以及完善对NLP领域模型的支持。
...@@ -22,40 +22,3 @@ PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领 ...@@ -22,40 +22,3 @@ PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领
- 支持基于进化算法的轻量神经网络结构自动搜索(Light-NAS) - 支持基于进化算法的轻量神经网络结构自动搜索(Light-NAS)
- 支持 FLOPS / 硬件延时约束 - 支持 FLOPS / 硬件延时约束
- 支持多平台模型延时评估 - 支持多平台模型延时评估
## 安装
安装PaddleSlim前,请确认已正确安装Paddle1.6版本或更新版本。Paddle安装请参考:[Paddle安装教程](https://www.paddlepaddle.org.cn/install/quick)
- 安装develop版本
```bash
git clone https://github.com/PaddlePaddle/PaddleSlim.git
cd PaddleSlim
python setup.py install
```
- 安装官方发布的最新版本
```bash
pip install paddleslim -i https://pypi.org/simple
```
- 安装历史版本
请点击[pypi.org](https://pypi.org/project/paddleslim/#history)查看可安装历史版本。
## 使用
- [API文档](doc/api_guide.md):API使用介绍,包括[蒸馏]()、[剪裁]()、[量化]()和[模型结构搜索]()。
- [示例](doc/demo_guide.md):基于mnist和cifar10等简单分类任务的模型压缩示例,您可以通过该部分快速体验和了解PaddleSlim的功能。
- [实践教程]():经典模型的分析和压缩实验教程。
- [模型库]():经过压缩的分类、检测、语义分割模型,包括权重文件、网络结构文件和性能数据。
- [Paddle检测库]():介绍如何在检测库中使用PaddleSlim。
- [Paddle分割库]():介绍如何在分割库中使用PaddleSlim。
- [PaddleLite]():介绍如何使用预测库PaddleLite部署PaddleSlim产出的模型。
## 贡献与反馈
# Model Zoo
## 1. 图象分类
数据集:ImageNet1000类
### 1.1 量化
| 模型 | 压缩方法 | Top-1/Top-5 Acc | 模型体积(MB) | 下载 |
|:--:|:---:|:--:|:--:|:--:|
|MobileNetV1|-|70.99%/89.68%| xx | [下载链接]() |
|MobileNetV1|quant_post|xx%/xx%| xx | [下载链接]() |
|MobileNetV1|quant_aware|xx%/xx%| xx | [下载链接]() |
| MobileNetV2 | - |72.15%/90.65%| xx | [下载链接]() |
| MobileNetV2 | quant_post |xx%/xx%| xx | [下载链接]() |
| MobileNetV2 | quant_aware |xx%/xx%| xx | [下载链接]() |
|ResNet50|-|76.50%/93.00%| xx | [下载链接]() |
|ResNet50|quant_post|xx%/xx%| xx | [下载链接]() |
|ResNet50|quant_aware|xx%/xx%| xx | [下载链接]() |
### 1.2 剪裁
| 模型 | 压缩方法 | Top-1/Top-5 Acc | 模型体积(MB) | GFLOPs | 下载 |
|:--:|:---:|:--:|:--:|:--:|:--:|
| MobileNetV1 | Baseline | 70.99%/89.68% | 17 | 1.11 | [下载链接](http://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV1_pretrained.tar) |
| MobileNetV1 | uniform -50% | 69.4%/88.66% (-1.59%/-1.02%) | 9 | 0.56 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV1_uniform-50.tar) |
| MobileNetV1 | sensitive -30% | 70.4%/89.3% (-0.59%/-0.38%) | 12 | 0.74 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV1_sensitive-30.tar) |
| MobileNetV1 | sensitive -50% | 69.8% / 88.9% (-1.19%/-0.78%) | 9 | 0.56 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV1_sensitive-50.tar) |
| MobileNetV2 | - | 72.15%/90.65% | 15 | 0.59 | [下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV2_pretrained.tar) |
| MobileNetV2 | uniform -50% | 65.79%/86.11% (-6.35%/-4.47%) | 11 | 0.296 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV2_uniform-50.tar) |
| ResNet34 | - | 72.15%/90.65% | 84 | 7.36 | [下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/ResNet34_pretrained.tar) |
| ResNet34 | uniform -50% | 70.99%/89.95% (-1.36%/-0.87%) | 41 | 3.67 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/ResNet34_uniform-50.tar) |
| ResNet34 | auto -55.05% | 70.24%/89.63% (-2.04%/-1.06%) | 33 | 3.31 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/ResNet34_auto-55.tar) |
### 1.3 蒸馏
| 模型 | 压缩方法 | Top-1/Top-5 Acc | 模型体积(MB) | 下载 |
|:--:|:---:|:--:|:--:|:--:|
| MobileNetV1 | student | 70.99%/89.68% | 17 | [下载链接](http://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV1_pretrained.tar) |
|ResNet50_vd|teacher|79.12%/94.44%| 99 | [下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_vd_pretrained.tar) |
|MobileNetV1|ResNet50_vd<sup>[1](#trans1)</sup> distill|72.77%/90.68% (+1.78%/+1.00%)| 17 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV1_distilled.tar) |
| MobileNetV2 | student | 72.15%/90.65% | 15 | [下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV2_pretrained.tar) |
| MobileNetV2 | ResNet50_vd distill | 74.28%/91.53% (+2.13%/+0.88%) | 15 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/MobileNetV2_distilled.tar) |
| ResNet50 | student | 76.50%/93.00% | 99 | [下载链接](http://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar) |
|ResNet101|teacher|77.56%/93.64%| 173 | [下载链接](http://paddle-imagenet-models-name.bj.bcebos.com/ResNet101_pretrained.tar) |
| ResNet50 | ResNet101 distill | 77.29%/93.65% (+0.79%/+0.65%) | 99 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/ResNet50_distilled.tar) |
!!! note "Note"
<a name="trans1">[1]</a>:带_vd后缀代表该预训练模型使用了Mixup,Mixup相关介绍参考[mixup: Beyond Empirical Risk Minimization](https://arxiv.org/abs/1710.09412)
## 2. 目标检测
### 2.1 量化
数据集: COCO 2017
| 模型 | 压缩方法 | 数据集 | Image/GPU | 输入608 Box AP | 输入416 Box AP | 输入320 Box AP | 模型体积(MB) | 下载 |
| :----------------------------: | :---------: | :----: | :-------: | :------------: | :------------: | :------------: | :------------: | :----------: |
| MobileNet-V1-YOLOv3 | - | COCO | 8 | 29.3 | 29.3 | 27.1 | xx | [下载链接]() |
| MobileNet-V1-YOLOv3 | quant_post | COCO | 8 | xx | xx | xx | xx | [下载链接]() |
| MobileNet-V1-YOLOv3 | quant_aware | COCO | 8 | xx | xx | xx | xx | [下载链接]() |
| R50-dcn-YOLOv3 obj365_pretrain | - | COCO | 8 | 41.4 | xx | xx | xx | [下载链接]() |
| R50-dcn-YOLOv3 obj365_pretrain | quant_post | COCO | 8 | xx | xx | xx | xx | [下载链接]() |
| R50-dcn-YOLOv3 obj365_pretrain | quant_aware | COCO | 8 | xx | xx | xx | xx | [下载链接]() |
数据集:WIDER-FACE
| 模型 | 压缩方法 | Image/GPU | 输入尺寸 | Easy/Medium/Hard | 模型体积(MB) | 下载 |
| :------------: | :---------: | :-------: | :------: | :---------------: | :------------: | :----------: |
| BlazeFace | - | 8 | 640 | 0.915/0.892/0.797 | xx | [下载链接]() |
| BlazeFace | quant_post | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
| BlazeFace | quant_aware | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
| BlazeFace-Lite | - | 8 | 640 | 0.909/0.885/0.781 | xx | [下载链接]() |
| BlazeFace-Lite | quant_post | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
| BlazeFace-Lite | quant_aware | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
| BlazeFace-NAS | - | 8 | 640 | 0.837/0.807/0.658 | xx | [下载链接]() |
| BlazeFace-NAS | quant_post | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
| BlazeFace-NAS | quant_aware | 8 | 640 | xx/xx/xx | xx | [下载链接]() |
### 2.2 剪裁
数据集:Pasacl VOC & COCO 2017
| 模型 | 压缩方法 | 数据集 | Image/GPU | 输入608 Box AP | 输入416 Box AP | 输入320 Box AP | 模型体积(MB) | GFLOPs (608*608) | 下载 |
| :----------------------------: | :---------------: | :--------: | :-------: | :------------: | :------------: | :------------: | :----------: | :--------------: | :----------------------------------------------------------: |
| MobileNet-V1-YOLOv3 | Baseline | Pascal VOC | 8 | 76.2 | 76.7 | 75.3 | 94 | 40.49 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_mobilenet_v1_voc.tar) |
| MobileNet-V1-YOLOv3 | sensitive -52.88% | Pascal VOC | 8 | 77.6 (+1.4) | 77.7 (1.0) | 75.5 (+0.2) | 31 | 19.08 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_mobilenet_v1_voc_prune.tar) |
| MobileNet-V1-YOLOv3 | - | COCO | 8 | 29.3 | 29.3 | 27.0 | 95 | 41.35 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_mobilenet_v1.tar) |
| MobileNet-V1-YOLOv3 | sensitive -51.77% | COCO | 8 | 26.0 (-3.3) | 25.1 (-4.2) | 22.6 (-4.4) | 32 | 19.94 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_mobilenet_v1_prune.tar) |
| R50-dcn-YOLOv3 | - | COCO | 8 | 39.1 | - | - | 177 | 89.60 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r50vd_dcn.tar) |
| R50-dcn-YOLOv3 | sensitive -9.37% | COCO | 8 | 39.3 (+0.2) | - | - | 150 | 81.20 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_r50vd_dcn_prune.tar) |
| R50-dcn-YOLOv3 | sensitive -24.68% | COCO | 8 | 37.3 (-1.8) | - | - | 113 | 67.48 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_r50vd_dcn_prune578.tar) |
| R50-dcn-YOLOv3 obj365_pretrain | - | COCO | 8 | 41.4 | - | - | 177 | 89.60 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r50vd_dcn_obj365_pretrained_coco.tar) |
| R50-dcn-YOLOv3 obj365_pretrain | sensitive -9.37% | COCO | 8 | 40.5 (-0.9) | - | - | 150 | 81.20 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_r50vd_dcn_obj365_pretrained_coco_prune.tar) |
| R50-dcn-YOLOv3 obj365_pretrain | sensitive -24.68% | COCO | 8 | 37.8 (-3.3) | - | - | 113 | 67.48 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_r50vd_dcn_obj365_pretrained_coco_prune578.tar) |
### 2.3 蒸馏
数据集:Pasacl VOC & COCO 2017
| 模型 | 压缩方法 | 数据集 | Image/GPU | 输入608 Box AP | 输入416 Box AP | 输入320 Box AP | 模型体积(MB) | 下载 |
| :-----------------: | :---------------------: | :--------: | :-------: | :------------: | :------------: | :------------: | :------------: | :----------------------------------------------------------: |
| MobileNet-V1-YOLOv3 | - | Pascal VOC | 8 | 76.2 | 76.7 | 75.3 | 94 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_mobilenet_v1_voc.tar) |
| ResNet34-YOLOv3 | - | Pascal VOC | 8 | 82.6 | 81.9 | 80.1 | 162 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r34_voc.tar) |
| MobileNet-V1-YOLOv3 | ResNet34-YOLOv3 distill | Pascal VOC | 8 | 79.0 (+2.8) | 78.2 (+1.5) | 75.5 (+0.2) | 94 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_mobilenetv1_voc_distilled.tar) |
| MobileNet-V1-YOLOv3 | - | COCO | 8 | 29.3 | 29.3 | 27.0 | 95 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_mobilenet_v1.tar) |
| ResNet34-YOLOv3 | - | COCO | 8 | 36.2 | 34.3 | 31.4 | 163 | [下载链接](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r34.tar) |
| MobileNet-V1-YOLOv3 | ResNet34-YOLOv3 distill | COCO | 8 | 31.4 (+2.1) | 30.0 (+0.7) | 27.1 (+0.1) | 95 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/yolov3_mobilenetv1_coco_distilled.tar) |
## 3. 图像分割
数据集:Cityscapes
### 3.1 量化
| 模型 | 压缩方法 | mIoU | 模型体积(MB) | 下载 |
| :--------------------: | :---------: | :---: | :------------: | :----------: |
| DeepLabv3+/MobileNetv1 | - | 63.26 | xx | [下载链接]() |
| DeepLabv3+/MobileNetv1 | quant_post | xx | xx | [下载链接]() |
| DeepLabv3+/MobileNetv1 | quant_aware | xx | xx | [下载链接]() |
| DeepLabv3+/MobileNetv2 | - | 69.81 | xx | [下载链接]() |
| DeepLabv3+/MobileNetv2 | quant_post | xx | xx | [下载链接]() |
| DeepLabv3+/MobileNetv2 | quant_aware | xx | xx | [下载链接]() |
### 3.2 剪裁
| 模型 | 压缩方法 | mIoU | 模型体积(MB) | GFLOPs | 下载 |
| :-------: | :---------------: | :-----------: | :------------: | :----: | :----------------------------------------------------------: |
| fast-scnn | baseline | 69.64 | 11 | 14.41 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape.tar) |
| fast-scnn | uniform -17.07% | 69.58 (-0.06) | 8.5 | 11.95 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_uniform-17.tar) |
| fast-scnn | sensitive -47.60% | 66.68 (-2.96) | 5.7 | 7.55 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_sensitive-47.tar) |
Quick Start
========
.. toctree::
:maxdepth: 1
pruning_tutorial_en.md
# Pruning of image classification model - quick start
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的卷积通道剪裁接口]()。
该示例包含以下步骤:
1. 导入依赖
2. 构建模型
3. 剪裁
4. 训练剪裁后的模型
以下章节依次次介绍每个步骤的内容。
## 1. 导入依赖
PaddleSlim依赖Paddle1.7版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
```
import paddle
import paddle.fluid as fluid
import paddleslim as slim
```
## 2. 构建网络
该章节构造一个用于对MNIST数据进行分类的分类模型,选用`MobileNetV1`,并将输入大小设置为`[1, 28, 28]`,输出类别数为10。
为了方便展示示例,我们在`paddleslim.models`下预定义了用于构建分类模型的方法,执行以下代码构建分类模型:
```
exe, train_program, val_program, inputs, outputs =
slim.models.image_classification("MobileNet", [1, 28, 28], 10, use_gpu=False)
```
>注意:paddleslim.models下的API并非PaddleSlim常规API,是为了简化示例而封装预定义的一系列方法,比如:模型结构的定义、Program的构建等。
## 3. 剪裁卷积层通道
### 3.1 计算剪裁之前的FLOPs
```
FLOPs = slim.analysis.flops(train_program)
print("FLOPs: {}".format(FLOPs))
```
### 3.2 剪裁
我们这里对参数名为`conv2_1_sep_weights``conv2_2_sep_weights`的卷积层进行剪裁,分别剪掉20%和30%的通道数。
代码如下所示:
```
pruner = slim.prune.Pruner()
pruned_program, _, _ = pruner.prune(
train_program,
fluid.global_scope(),
params=["conv2_1_sep_weights", "conv2_2_sep_weights"],
ratios=[0.33] * 2,
place=fluid.CPUPlace())
```
以上操作会修改`train_program`中对应卷积层参数的定义,同时对`fluid.global_scope()`中存储的参数数组进行裁剪。
### 3.3 计算剪裁之后的FLOPs
```
FLOPs = paddleslim.analysis.flops(train_program)
print("FLOPs: {}".format(FLOPs))
```
## 4. 训练剪裁后的模型
### 4.1 定义输入数据
为了快速执行该示例,我们选取简单的MNIST数据,Paddle框架的`paddle.dataset.mnist`包定义了MNIST数据的下载和读取。
代码如下:
```
import paddle.dataset.mnist as reader
train_reader = paddle.batch(
reader.train(), batch_size=128, drop_last=True)
train_feeder = fluid.DataFeeder(inputs, fluid.CPUPlace())
```
### 4.2 执行训练
以下代码执行了一个`epoch`的训练:
```
for data in train_reader():
acc1, acc5, loss = exe.run(pruned_program, feed=train_feeder.feed(data), fetch_list=outputs)
print(acc1, acc5, loss)
```
Aadvanced Tutorials
========
.. toctree::
:maxdepth: 1
sensitivity_tutorial_en.md
# Pruning of image classification model - sensitivity
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的敏感度分析接口](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#sensitivity)
该示例包含以下步骤:
1. 导入依赖
2. 构建模型
3. 定义输入数据
4. 定义模型评估方法
5. 训练模型
6. 获取待分析卷积参数名称
7. 分析敏感度
8. 剪裁模型
以下章节依次次介绍每个步骤的内容。
## 1. 导入依赖
PaddleSlim依赖Paddle1.7版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
```
import paddle
import paddle.fluid as fluid
import paddleslim as slim
```
site_name: PaddleSlim Docs
repo_url: https://github.com/PaddlePaddle/PaddleSlim
nav:
- Home: index.md
- 模型库: model_zoo.md
- 教程:
- 图像分类模型通道剪裁-快速开始: tutorials/pruning_tutorial.md
- 图像分类模型通道剪裁-敏感度分析: tutorials/image_classification_sensitivity_analysis_tutorial.md
- 离线量化: tutorials/quant_post_demo.md
- 量化训练: tutorials/quant_aware_demo.md
- Embedding量化: tutorials/quant_embedding_demo.md
- SA搜索: tutorials/nas_demo.md
- One-shot搜索: tutorials/one_shot_nas_demo.md
- 搜索空间: search_space.md
- 知识蒸馏: tutorials/distillation_demo.md
- API:
- 量化: api/quantization_api.md
- 剪枝与敏感度: api/prune_api.md
- 模型分析: api/analysis_api.md
- 简单知识蒸馏: api/single_distiller_api.md
- 大规模可扩展知识蒸馏框架 Pantheon: api/pantheon_api.md
- SA搜索: api/nas_api.md
- One-shot搜索: api/one_shot_api.md
- 搜索空间: search_space.md
- 硬件延时评估表: table_latency.md
- 算法原理: algo/algo.md
theme:
name: readthedocs
highlightjs: true
extra_css:
- extra.css
markdown_extensions:
- admonition
- codehilite
- toc:
permalink: "#"
- footnotes
- meta
- def_list
- pymdownx.arithmatex
- pymdownx.betterem:
smart_enable: all
- pymdownx.caret
- pymdownx.critic
- pymdownx.details
- pymdownx.magiclink
- pymdownx.mark
- pymdownx.smartsymbols
- pymdownx.superfences
- pymdownx.tasklist
- pymdownx.tilde
- mdx_math
extra_javascript:
- mathjax-config.js
- https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML
mkdocs sphinx
markdown recommonmark
python-markdown-math sphinx_markdown_tables
pymdown-extensions sphinx_rtd_theme
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SOURCEDIR = ./
BUILDDIR = build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
## 目录 # 算法原理
## 目录
- [量化原理介绍](#1-quantization-aware-training量化介绍) - [量化原理介绍](#1-quantization-aware-training量化介绍)
- [剪裁原理介绍](#2-卷积核剪裁原理) - [剪裁原理介绍](#2-卷积核剪裁原理)
......
# 模型分析
## FLOPs ## FLOPs
paddleslim.analysis.flops(program, detail=False) [源代码](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/analysis/flops.py) paddleslim.analysis.flops(program, detail=False) [源代码](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/analysis/flops.py)
......
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
API文档
======================================
.. toctree::
:maxdepth: 1
analysis_api.md
nas_api.md
one_shot_api.md
pantheon_api.md
prune_api.md
quantization_api.md
single_distiller_api.md
search_space.md
table_latency.md
# SA-NAS
## 搜索空间参数的配置 ## 搜索空间参数的配置
通过参数配置搜索空间。更多搜索空间的使用可以参考[search_space](../search_space.md) 通过参数配置搜索空间。更多搜索空间的使用可以参考[search_space](../search_space.md)
......
# OneShotNAS
## OneShotSearch ## OneShotSearch
paddleslim.nas.one_shot.OneShotSearch(model, eval_func, strategy='sa', search_steps=100)[代码]() paddleslim.nas.one_shot.OneShotSearch(model, eval_func, strategy='sa', search_steps=100)[代码]()
......
# 多进程蒸馏
## Teacher ## Teacher
pantheon.Teacher() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L78) pantheon.Teacher() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L78)
......
# 卷积层通道剪裁
## Pruner ## Pruner
paddleslim.prune.Pruner(criterion="l1_norm")[源代码](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/prune/pruner.py#L28) paddleslim.prune.Pruner(criterion="l1_norm")[源代码](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/prune/pruner.py#L28)
......
# 量化
## 量化配置 ## 量化配置
通过字典配置量化参数 通过字典配置量化参数
......
# 搜索空间
## 搜索空间简介 ## 搜索空间简介
: 搜索空间是神经网络搜索中的一个概念。搜索空间是一系列模型结构的汇集, SANAS主要是利用模拟退火的思想在搜索空间中搜索到一个比较小的模型结构或者一个精度比较高的模型结构。 : 搜索空间是神经网络搜索中的一个概念。搜索空间是一系列模型结构的汇集, SANAS主要是利用模拟退火的思想在搜索空间中搜索到一个比较小的模型结构或者一个精度比较高的模型结构。
......
# 简单蒸馏
## merge ## merge
paddleslim.dist.merge(teacher_program, student_program, data_name_map, place, scope=fluid.global_scope(), name_prefix='teacher_') [[源代码]](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dist/single_distiller.py#L19) paddleslim.dist.merge(teacher_program, student_program, data_name_map, place, scope=fluid.global_scope(), name_prefix='teacher_') [[源代码]](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dist/single_distiller.py#L19)
......
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../../'))
# -- Project information -----------------------------------------------------
project = u'PaddleSlim'
copyright = u'2020, paddleslim'
author = u'paddleslim'
# The short X.Y version
version = u''
# The full version, including alpha/beta/rc tags
release = u'1.0'
# -- General configuration ---------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
'sphinx.ext.githubpages',
'sphinx.ext.napoleon',
'recommonmark',
'sphinx_markdown_tables',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
source_suffix = ['.rst', '.md']
#source_suffix = '.rst'
# The master toctree document.
master_doc = 'index'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = u'zh_CN'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}
# -- Options for HTMLHelp output ---------------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'PaddleSlimdoc'
# -- Options for LaTeX output ------------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [(master_doc, 'PaddleSlim.tex', u'PaddleSlim Documentation',
u'paddleslim', 'manual'), ]
# -- Options for manual page output ------------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, 'paddleslim', u'PaddleSlim Documentation', [author],
1)]
# -- Options for Texinfo output ----------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'PaddleSlim', u'PaddleSlim Documentation', author,
'PaddleSlim', 'One line description of project.', 'Miscellaneous'),
]
# -- Options for Epub output -------------------------------------------------
# Bibliographic Dublin Core info.
epub_title = project
# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''
# A unique identification for the text.
#
# epub_uid = ''
# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']
# -- Extension configuration -------------------------------------------------
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
欢迎使用PaddleSlim
==================
.. toctree::
:maxdepth: 1
index_en
intro.md
install.md
quick_start/index
tutorials/index
api_cn/index
model_zoo.md
algo/algo.md
.. PaddleSlim documentation master file, created by
sphinx-quickstart on Wed Feb 5 14:04:52 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
English Documents
================
.. toctree::
:maxdepth: 1
# 安装
安装PaddleSlim前,请确认已正确安装Paddle1.6版本或更新版本。Paddle安装请参考:[Paddle安装教程](https://www.paddlepaddle.org.cn/install/quick)
- 安装develop版本
```bash
git clone https://github.com/PaddlePaddle/PaddleSlim.git
cd PaddleSlim
python setup.py install
```
- 安装官方发布的最新版本
```bash
pip install paddleslim -i https://pypi.org/simple
```
- 安装历史版本
请点击[pypi.org](https://pypi.org/project/paddleslim/#history)查看可安装历史版本。
# PaddleSlim简介
PaddleSlim是PaddlePaddle框架的一个子模块,主要用于压缩图像领域模型。在PaddleSlim中,不仅实现了目前主流的网络剪枝、量化、蒸馏三种压缩策略,还实现了超参数搜索和小模型网络结构搜索功能。在后续版本中,会添加更多的压缩策略,以及完善对NLP领域模型的支持。
## 功能
- 模型剪裁
- 支持通道均匀模型剪裁(uniform pruning)
- 基于敏感度的模型剪裁
- 基于进化算法的自动模型剪裁三种方式
- 量化训练
- 在线量化训练(training aware)
- 离线量化(post training)
- 支持对权重全局量化和Channel-Wise量化
- 蒸馏
- 轻量神经网络结构自动搜索(Light-NAS)
- 支持基于进化算法的轻量神经网络结构自动搜索(Light-NAS)
- 支持 FLOPS / 硬件延时约束
- 支持多平台模型延时评估
# 模型库
## 1. 图象分类 ## 1. 图象分类
数据集:ImageNet1000类 数据集:ImageNet1000类
...@@ -646,4 +648,3 @@ ...@@ -646,4 +648,3 @@
| fast-scnn | baseline | 69.64 | 11 | 14.41 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape.tar) | | fast-scnn | baseline | 69.64 | 11 | 14.41 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape.tar) |
| fast-scnn | uniform -17.07% | 69.58 (-0.06) | 8.5 | 11.95 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_uniform-17.tar) | | fast-scnn | uniform -17.07% | 69.58 (-0.06) | 8.5 | 11.95 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_uniform-17.tar) |
| fast-scnn | sensitive -47.60% | 66.68 (-2.96) | 5.7 | 7.55 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_sensitive-47.tar) | | fast-scnn | sensitive -47.60% | 66.68 (-2.96) | 5.7 | 7.55 | [下载链接](https://paddlemodels.bj.bcebos.com/PaddleSlim/fast_scnn_cityscape_sensitive-47.tar) |
快速开始
========
.. toctree::
:maxdepth: 2
:caption: Contents:
pruning_tutorial.md
进阶教程
========
.. toctree::
:maxdepth: 2
:caption: Contents:
image_classification_sensitivity_analysis_tutorial.md
# 图像分类模型通道剪裁-敏感度分析
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的敏感度分析接口](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#sensitivity)
该示例包含以下步骤:
1. 导入依赖
2. 构建模型
3. 定义输入数据
4. 定义模型评估方法
5. 训练模型
6. 获取待分析卷积参数名称
7. 分析敏感度
8. 剪裁模型
以下章节依次次介绍每个步骤的内容。
## 1. 导入依赖
PaddleSlim依赖Paddle1.7版本,请确认已正确安装Paddle,然后按以下方式导入Paddle和PaddleSlim:
```
import paddle
import paddle.fluid as fluid
import paddleslim as slim
```
...@@ -19,18 +19,16 @@ __all__ = ["flops"] ...@@ -19,18 +19,16 @@ __all__ = ["flops"]
def flops(program, only_conv=True, detail=False): def flops(program, only_conv=True, detail=False):
""" """Get FLOPs of target graph.
Get FLOPS of target graph.
Args: Args:
program(Program): The program used to calculate FLOPS. program(Program): The program used to calculate FLOPS.
only_conv(bool): Just return number of mul-adds in convolution and FC layer if `only_conv` is true. only_conv(bool): Just return number of mul-adds in convolution and FC layer if `only_conv` is true.
default: True. default: True.
detail(bool): Whether to return detail of each convolution layer. detail(bool): Whether to return detail of each convolution layer.
Return: Returns:
If `detail` is true, then return a tuple in format `(FLOPs, details)`, otherwise it will just return `FlOPs` int|tuple: If `detail` is true, then return a tuple in format `(FLOPs, details)`, otherwise it will just return `FlOPs`. The details is a dict whose key is the parameter name of convlution layer and value is the FLOPs of each convolution layer.
FLOPs(int): The FLOPs of target network.
details(dict): The key is the parameter name of convlution layer and the value is the FLOPs of each convolution layer.
""" """
graph = GraphWrapper(program) graph = GraphWrapper(program)
return _graph_flops(graph, only_conv=only_conv, detail=detail) return _graph_flops(graph, only_conv=only_conv, detail=detail)
......
"""Define latency evaluators that evaluate the performance of mode on devices.
"""
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License" # Licensed under the Apache License, Version 2.0 (the "License"
...@@ -18,11 +20,19 @@ __all__ = ["LatencyEvaluator", "TableLatencyEvaluator"] ...@@ -18,11 +20,19 @@ __all__ = ["LatencyEvaluator", "TableLatencyEvaluator"]
class LatencyEvaluator(object): class LatencyEvaluator(object):
def __init__(self): """Base class of latency evaluator.
pass """
def latency(self, graph): def latency(self, graph):
pass """Get latency of graph. It is an abstract method.
Args:
graph(GrapWrapper | Program): The graph to be evaluated.
Returns:
latency(float): The latency of given graph on current evaluator.
"""
raise NotImplementedError('Abstract method.')
def _get_ops_from_graph(self, graph, only_conv): def _get_ops_from_graph(self, graph, only_conv):
assert isinstance(graph, GraphWrapper) assert isinstance(graph, GraphWrapper)
...@@ -241,13 +251,14 @@ class LatencyEvaluator(object): ...@@ -241,13 +251,14 @@ class LatencyEvaluator(object):
class TableLatencyEvaluator(LatencyEvaluator): class TableLatencyEvaluator(LatencyEvaluator):
"""The evaluator used to get graph's latency on some devices and infer engines.
Args:
table_file(str): The path of file that records the devices latency of operators.
delimiter(str): The delimiter used in `table_file`.
"""
def __init__(self, table_file, delimiter=","): def __init__(self, table_file, delimiter=","):
"""
The evaluator used to get graph's latency on some devices and infer engines.
Args:
- table_file(str): The path of file that records the devices latency of operators.
- delimiter(str): The delimiter used in `table_file`.
"""
self._table = self._load_table(table_file) self._table = self._load_table(table_file)
self._delimiter = delimiter self._delimiter = delimiter
...@@ -267,11 +278,12 @@ class TableLatencyEvaluator(LatencyEvaluator): ...@@ -267,11 +278,12 @@ class TableLatencyEvaluator(LatencyEvaluator):
return self._table[op_str] return self._table[op_str]
def latency(self, graph, only_conv=True): def latency(self, graph, only_conv=True):
""" """Get latency of target graph.
Get latency of target graph.
Args: Args:
- graph(GrapWrapper | Program): The graph to be evaluated. graph(GrapWrapper | Program): The graph to be evaluated.
- only_conv(bool): only evaluated convolution layer if `only_conv` is true. Default: True. only_conv(bool): only evaluated convolution layer if `only_conv` is true. Default: True.
Returns: Returns:
latency(float): The latency of given graph on current evaluator. latency(float): The latency of given graph on current evaluator.
""" """
......
...@@ -21,8 +21,12 @@ __all__ = ["model_size"] ...@@ -21,8 +21,12 @@ __all__ = ["model_size"]
def model_size(program): def model_size(program):
""" """
Get total value numbers of all parameters. Get total value numbers of all parameters.
Args: Args:
program(Program): The program used to calculate model size. program(fluid.Program): The program used to calculate model size.
Returns:
int: The total count of all parameters.
""" """
size = 0 size = 0
for block in program.blocks: for block in program.blocks:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册