A Closer Look at Structured Pruning for Neural Network Compression

Overview

A Closer Look at Structured Pruning for Neural Network Compression

Code used to reproduce experiments in https://arxiv.org/abs/1810.04622.

To prune, we fill our networks with custom MaskBlocks, which are manipulated using Pruner in funcs.py. There will certainly be a better way to do this, but we leave this as an exercise to someone who can code much better than we can.

Setup

This is best done in a clean conda environment:

conda create -n prunes python=3.6
conda activate prunes
conda install pytorch torchvision -c pytorch

Repository layout

-train.py: contains all of the code for training large models from scratch and for training pruned models from scratch
-prune.py: contains the code for pruning trained models
-funcs.py: contains useful pruning functions and any functions we used commonly

CIFAR Experiments

First, you will need some initial models.

To train a WRN-40-2:

python train.py --net='res' --depth=40 --width=2.0 --data_loc=
   
     --save_file='res'

   

The default arguments of train.py are suitable for training WRNs. The following trains a DenseNet-BC-100 (k=12) with its default hyperparameters:

python train.py --net='dense' --depth=100 --data_loc=
   
     --save_file='dense' --no_epochs 300 -b 64 --epoch_step '[150,225]' --weight_decay 0.0001 --lr_decay_ratio 0.1

   

These will automatically save checkpoints to the checkpoints folder.

Pruning

Once training is finished, we can prune our networks using prune.py (defaults are set to WRN pruning, so extra arguments are needed for DenseNets)

python prune.py --net='res'   --data_loc=
   
     --base_model='res' --save_file='res_fisher'
python prune.py --net='res'   --data_loc=
    
      --l1_prune=True --base_model='res' --save_file='res_l1'

python prune.py --net='dense' --depth 100 --data_loc=
     
       --base_model='dense' --save_file='dense_fisher' --learning_rate 1e-3 --weight_decay 1e-4 --batch_size 64 --no_epochs 2600
python prune.py --net='dense' --depth 100 --data_loc=
      
        --l1_prune=True --base_model='dense' --save_file='dense_l1'  --learning_rate 1e-3 --weight_decay 1e-4 --batch_size 64  --no_epochs 2600


      
     
    
   

Note that the default is to perform Fisher pruning, so you don't need to pass a flag to use it.
Once finished, we can train the pruned models from scratch, e.g.:

python train.py --data_loc=
   
     --net='res' --base_file='res_fisher_
    
     _prunes' --deploy --mask=1 --save_file='res_fisher_
     
      _prunes_scratch'

     
    
   

Each model can then be evaluated using:

python train.py --deploy --eval --data_loc=
   
     --net='res' --mask=1 --base_file='res_fisher_
    
     _prunes'

    
   

Training Reduced models

This can be done by varying the input arguments to train.py. To reduce depth or width of a WRN, change the corresponding option:

python train.py --net='res' --depth=
   
     --width=
    
      --data_loc=
     
       --save_file='res_reduced'

     
    
   

To add bottlenecks, use the following:

python train.py --net='res' --depth=40 --width=2.0 --data_loc=
   
     --save_file='res_bottle' --bottle --bottle_mult 
    

    
   

With DenseNets you can modify the depth or growth, or use --bottle --bottle_mult as above.

Acknowledgements

Jack Turner wrote the L1 stuff, and some other stuff for that matter.

Code has been liberally borrowed from many a repo, including, but not limited to:

https://github.com/xternalz/WideResNet-pytorch
https://github.com/bamos/densenet.pytorch
https://github.com/kuangliu/pytorch-cifar
https://github.com/ShichenLiu/CondenseNet

Citing this work

If you would like to cite this work, please use the following bibtex entry:

