目录
Network Compression
- Smaller Model: Deploying ML models in resource-constrained environments
We will not talk about hard-ware solution today.
Network Pruning (网络剪枝)
Network can be pruned
- Networks are typically over-parameterized (there is significant redundant weights or neurons) ⇒ \Rightarrow ⇒ Prune them!
Network Pruning
- Evaluate the Importance:
- Importance of a weight (Weight pruning): absolute values (接近 0 的参数可以被认为是不必要的), life long (类似于 life long learning 中计算参数重要性 b i b_i bi 的方法) …
- Importance of a neuron (Neuron pruning): the number of times it wasn’t zero on a given data set ……
- After pruning, the accuracy will drop (hopefully not too much). Fine-tuning on training data for recover (Don’t prune too much at once, or the network won’t recover.)
Weight pruning
- The network architecture becomes irregular
⇒
\Rightarrow
⇒ Hard to implement, hard to speedup …… (首先,Weight pruning 比较难在 Pytorch 等框架上部署,其次,如果把剪掉的 weight 值设为 0,其实并没有减少模型的规模)
上图中 Sparsity 表示被剪枝的 weight 百分比 (注意被剪枝的权重可以达到 95% 以上);可以看到在一些层上 Speedup 甚至是小于 1 的,剪枝后速度变得更慢了
Neuron pruning
- Easy to implement, easy to speedup ……
Why Pruning?
- How about simply train a smaller network?
- It is widely known that smaller network is more difficult to learn successfully. (Larger network is easier to optimize?)
- Lottery Ticket Hypothesis: 大的网络可以看作许多小的网络的组合,可能更容易被训练
在 Lottery Ticket Hypothesis 的论文中,作者发现在将一个训练好的大网络剪枝后 (weight pruning),如果将得到的小网络参数重新随机初始化并加以训练,得到的效果是不好的,但如果用原先大网络中随机初始化的参数作为初始值加以训练,就可以得到不错的效果
“sign-ificance” of initial weights: Keeping the sign is critical: 在 Deconstructing Lottery Tickets: Zeros, Signs, and the Supermask 这篇论文中,作者发现如果要在训练剪枝后得到的小网络时得到好的效果,关键是要使小网络参数的初始值符号与原来大网络的初始值符号一致:
但上述说法还停留在假说阶段,在 Rethinking the Value of Network Pruning 中,作者发现如果对剪枝后的小网络训练足够多的 epoch,也能达到不错的效果,并认为 Lottery Ticket Hypothesis 只在学习率较小且使用 weight pruning 时才能被观察到
Knowledge Distillation (知识蒸馏)
- paper:
- Knowledge Distillation: Teacher network + Student network; 让学生网络的输出逼近教师网络的输出
- e.g. 在下图中,学生网络不仅可以学到要输出 1,还可以学到 1 和 7 很相似这一信息
- e.g. 在下图中,学生网络不仅可以学到要输出 1,还可以学到 1 和 7 很相似这一信息
- Temperature for softmax: 使教师网络的输出更加平滑
- Teacher network 也可以利用集成学习:
Parameter Quantization (权重量化)
- (1) Using less bits to represent a value: e.g. float8
- (2) Weight clustering: 用某一个参数代表一群参数 (可以在训练时要求参数之间比较接近,训练结束后再使用 Weight clustering 压缩模型)
- (3) Represent frequent clusters by less bits, represent rare clusters by more bits. e.g. Huffman encoding
- (4) Binary Weights: Your weights are always +1 or -1
- paper: Binary Connect、Binary Network、XNOR-net
Architecture Design
Low rank approximation (低秩分解)
- 假设
M
M
M 和
N
N
N 层的神经元数量都很多,我们可以在
M
M
M 和
N
N
N 中间加上一层,神经元数量为
K
K
K,达到减少参数量的目的
W = U V W=UV W=UV,这样近似降低了 W W W 秩的上界
也可以简单地用 SVD 分解或 PQ 分解来直接对原始权重矩阵进行分解
Depthwise Separable Convolution (深度可分离卷积)
Depthwise Convolution
- Each filter only considers one channel: Filter number = Input channel number
- There is no interaction between channels ⇒ \Rightarrow ⇒ Pointwise Convolution
Pointwise Convolution
- The filters are
1
×
1
1\times1
1×1 matrices
Depthwise Separable Convolution v.s. Convolution
- Depthwise Separable Convolution: k × k × I + I × O k\times k \times I + I\times O k×k×I+I×O
- Convolution:
k
×
k
×
I
×
O
k\times k\times I\times O
k×k×I×O
k × k × I + I × O k × k × I × O = 1 O + 1 k × k \frac{k\times k \times I + I\times O}{k\times k\times I\times O}=\frac{1}{O}+\frac{1}{k\times k} k×k×I×Ok×k×I+I×O=O1+k×k1
More
- 分组卷积
- 全局池化代替全连接层
- 1 × 1 1\times1 1×1 卷积实现通道降维来降低模型计算量
- BN 层合并: 在训练检测模型时,BN 层可以有效加速收敛,并在一定程度上防止模型的过拟合,但在前向测试时,BN 层的存在也增加了多余的计算量。由于测试时 BN 层的参数已经固定,因此可以在测试时将 BN 层的计算合并到卷积层,从而减少计算量,实现模型加速
- …
To learn more ……
Dynamic Computation
- Dynamic Computation: The network adjusts the computation it need. (e.g. 在嵌入式设备上使用较少运算量、电量较少时使用较少运算量)
Dynamic Depth
- 如下图所示,可以在训练时 minimize
L
L
L,然后根据需要选择使用
y
′
,
y
′
′
.
.
.
y',y''...
y′,y′′... 作为输出
- To learn more: Multi-Scale Dense Networks for Resource Efficient Image Classification
Dynamic Width
- paper: Slimmable Neural Networks
Computation based on Sample Difficulty
- 根据任务的难度,让网络自行决定需要的宽度和深度
To learn more…
- SkipNet: Learning Dynamic Routing in Convolutional Networks
- Runtime Neural Pruning
- BlockDrop: Dynamic Inference Paths in Residual Networks
References
- 李宏毅 2021 ML 课程
- 《深度学习之 PyTorch 物体检测实战》