Channel Pruning for Accelerating Very Deep Neural Networks

https://github.com/yihui-he/channel-pruning

 

ICCV 2017, by Yihui HeXiangyu Zhang and Jian Sun

Please have a look at AMC: AutoML for Model Compression and Acceleration on Mobile Devices ECCV'18, which combines channel pruning and reinforcement learning to further accelerate CNN.

In this repository, we released code for the following models:

modelSpeed-upAccuracy
VGG-16 channel pruning5x88.1 (Top-5), 67.8 (Top-1)
VGG-16 3C14x89.9 (Top-5), 70.6 (Top-1)
ResNet-502x90.8 (Top-5), 72.3 (Top-1)
faster RCNN2x36.7 (AP@.50:.05:.95)
faster RCNN4x35.1 (AP@.50:.05:.95)

1 3C method combined spatial decomposition (Speeding up Convolutional Neural Networks with Low Rank Expansions) and channel decomposition (Accelerating Very Deep Convolutional Networks for Classification and Detection) (mentioned in 4.1.2)

i2i1
Structured simplification methodsChannel pruning (d)

Citation

If you find the code useful in your research, please consider citing:

@InProceedings{He_2017_ICCV,
author = {He, Yihui and Zhang, Xiangyu and Sun, Jian},
title = {Channel Pruning for Accelerating Very Deep Neural Networks},
booktitle = {The IEEE International Conference on Computer Vision (ICCV)},
month = {Oct},
year = {2017}
}

Contents

  1. Requirements
  2. Installation
  3. Channel Pruning and finetuning
  4. Pruned models for download
  5. Pruning faster RCNN
  6. FAQ

requirements

  1. Python3 packages you might not have: scipysklearneasydict, use sudo pip3 install to install.
  2. For finetuning with 128 batch size, 4 GPUs (~11G of memory)

Installation (sufficient for the demo)

  1. Clone the repository

    # Make sure to clone with --recursive
    git clone --recursive https://github.com/yihui-he/channel-pruning.git
  2. Build my Caffe fork (which support bicubic interpolation and resizing image shorter side to 256 then crop to 224x224)

    cd caffe
    
    # If you're experienced with Caffe and have all of the requirements installed, then simply do:
    make all -j8 && make pycaffe
    # Or follow the Caffe installation instructions here:
    # http://caffe.berkeleyvision.org/installation.html
    
    # you might need to add pycaffe to PYTHONPATH, if you've already had a caffe before
  3. Download ImageNet classification dataset http://www.image-net.org/download-images

  4. Specify imagenet source path in temp/vgg.prototxt (line 12 and 36)

Channel Pruning

For fast testing, you can directly download pruned model. See next section

  1. Download the original VGG-16 modelhttp://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel
    move it to temp/vgg.caffemodel (or create a softlink instead)

  2. Start Channel Pruning

    python3 train.py -action c3 -caffe [GPU0]
    # or log it with ./run.sh python3 train.py -action c3 -caffe [GPU0]
    # replace [GPU0] with actual GPU device like 0,1 or 2
  3. Combine some factorized layers for further compression, and calculate the acceleration ratio. Replace the ImageData layer of temp/cb_3c_3C4x_mem_bn_vgg.prototxt with temp/vgg.prototxt's

    ./combine.sh | xargs ./calflop.sh
  4. Finetuning

    caffe train -solver temp/solver.prototxt -weights temp/cb_3c_vgg.caffemodel -gpu [GPU0,GPU1,GPU2,GPU3]
    # replace [GPU0,GPU1,GPU2,GPU3] with actual GPU device like 0,1,2,3
  5. Testing

    Though testing is done while finetuning, you can test anytime with:

    caffe test -model path/to/prototxt -weights path/to/caffemodel -iterations 5000 -gpu [GPU0]
    # replace [GPU0] with actual GPU device like 0,1 or 2

Pruned models (for download)

For fast testing, you can directly download pruned model from releaseVGG-16 3C 4XVGG-16 5XResNet-50 2X. Or follow Baidu Yun Download link

Test with:

caffe test -model channel_pruning_VGG-16_3C4x.prototxt -weights channel_pruning_VGG-16_3C4x.caffemodel -iterations 5000 -gpu [GPU0]
# replace [GPU0] with actual GPU device like 0,1 or 2
代码修剪卷积神经网络用于资源高效推理,是一种优化模型的方法,旨在减小模型的尺寸和计算量,从而实现在资源受限的设备上进行高效推理。 修剪是指通过删除模型中的一些参数或神经元来减小模型的复杂性。在卷积神经网络中,修剪通常包括删除一些卷积核或通道,以及减少连接权重的数量。这可以通过一些算法和技术来实现,例如剪枝算法、稀疏矩阵和低秩近似等。 修剪卷积神经网络可以带来多个好处。首先,它可以大大减小模型的尺寸,从而降低了存储模型所需的内存空间。其次,修剪可以减少模型的计算量,使得模型可以更快地进行推理。这对移动设备和嵌入式系统等资源受限的设备非常重要。此外,修剪还可以提高模型的泛化能力,减少过拟合的风险。 对于修剪卷积神经网络的代码实现,一般分为两个步骤。首先,需要训练一个初始的卷积神经网络模型。然后,通过一些修剪算法选择要修剪的参数或神经元,并将其从模型中移除。修剪的目标可以是按照权重大小或梯度大小进行选择。 在实际编程中,代码可以使用一些深度学习框架(如PyTorch或TensorFlow)来实现。这些框架通常提供了一些修剪工具和函数,以便进行参数和神经元的修剪。开发者需要根据具体的应用场景和需求来选择合适的修剪策略,并根据框架的API来实现修剪过程。 总之,代码修剪卷积神经网络是一种资源高效推理的方法,通过减小模型的尺寸和计算量,提高模型的效率和性能。这对于在资源受限的设备上进行深度学习推理任务非常有意义。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值