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

Update links (#664)

上级 a3888f3e
# 知识蒸馏
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的知识蒸馏接口](https://paddlepaddle.github.io/PaddleSlim/api/single_distiller_api/)
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的知识蒸馏接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/dist/single_distiller_api.html)
该示例包含以下步骤:
1. 导入依赖
......
# 网络结构搜索
该教程以图像分类模型MobileNetV2为例,说明如何在cifar10数据集上快速使用[网络结构搜索接口](../api/nas_api.md)
该教程以图像分类模型MobileNetV2为例,说明如何在cifar10数据集上快速使用[网络结构搜索接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/nas/nas_api.html)
该示例包含以下步骤:
1. 导入依赖
......
# 卷积Filter剪裁
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的卷积通道剪裁接口]()。
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的卷积通道剪裁接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html)
该示例包含以下步骤:
1. 导入依赖
......
......@@ -128,7 +128,7 @@ test(val_program)
## 4. 量化
按照[默认配置]()在``train_program``和``val_program``中加入量化和反量化op.
按照[默认配置](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/quant/quantization_api.html#id12)``train_program````val_program``中加入量化和反量化op.
```python
......@@ -156,7 +156,7 @@ test(val_quant_program)
## 6. 保存量化后的模型
``4. 量化``中使用接口``slim.quant.quant_aware``接口得到的模型只适合训练时使用,为了得到最终使用时的模型,需要使用[slim.quant.convert]()接口,然后使用[fluid.io.save_inference_model]()保存模型。``float_prog``的参数数据类型是float32,但是数据范围是int8, 保存之后可使用fluid或者paddle-lite加载使用,paddle-lite在使用时,会先将类型转换为int8。``int8_prog``的参数数据类型是int8, 保存后可看到量化后模型大小,不可加载使用。
``4. 量化``中使用接口``slim.quant.quant_aware``接口得到的模型只适合训练时使用,为了得到最终使用时的模型,需要使用[slim.quant.convert](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/quant/quantization_api.html#convert)接口,然后使用[fluid.io.save_inference_model](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/static/save_inference_model_cn.html#save-inference-model)保存模型。``float_prog``的参数数据类型是float32,但是数据范围是int8, 保存之后可使用fluid或者paddle-lite加载使用,paddle-lite在使用时,会先将类型转换为int8。``int8_prog``的参数数据类型是int8, 保存后可看到量化后模型大小,不可加载使用。
```python
......
# 离线量化
该教程以图像分类模型MobileNetV1为例,说明如何快速使用PaddleSlim的[静态离线量化接口]()。 该示例包含以下步骤:
该教程以图像分类模型MobileNetV1为例,说明如何快速使用PaddleSlim的[静态离线量化接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/quant/quantization_api.html#quant-post-dynamic)。 该示例包含以下步骤:
1. 导入依赖
2. 构建模型
......
......@@ -12,8 +12,8 @@ OFA的基本流程分为以下步骤:
4. 传入模型和相应配置
### 1. 定义超网络
这里的超网络指的是用[动态OP](../../api_cn/ofa_layer_api.rst)组网的网络。
PaddleSlim提供了三种获得超网络的方式,具体可以参考[超网络转换](../../api_cn/convert_supernet_api.rst)
这里的超网络指的是用[动态OP](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/ofa/ofa_layer_api.html)组网的网络。
PaddleSlim提供了三种获得超网络的方式,具体可以参考[超网络转换](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/ofa/convert_supernet_api.html)
```python
import paddle
......@@ -26,7 +26,7 @@ OFA的基本流程分为以下步骤:
```
### 2. 训练配置
训练配置默认根据论文中PS的训练模式进行配置,可进行配置的参数和含义可以参考: [RunConfig](../../api_cn/ofa_api.rst)
训练配置默认根据论文中PS的训练模式进行配置,可进行配置的参数和含义可以参考: [RunConfig](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/ofa/ofa_api.html)
```python
from paddleslim.nas.ofa import RunConfig
......@@ -42,7 +42,7 @@ OFA的基本流程分为以下步骤:
```
### 3. 蒸馏配置
为OFA训练过程添加蒸馏配置,可进行配置的参数和含义可以参考: [DistillConfig](../../api_cn/ofa_api.rst)
为OFA训练过程添加蒸馏配置,可进行配置的参数和含义可以参考: [DistillConfig](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/ofa/ofa_api.html#distillconfig)
```python
from paddle.vision.models import mobilenet_v1
......
......@@ -4,11 +4,9 @@ PaddleSlim提供了4种网络结构搜索的方法:基于模拟退火进行网
| 算法名称 | 算法简介 | 代表模型 |
|:---------:|:------------:|:--------:|
| [Once-For-All](nas_ofa.md) | OFA是一种基于One-Shot NAS的压缩方案。这种方式比较高效,其优势是只需要训练一个超网络就可以从中选择满足不同延时要求的子模型。 | Once-For-All |
| [SANAS](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/quick_start/static/nas_tutorial.md) | SANAS是基于模拟退火的方式进行网络结构搜索,在机器资源不多的情况下,选择这种方式一般能得到比强化学习更好的模型。 | \ |
| [RLNAS](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/api_cn/static/nas/nas_api.rst) | RLNAS是基于强化学习的方式进行网络结构搜索,这种方式需要耗费大量机器资源。 | ENAS、NasNet、MNasNet |
| [DARTS/PCDARTS](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/docs/zh_cn/api_cn/dygraph/nas/darts.rst) | DARTS是基于梯度进行网络结构搜索,这种方式比较高效,大大减少了搜索时间和所需要的机器资源。 |DARTS、PCDARTS、ProxylessNAS|
| [Once-For-All](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/nas/dygraph/nas_ofa.html) | OFA是一种基于One-Shot NAS的压缩方案。这种方式比较高效,其优势是只需要训练一个超网络就可以从中选择满足不同延时要求的子模型。 | Once-For-All |
| [SANAS](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/static/nas_tutorial.html) | SANAS是基于模拟退火的方式进行网络结构搜索,在机器资源不多的情况下,选择这种方式一般能得到比强化学习更好的模型。 | \ |
| [RLNAS](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/nas/nas_api.html#rlnas) | RLNAS是基于强化学习的方式进行网络结构搜索,这种方式需要耗费大量机器资源。 | ENAS、NasNet、MNasNet |
## 参考文献
[1] H. Cai, C. Gan, T. Wang, Z. Zhang, and S. Han. Once for all: Train one network and specialize it for efficient deployment. In International Conference on Learning Representations, 2020.
......
......@@ -103,32 +103,19 @@ pruner = L1NormFilterPruner(net, [1, 3, 224, 224])
第三步:同理得到第4个红点。把『卷积1』对应的4个红点链接成曲线,即为『卷积1』的敏感度曲线。
第四步:同理得到『卷积K』的敏感度曲线。
<div align="center">
<img src="filter_pruning/4-1.png" width="600" height="300">
</div>
<div align="center">
图4-1 敏感度计算过程示意图
</div>
![](./filter_pruning/4-1.png)
<center>图4-1</center>
如图4-2所示,为VGG-16在CIFAR10分类任务上的敏感度曲线示意图:
<div align="center">
<img src="filter_pruning/4-2.png" width="600" height="500">
</div>
<div align="center">
图4-2 VGG-16敏感度示例
</div>
![](./filter_pruning/4-2.png)
<center>图4-2</center>
考虑到不同的模型在不同的任务上的精度数值差别较大,甚至不在同一个量级,所以,PaddleSlim在计算和存储卷积层敏感度时,使用的是精度的损失比例。如图4-3所示,为PaddleSlim计算出的MobileNetV1-YOLOv3在VOC检测任务上的敏感度示意图,其中,纵轴为精度损失:
<div align="center">
<img src="filter_pruning/4-3.png" width="600" height="600">
</div>
<div align="center">
图4-3 用精度损失表示的敏感度
</div>
![](./filter_pruning/4-3.png)
<center>图4-3</center>
#### 敏感度计算
......
......@@ -17,12 +17,8 @@ def cal_mask(self, var_name, pruned_ratio, group):
### 1.1 Group概念介绍
<div align="center">
<img src="self_define_filter_pruning/1-1.png" width="600" height="230">
</div>
<div align="center">
<strong>图1-1 卷积层关联关系示意图</strong>
</div>
![](./self_define_filter_pruning/1-1.png)
<center><strong>图1-1 卷积层关联关系示意图</strong></center>
如图1-1所示,在给定模型中有两个卷积层,第一个卷积层有3个`filters`,第二个卷积层有2个`filters`。如果删除第一个卷积绿色的`filter`,第一个卷积的输出特征图的通道数也会减1,同时需要删掉第二个卷积层绿色的`kernels`。如上所述的两个卷积共同组成一个group,表示如下:
......@@ -52,13 +48,8 @@ group = {
图1-2为更复杂的情况,其中,`Add`操作的所有输入的通道数需要保持一致,`Concat`操作的输出通道数的调整可能会影响到所有输入的通道数,因此`group`中可能包含多个卷积的参数或变量,可以是:卷积权重、卷积bias、`batch norm`相关参数等。
<div align="center">
<img src="self_define_filter_pruning/1-2.png" width="388" height="350">
</div>
<div align="center">
<strong>图1-2 复杂网络示例</strong>
</div>
![](./self_define_filter_pruning/1-2.png)
<center><strong>图1-2 复杂网络示例</strong></center>
## 2. 定义模型
......@@ -132,31 +123,20 @@ pruner.restore()
满足上述条件后,我们才能裁掉更多Norm统计值较小的参数,如图4-1中红色部分所示。
<div align="center">
<img src="self_define_filter_pruning/4-1.png" width="600" height="170">
</div>
<div align="center">
<strong>图4-1</strong>
</div>
![](./self_define_filter_pruning/4-1.png)
<center><strong>图 4-1</strong></center>
而现实中的模型的权重分布如图4-2中绿色分布所示,总是有较小的偏差或较大的最小值。
<div align="center">
<img src="self_define_filter_pruning/4-2.png" width="600" height="224">
</div>
<div align="center">
<strong>图4-2</strong>
</div>
![](./self_define_filter_pruning/4-2.png)
<center><strong>图 4-2</strong></center>
考虑到上述传统方法的缺点,FPGM则用filter之间的几何距离来表示重要性,其遵循的原则就是:几何距离比较近的filters,作用也相近。
如图4-3所示,有3个filters,将各个filter展开为向量,并两两计算几何距离。其中,绿色filter的重要性得分就是它到其它两个filter的距离和,即0.7071+0.5831=1.2902。同理算出另外两个filters的得分,绿色filter得分最高,其重要性最高。
<div align="center">
<img src="self_define_filter_pruning/4-3.png" width="400" height="560">
</div>
<div align="center">
<strong>图4-3</strong>
</div>
![](./self_define_filter_pruning/4-3.png)
<center><strong>图 4-3</strong></center>
### 4.2 实现
......
......@@ -18,7 +18,7 @@ PaddleSlim提供以下内置剪裁方法。
- 支持自定义各层剪裁率:意为是否支持手动指定各个卷积层的剪裁率。
除了以上内置策略,PaddleSlim还支持用户自定义卷积通道剪裁策略,请参考:[自定义卷积通道剪裁教程]()
除了以上内置策略,PaddleSlim还支持用户自定义卷积通道剪裁策略,请参考:[自定义卷积通道剪裁教程](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/pruning/dygraph/self_defined_filter_pruning.html)
## 各类方法效果对比
......@@ -52,7 +52,7 @@ pruner =paddleslim. L1NormFilterPruner(net, [1, 3, 128, 128])
pruner.prune_vars({"conv2d_0.w_0": 0.3})
```
[API文档](../../api_cn/dygraph/pruners/l1norm_filter_pruner.html) | [完整示例](../../quick_start/dygraph/dygraph_pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/pruners/l1norm_filter_pruner.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/dygraph/dygraph_pruning_tutorial.html)
#### 静态图
......@@ -66,7 +66,7 @@ pruned_program, _, _ = pruner.prune(
place=fluid.CPUPlace())
```
[API文档](../../api_cn/static/prune/prune_api.html) | [完整示例](../../quick_start/static/pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/static/pruning_tutorial.html)
### FPGMFilterPruner
......@@ -84,7 +84,7 @@ pruner =paddleslim.FPGMFilterPruner(net, [1, 3, 128, 128])
pruner.prune_vars({"conv2d_0.w_0": 0.3})
```
[API文档](../../api_cn/dygraph/pruners/fpgm_filter_pruner.html) | [完整示例](../../quick_start/dygraph/dygraph_pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/dygraph/pruners/fpgm_filter_pruner.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/dygraph/dygraph_pruning_tutorial.html)
#### 静态图
......@@ -98,7 +98,7 @@ pruned_program, _, _ = pruner.prune(
place=fluid.CPUPlace())
```
[API文档](../../api_cn/static/prune/prune_api.html) | [完整示例](../../quick_start/static/pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/static/pruning_tutorial.html)
### SlimFilterPruner
......@@ -121,7 +121,7 @@ pruned_program, _, _ = pruner.prune(
place=fluid.CPUPlace())
```
[API文档](../../api_cn/static/prune/prune_api.html) | [完整示例](../../quick_start/static/pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/static/pruning_tutorial.html)
### OptSlimFilterPruner
......@@ -142,4 +142,4 @@ pruned_program, _, _ = pruner.prune(
place=fluid.CPUPlace())
```
[API文档](../../api_cn/static/prune/prune_api.html) | [完整示例](../../quick_start/static/pruning_tutorial.html)
[API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html) | [完整示例](https://paddleslim.readthedocs.io/zh_CN/latest/quick_start/static/pruning_tutorial.html)
# 敏感度分析
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的敏感度分析接口](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#sensitivity)
该教程以图像分类模型MobileNetV1为例,说明如何快速使用[PaddleSlim的敏感度分析接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html#sensitivity)
该示例包含以下步骤:
1. 导入依赖
......@@ -109,7 +109,7 @@ params = params[:5]
### 7.1 简单计算敏感度
调用[sensitivity接口](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#sensitivity)对训练好的模型进行敏感度分析。
调用[sensitivity接口](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html#paddleslim.prune.sensitivity)对训练好的模型进行敏感度分析。
在计算过程中,敏感度信息会不断追加保存到选项`sensitivities_file`指定的文件中,该文件中已有的敏感度信息不会被重复计算。
......@@ -204,7 +204,7 @@ print(s)
### 8.1 计算剪裁率
首先,调用PaddleSlim提供的[get_ratios_by_loss](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#get_ratios_by_loss)方法根据敏感度计算剪裁率,通过调整参数`loss`大小获得合适的一组剪裁率:
首先,调用PaddleSlim提供的[get_ratios_by_loss](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html#paddleslim.prune.get_ratios_by_loss)方法根据敏感度计算剪裁率,通过调整参数`loss`大小获得合适的一组剪裁率:
```python
......@@ -230,7 +230,7 @@ print("FLOPs after pruning: {}".format(slim.analysis.flops(pruned_program)))
### 8.3 剪裁测试网络
>注意:对测试网络进行剪裁时,需要将`only_graph`设置为True,具体原因请参考[Pruner API文档](https://paddlepaddle.github.io/PaddleSlim/api/prune_api/#pruner)
>注意:对测试网络进行剪裁时,需要将`only_graph`设置为True,具体原因请参考[Pruner API文档](https://paddleslim.readthedocs.io/zh_CN/latest/api_cn/static/prune/prune_api.html#paddleslim.prune.Pruner)
```python
......
......@@ -5,8 +5,8 @@
PaddleSlim包含`QAT量化训练``PACT改进的量化训练`两种量化方法
- [QAT]()
- [PACT]()
- QAT
- PACT
## 使用方法
......
......@@ -3,13 +3,13 @@
## 图像分类INT8量化模型在CPU上的部署和预测
PaddleSlim主要包含三种量化方法:量化训练(Quant Aware Training, QAT)、动态离线量化(Post Training Quantization Dynamic, PTQ Dynamic)、静态离线量化(Post Training Quantization Static, PTQ Static)。
- [量化训练](quant_aware_training_tutorial.md) 量化训练让模型感知量化运算对模型精度带来的影响,通过finetune训练降低量化误差。
- [动态离线量化](quant_post_dynamic_tutorial.md) 动态离线量化仅将模型中特定算子的权重从FP32类型映射成INT8/16类型。
- [静态离线量化](quant_post_static_tutorial.md) 静态离线量化使用少量无标签校准数据,采用KL散度等方法计算量化比例因子。
- [量化训练](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/quant/static/quant_aware_tutorial.html) 量化训练让模型感知量化运算对模型精度带来的影响,通过finetune训练降低量化误差。
- [动态离线量化](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/quant/static/quant_post_tutorial.html) 动态离线量化仅将模型中特定算子的权重从FP32类型映射成INT8/16类型。
- [静态离线量化](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/quant/static/quant_post_tutorial.html) 静态离线量化使用少量无标签校准数据,采用KL散度等方法计算量化比例因子。
除此之外,PaddleSlim还有一种对embedding层量化的方法,将网络中embedding层参数从float32类型量化到int8类型。
- [Embedding量化](../static/embedding_quant_tutorial.md) Embedding量化仅将embedding参数从float32类型映射到int8类型,可以降低embedding参数体积。
- [Embedding量化](https://paddleslim.readthedocs.io/zh_CN/latest/tutorials/quant/static/embedding_quant_tutorial.html) Embedding量化仅将embedding参数从float32类型映射到int8类型,可以降低embedding参数体积。
下图展示了如何根据需要选择模型量化方法
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册