PyTorch implementation of Data Free Quantization Through Weight Equalization and Bias Correction with some ideas from ZeroQ: A Novel Zero Shot Quantization Framework.
Int8**: Fake quantization; 8 bits weight, 8 bits activation, 16 bits bias
Int8*: Fake quantization; 8 bits weight, 8 bits activation, 8 bits bias
Int8': Fake quantization; 8 bits weight(symmetric), 8 bits activation(symmetric), 32 bits bias
Int8: Int8 Inference using ncnn; 8 bits weight(symmetric), 8 bits activation(symmetric), 32 bits bias
- Tested with MobileNetV2 and ResNet-18
- ImageNet validation set (Acc.)
MobileNetV2 | ResNet-18 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
- Tested with Deeplab-v3-plus_mobilenetv2
Pascal VOC 2012 val set (mIOU) | Pascal VOC 2007 test set (mIOU) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
- Tested with MobileNetV2 SSD-Lite model
Pascal VOC 2012 val set (mAP with 12 metric) | Pascal VOC 2007 test set (mAP with 07 metric) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
There are 6 arguments, all default to False
- quantize: whether to quantize parameters and activations.
- relu: whether to replace relu6 to relu.
- equalize: whether to perform cross layer equalization.
- correction: whether to apply bias correction
- clip_weight: whether to clip weights in range [-15, 15] (for convolution and linear layer)
- distill_range: whether to use distill data for setting min/max range of activation quantization
run the equalized model by:
python main_cls.py --quantize --relu --equalize
run the equalized and bias-corrected model by:
python main_cls.py --quantize --relu --equalize --correction
run the equalized and bias-corrected model with distilled data by:
python main_cls.py --quantize --relu --equalize --correction --distill_range
export equalized and bias-corrected model to onnx and generage calibration table file:
python convert_ncnn.py --equalize --correction --quantize --relu --ncnn_build path_to_ncnn_build_folder
According to recent paper ZeroQ, we can distill some fake data to match the statistics from batch-normalization layers, then use it to set the min/max value range of activation quantization.
It does not need each conv followed by batch norm layer, and should produce better and more stable results using distilled data (the method from DFQ sometimes failed to find a good enough value range).
Here are some modifications that differs from original ZeroQ implementation:
- Initialization of distilled data
- Early stop criterion
Also, I think it can be applied to optimizing cross layer equalization and bias correction. The results will be updated as long as I make it to work.
Using distilled data to do LE or BC did not perform as good as using estimation from batch norm layers, probably because of overfitting.
The 'Int8' model in this repo is actually simulation of 8 bits, the actual calculation is done in floating points.
This is done by quantizing-dequantizing parameters in each layer and activation between 2 consecutive layers;
Which means each tensor will have dtype 'float32', but there would be at most 256 (2^8) unique values in it.
Weight_quant(Int8) = Quant(Weight)
Weight_quant(FP32) = Weight_quant(Int8*) = Dequant(Quant(Weight))
Somehow I cannot make Bias-Correction work on 8-bits bias quantization for all scenarios (even with data dependent correction).
I am not sure how the original paper managed to do it with 8 bits quantization, but I guess they either use some non-uniform quantization techniques or use more bits for bias parameters as I do.
Refer to ncnn, pytorch2ncnn, ncnn-quantize, ncnn-int8-inference for more details.
You will need to install/build the followings:
ncnn
onnx-simplifier
Inference_cls.cpp only implements mobilenetv2. Basic steps are:
- Run convert_ncnn.py to convert pytorch model (with layer equalization or bias correction) to ncnn int8 model and generate calibration table file. The name of out_layer will be printed to console.
python convert_ncnn.py --quantize --relu --equalize --correction
- compile inference_cls.cpp
mkdir build
cd build
cmake ..
make
- Inference! link
./inference_cls --images=path_to_imagenet_validation_set --param=../modeling/ncnn/model_int8.param --bin=../modeling/ncnn/model_int8.bin --out_layer=name_from_step1
- cross layer equalization
- high bias absorption
- data-free bias correction
- test with detection model
- test with classification model
- use distilled data to set min/max activation range
-
use distilled data to find optimal scale matrix -
use distilled data to do bias correction - True Int8 inference
- https://github.com/jfzhang95/pytorch-deeplab-xception
- https://github.com/ricky40403/PyTransformer
- https://github.com/qfgaohao/pytorch-ssd
- https://github.com/tonylins/pytorch-mobilenet-v2
- https://github.com/xxradon/PytorchToCaffe
- https://github.com/amirgholami/ZeroQ