@article{crowley2018pruning,
  title={A Closer Look at Structured Pruning for Neural Network Compression},
  author={Crowley, Elliot J and Turner, Jack and Storkey, Amos and O'Boyle, Michael},
  journal={arXiv preprint arXiv:1810.04622},
  year={2018},
  }
Comments
  • Hi,how to define the maskBlock base on own model?

    Hi,how to define the maskBlock base on own model?

    my model include resnet18 as Backbone network and another three branches outputs, i want to prune the resnet18。 Now , how to define the maskBlock ? Thanks

    opened by cvJie 3
  • When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call python train.py --deploy --eval <-other-commands-> the network is pruned at runtime (by the if statement on line 73) and then used.

    If you would like to save the pruned model, you could put a:

    save_checkpoint({
                    'epoch': SD['epoch'],
                    'state_dict': model.state_dict(),
                    'error_history': SD['error_history'],
                }, filename=filename)
    

    after the call to pruner.compress(model) on line 89.

    However, it is better to leave the saved checkpoint in its original state, with the masks attached, to make it easier to recover the pruning trajectories. You can check the number of ops/params with get_inf_params(model) if you want an indication of compression rate. Whenever you want to use a pruned model for something, you then just need to run the code from lines 71 to 89 in train.py, followed by the task you want the pruned model to complete.

    Hope this helps.

    Originally posted by @jack-willturner in https://github.com/BayesWatch/pytorch-prunes/issues/1#issuecomment-541559480

    opened by cvJie 0
  • When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call python train.py --deploy --eval <-other-commands-> the network is pruned at runtime (by the if statement on line 73) and then used.

    If you would like to save the pruned model, you could put a:

    save_checkpoint({
                    'epoch': SD['epoch'],
                    'state_dict': model.state_dict(),
                    'error_history': SD['error_history'],
                }, filename=filename)
    

    after the call to pruner.compress(model) on line 89.

    However, it is better to leave the saved checkpoint in its original state, with the masks attached, to make it easier to recover the pruning trajectories. You can check the number of ops/params with get_inf_params(model) if you want an indication of compression rate. Whenever you want to use a pruned model for something, you then just need to run the code from lines 71 to 89 in train.py, followed by the task you want the pruned model to complete.

    Hope this helps.

    Originally posted by @jack-willturner in https://github.com/BayesWatch/pytorch-prunes/issues/1#issuecomment-541559480

    opened by cvJie 0
  • When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call `python train.py --deploy --eval <-other-commands->` the network is pruned at runtime (by the `if` statement on line 73) and then used.

    When you call python train.py --deploy --eval <-other-commands-> the network is pruned at runtime (by the if statement on line 73) and then used.

    If you would like to save the pruned model, you could put a:

    save_checkpoint({
                    'epoch': SD['epoch'],
                    'state_dict': model.state_dict(),
                    'error_history': SD['error_history'],
                }, filename=filename)
    

    after the call to pruner.compress(model) on line 89.

    However, it is better to leave the saved checkpoint in its original state, with the masks attached, to make it easier to recover the pruning trajectories. You can check the number of ops/params with get_inf_params(model) if you want an indication of compression rate. Whenever you want to use a pruned model for something, you then just need to run the code from lines 71 to 89 in train.py, followed by the task you want the pruned model to complete.

    Hope this helps.

    Originally posted by @jack-willturner in https://github.com/BayesWatch/pytorch-prunes/issues/1#issuecomment-541559480

    opened by cvJie 0
  • RuntimeError: running_mean should contain 57 elements not 64

    RuntimeError: running_mean should contain 57 elements not 64

    hi, I success to pruned and finetune the cifar res_model, have successed to finish the pruned model by own data and model, but and now need to finetune the pruned model ,happened some error:

    raceback (most recent call last): File "train_fashion.py", line 155, in log_dict_train=train(epoch,train_loader) File "train_fashion.py", line 117, in train log_dict_train, _ = trainer.train(epoch, train_loader) File "/fashiontrain/fashionprunes/lib/trains/base_trainer.py", line 103, in train return self.run_epoch('train', epoch, data_loader) File "/fashiontrain/fashionprunes/lib/trains/base_trainer.py", line 79, in run_epoch output, loss, loss_stats = model_with_loss(batch) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/fashiontrain/fashionprunes/lib/trains/base_trainer.py", line 19, in forward outputs = self.model(batch['input']) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/fashiontrain/fashionprunes/lib/models/networks/msra_resnet.py", line 372, in forward x = self.layer1(x) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py", line 92, in forward input = module(input) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/fashiontrain/fashionprunes/lib/models/networks/msra_resnet.py", line 137, in forward out = self.bn1(out) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/batchnorm.py", line 83, in forward exponential_average_factor, self.eps) File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 1697, in batch_norm training, momentum, eps, torch.backends.cudnn.enabled RuntimeError: running_mean should contain 57 elements not 64

    can you give me some advices?? thank you

    opened by cvJie 2
Owner
Bayesian and Neural Systems Group
Machine learning research group @ University of Edinburgh
Bayesian and Neural Systems Group
Distiller is an open-source Python package for neural network compression research.

Wiki and tutorials | Documentation | Getting Started | Algorithms | Design | FAQ Distiller is an open-source Python package for neural network compres

Intel Labs 4.1k Dec 28, 2022
PyTorch Implementation of [1611.06440] Pruning Convolutional Neural Networks for Resource Efficient Inference

PyTorch implementation of [1611.06440 Pruning Convolutional Neural Networks for Resource Efficient Inference] This demonstrates pruning a VGG16 based

Jacob Gildenblat 836 Dec 26, 2022
Code for paper "Energy-Constrained Compression for Deep Neural Networks via Weighted Sparse Projection and Layer Input Masking"

model_based_energy_constrained_compression Code for paper "Energy-Constrained Compression for Deep Neural Networks via Weighted Sparse Projection and

Haichuan Yang 16 Jun 15, 2022
A tutorial on "Bayesian Compression for Deep Learning" published at NIPS (2017).

Code release for "Bayesian Compression for Deep Learning" In "Bayesian Compression for Deep Learning" we adopt a Bayesian view for the compression of

Karen Ullrich 190 Dec 30, 2022
Fast, general, and tested differentiable structured prediction in PyTorch

Torch-Struct: Structured Prediction Library A library of tested, GPU implementations of core structured prediction algorithms for deep learning applic

HNLP 1.1k Jan 7, 2023
High-level batteries-included neural network training library for Pytorch

Pywick High-Level Training framework for Pytorch Pywick is a high-level Pytorch training framework that aims to get you up and running quickly with st

null 382 Dec 6, 2022
A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API

micrograd A tiny Autograd engine (with a bite! :)). Implements backpropagation (reverse-mode autodiff) over a dynamically built DAG and a small neural

