/channel-pruning

Channel Pruning for Accelerating Very Deep Neural Networks

Primary LanguagePython

[配置的时候,如果是ubuntu14,里面会有python3.4。这里的caffe必须用python3来make && make py. 在makefile.config里面需要设置python3.4的路径:]


PYTHON_LIBRARIES := boost_python3 python3.4m PYTHON_INCLUDE := /usr/include/python3.4m
/usr/lib/python3/dist-packages/numpy/core/include

[在配置caffe环境前面,应该吧所有的protobuf都省略,在最开始自己安装最新的protobuf,如果不是最新的protoc,后面便宜的pycaffe始终运行不起,具体安装方式就是clone github上的protobuf,按上面的命令来安装这个库]

[但是可能发现没有相关哦路径,这个时候,sudo apt-get install python3-pip 就会生成相关的路径,接下来,安装哦一切包都用pip3来装]

用pip3之前也应该pip3 install --upgrade pip来升级pip3命令 用pip3的时候,可以在命令后面加  -i https:// 的国内源,来加快下载速度.(注意,现在国内源都是https!)


有可能会提示没有tkinter的东西 : 安装 : sudo apt-get install python3-tk

pip/pip3国内源: 新版ubuntu要求使用https源,要注意。 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ **科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 华中理工大学:http://pypi.hustunique.com/ 山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/ 临时使用: 可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple

ICCV 2017, by Yihui He, Xiangyu Zhang and Jian Sun

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

model Speed-up Accuracy
VGG-16 channel pruning 5x 88.1 (Top-5), 67.8 (Top-1)
VGG-16 3C1 4x 89.9 (Top-5), 70.6 (Top-1)
ResNet-50 2x 90.8 (Top-5), 72.3 (Top-1)
faster RCNN 2x 36.7 (AP@.50:.05:.95)
faster RCNN 4x 35.1 (AP@.50:.05:.95)
i2 i1
Structured simplification methods Channel 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: scipy, sklearn, easydict, 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 model http://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 release: VGG-16 3C 4X, VGG-16 5X, ResNet-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

Pruning faster RCNN

For fast testing, you can directly download pruned model from release
Or you can:

  1. clone my py-faster-rcnn repo: https://github.com/yihui-he/py-faster-rcnn
  2. use the pruned models from this repo to train faster RCNN 2X, 4X, solver prototxts are in https://github.com/yihui-he/py-faster-rcnn/tree/master/models/pascal_voc

FAQ

You can find answers of some commonly asked questions in our Github wiki, or just create a new issue