• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reducing the size of convolutional neural networks

Trained Ternary Quantization

pytorch implementation of Trained Ternary Quantization, a way of replacing full precision weights of a neural network by ternary values. I tested it on Tiny ImageNet dataset. The dataset consists of 64x64 images and has 200 classes.

The quantization roughly proceeds as follows.

  1. Train a model of your choice as usual (or take a trained model).
  2. Copy all full precision weights that you want to quantize. Then do the initial quantization:
    in the model replace them by ternary values {-1, 0, +1} using some heuristic.
  3. Repeat until convergence:
    • Make the forward pass with the quantized model.
    • Compute gradients for the quantized model.
    • Preprocess the gradients and apply them to the copy of full precision weights.
    • Requantize the model using the changed full precision weights.
  4. Throw away the copy of full precision weights and use the quantized model.

Results

I believe that this results can be made better by spending more time on hyperparameter optimization.

model accuracy, % top5 accuracy, % number of parameters
DenseNet-121 74 91 7151176
TTQ DenseNet-121 66 87 ~7M 2-bit, 88% are zeros
small DenseNet 49 75 440264
TTQ small DenseNet 40 67 ~0.4M 2-bit, 38% are zeros
SqueezeNet 52 77 827784
TTQ SqueezeNet 36 63 ~0.8M 2-bit, 66% are zeros

Implementation details

  • I use pretrained DenseNet-121, but I train SqueezeNet and small DenseNet from scratch.
  • I modify the SqueezeNet architecture by adding batch normalizations and skip connections.
  • I quantize all layers except the first CONV layer, the last FC layer, and all BATCH_NORM layers.

How to reproduce results

For example, for small DenseNet:

  1. Download Tiny ImageNet dataset and extract it to ~/data folder.
  2. Run python utils/move_tiny_imagenet_data.py to prepare the data.
  3. Go to vanilla_densenet_small/. Run train.ipynb to train the model as usual.
    Or you can skip this step and use model.pytorch_state (the model already trained by me).
  4. Go to ttq_densenet_small/.
  5. Run train.ipynb to do TTQ.
  6. Run test_and_explore.ipynb to explore the quantized model.

To use this on your data you need to edit utils/input_pipeline.py and to change the model architecture in files like densenet.py and get_densenet.py as you like.

Requirements

  • pytorch 0.2, Pilllow, torchvision
  • numpy, sklearn, tqdm, matplotlib

More Repositories

1

mtcnn-pytorch

Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks
Jupyter Notebook
648
star
2

knowledge-distillation-keras

A machine learning experiment
Jupyter Notebook
182
star
3

FaceBoxes-tensorflow

A fast face detector
Python
178
star
4

shufflenet-v2-tensorflow

A lightweight convolutional neural network
Jupyter Notebook
152
star
5

lda2vec-pytorch

Topic modeling with word vectors
Jupyter Notebook
115
star
6

image-classification-caltech-256

Exploring CNNs and model quantization on Caltech-256 dataset
Jupyter Notebook
83
star
7

wing-loss

A facial landmarks regressor
Jupyter Notebook
71
star
8

ShuffleNet-tensorflow

A ShuffleNet implementation tested on Tiny ImageNet dataset
Jupyter Notebook
41
star
9

light-head-rcnn

Python
23
star
10

set-transformer

A neural network architecture for prediction on sets
Python
21
star
11

single-shot-detector

A lightweight version of RetinaNet
Python
16
star
12

MultiPoseNet

Python
9
star
13

associative-domain-adaptation

A simple domain adaptation example
Python
8
star
14

multi-scale-gradient-gan

Generation of high resolution fashion images
Python
7
star
15

WESPE

Manipulating image quality using GANs
Python
6
star
16

bicycle-gan

Multimodal edges to image translation
Python
5
star
17

point-cloud-autoencoder

Python
5
star
18

contextual-loss

Jupyter Notebook
3
star
19

CNNMRF

Jupyter Notebook
3
star
20

universal-style-transfer

Python
2
star
21

EDANet

Python
2
star
22

U-GAT-IT

Unsupervised Image-to-Image Translation
Python
2
star
23

maxout-networks-tensorflow

A neural network with maxout activation units
Python
1
star
24

large-shufflenet-tpu

Python
1
star