Andrej 3.5k Jan 8, 2023
GPU-accelerated PyTorch implementation of Zero-shot User Intent Detection via Capsule Neural Networks

GPU-accelerated PyTorch implementation of Zero-shot User Intent Detection via Capsule Neural Networks This repository implements a capsule model Inten

Joel Huang 15 Dec 24, 2022
PyNIF3D is an open-source PyTorch-based library for research on neural implicit functions (NIF)-based 3D geometry representation.

PyNIF3D is an open-source PyTorch-based library for research on neural implicit functions (NIF)-based 3D geometry representation. It aims to accelerate research by providing a modular design that allows for easy extension and combination of NIF-related components, as well as readily available paper implementations and dataset loaders.

Preferred Networks, Inc. 96 Nov 28, 2022
OptNet: Differentiable Optimization as a Layer in Neural Networks

OptNet: Differentiable Optimization as a Layer in Neural Networks This repository is by Brandon Amos and J. Zico Kolter and contains the PyTorch sourc

CMU Locus Lab 428 Dec 24, 2022
Tutorial for surrogate gradient learning in spiking neural networks

SpyTorch A tutorial on surrogate gradient learning in spiking neural networks Version: 0.4 This repository contains tutorial files to get you started

Friedemann Zenke 203 Nov 28, 2022
Learning Sparse Neural Networks through L0 regularization

Example implementation of the L0 regularization method described at Learning Sparse Neural Networks through L0 regularization, Christos Louizos, Max W

AMLAB 202 Nov 10, 2022
A Closer Look at Structured Pruning for Neural Network Compression

A Closer Look at Structured Pruning for Neural Network Compression Code used to reproduce experiments in https://arxiv.org/abs/1810.04622. To prune, w

Bayesian and Neural Systems Group 140 Dec 5, 2022
A Closer Look at Invalid Action Masking in Policy Gradient Algorithms

A Closer Look at Invalid Action Masking in Policy Gradient Algorithms This repo contains the source code to reproduce the results in the paper A Close

Costa Huang 73 Dec 24, 2022
code for `Look Closer to Segment Better: Boundary Patch Refinement for Instance Segmentation`

Look Closer to Segment Better: Boundary Patch Refinement for Instance Segmentation (CVPR 2021) Introduction PBR is a conceptually simple yet effective

H.Chen 143 Jan 5, 2023
PyTorch implementation of the R2Plus1D convolution based ResNet architecture described in the paper "A Closer Look at Spatiotemporal Convolutions for Action Recognition"

R2Plus1D-PyTorch PyTorch implementation of the R2Plus1D convolution based ResNet architecture described in the paper "A Closer Look at Spatiotemporal

Irhum Shafkat 342 Dec 16, 2022
Look Closer: Bridging Egocentric and Third-Person Views with Transformers for Robotic Manipulation

Look Closer: Bridging Egocentric and Third-Person Views with Transformers for Robotic Manipulation Official PyTorch implementation for the paper Look

Rishabh Jangir 20 Nov 24, 2022
Group Fisher Pruning for Practical Network Compression(ICML2021)

Group Fisher Pruning for Practical Network Compression (ICML2021) By Liyang Liu*, Shilong Zhang*, Zhanghui Kuang, Jing-Hao Xue, Aojun Zhou, Xinjiang W

Shilong Zhang 129 Dec 13, 2022
An implementation of based on pytorch and mmcv

FisherPruning-Pytorch An implementation of <Group Fisher Pruning for Practical Network Compression> based on pytorch and mmcv Main Functions Pruning f

Peng Lu 15 Dec 17, 2022
YOLOv5 Series Multi-backbone, Pruning and quantization Compression Tool Box.

YOLOv5-Compression Update News Requirements 环境安装 pip install -r requirements.txt Evaluation metric Visdrone Model mAP mAP@50 Parameters(M) GFLOPs FPS@

ZhangYuan 719 Jan 2, 2023