Home

Awesome

Caffe-Int8-Convert-Tools

This convert tools is base on TensorRT 2.0 Int8 calibration tools, which use the KL algorithm to find the suitable threshold to quantize the activions from Float32 to Int8(-127 - 127).

We provide the Classification(SqueezeNet_v1.1) and Detection(MobileNet_v1 SSD 300) demo based on ncnn(a high-performance neural network inference framework optimized for the mobile platform) and the community ready to support this implementation.

The pull request in ncnn

NCNN have a new convert tool to support Post-Training-Quantization

Using this new ncnn-quantization-tools, you can convert your ncnn model to ncnn int8 model directly. If you just want to deploy your model with ncnn,I suggest you use it.

Reference

For details, please read the following PDF:

8-bit Inference with TensorRT

MXNet quantization implementation:

Quantization module for generating quantized (INT8) models from FP32 models

An introduction to the principles of a Chinese blog written by my friend(bruce.zhang):

The implement of Int8 quantize base on TensorRT

HowTo

The purpose of this tool(caffe-int8-convert-tool-dev.py) is to test new features, such as mulit-channels quantization depend on group num.

This format is already supported in the ncnn latest version. I will do my best to transform some common network models into classification-dev

python caffe-int8-convert-tool-dev-weight.py -h
usage: caffe-int8-convert-tool-dev-weight.py [-h] [--proto PROTO] [--model MODEL]
                                  [--mean MEAN MEAN MEAN] [--norm NORM]
                                  [--images IMAGES] [--output OUTPUT]
                                  [--group GROUP] [--gpu GPU]

find the pretrained caffemodel int8 quantize scale value

optional arguments:
  -h, --help            show this help message and exit
  --proto PROTO         path to deploy prototxt.
  --model MODEL         path to pretrained caffemodel
  --mean MEAN           value of mean
  --norm NORM           value of normalize(scale value or std value)
  --images IMAGES       path to calibration images
  --output OUTPUT       path to output calibration table file
  --group GROUP         enable the group scale(0:disable,1:enable,default:1)
  --gpu GPU             use gpu to forward(0:disable,1:enable,default:0)
python caffe-int8-convert-tool-dev-weight.py --proto=test/models/mobilenet_v1.prototxt --model=test/models/mobilenet_v1.caffemodel --mean 103.94 116.78 123.68 --norm=0.017 --images=test/images/ output=mobilenet_v1.table --group=1 --gpu=1

How to use the output file(calibration-dev.table)

For example in MobileNet_v1_dev.table

conv1_param_0 0.0 3779.48337933 482.140562772 1696.53814502
conv2_1/dw_param_0 0 72.129143 149.919382 // the convdw layer's weight scale every group is 0.0 72.129 149.919 ......
......
conv1 49.466518
conv2_1/dw 123.720796 // the convdw layer's bottom blobchannel scale is 123.720
......

Three steps to implement the conv1 layer int8 convolution:

  1. Quantize the bottom_blob and weight:

    bottom_blob_int8 = bottom_blob_float32 * data_scale(49.466518)
    weight_int8 = weight_float32 * weight_scale(156.639840)
    
  2. Convolution_Int8:

    top_blob_int32 = bottom_blob_int8 * weight_int8
    
  3. Dequantize the TopBlob_Int32 and add the bias:

    top_blob_float32 = top_blob_int32 / [data_scale(49.466518) * weight_scale(156.639840)] + bias_float32
    

How to use with ncnn

quantized int8 inference

Accuracy and Performance

We use ImageNet2012 Dataset to complete some classification test.

TypeDetail
Calibration DatasetILSVRC2012_img_test   1k
Test DatasetILSVRC2012_img_val    5k
Frameworkncnn
Support LayerConvolution,ConvolutionDepthwise,ReLU

The following table show the Top1 and Top5 different between Float32 and Int8 inference.

ModelsFP32INT8Loss
Top1Top5Top1Top5Diff Top1Diff Top5
SqueezeNet v1.157.78%79.88%57.82%79.84%+0.04%-0.04%
MobileNet v167.26%87.92%66.74%87.43%-0.52%-0.49%
GoogleNet68.50%88.84%68.62%88.68%+0.12%-0.16%
ResNet1865.49%86.56%65.30%86.52%-0.19%-0.04%
ResNet5071.80%89.90%71.76%90.06%-0.04%+0.16%

We use VOC0712,MSCOCO Dataset to complete some detection test.

TypeDetail
Test DatasetVOC2007
UnitmAP (Class 20)
ModelsFP32INT8Loss
SqueezeNet SSD61.8061.27-0.53
MobileNet_v1 SSD70.4968.92-1.57

Speed up

The following table show the speedup between Float32 and Int8 inference. It should be noted that the winograd algorithm is enable in the Float32 and Int8 inference. The Hardware Platform is Hisi3519(Cortex-A17@880MHz)

Uint(ms)SqueezeNet v1.1MobileNet v1GoogleNetResNet18MobileNetv1 SSDSqueezeNet SSD
Float322824901107985970610
Int8192369696531605498
Ratiox1.46x1.33x1.59x1.85x1.60x1.22

Memory reduce

Runtime Memory : mbytes

Modelsfp32-wino63int8-wino23int8-wino43
squeezenet_v1_1503032
mobilenet_v1613535
mobilenet_v1_ssd904545
squeezenet_v1_ssd2107094
resnet1833577130
googlenet_v11547289

Storage Memory : mbytes

Modelsfp32int8
squeezenet_v1_14.711.20
mobilenet_v116.34.31
mobilenet_v1_ssd22.05.60
squeezenet_v1_ssd21.15.37
resnet1844.611.2
googlenet_v126.66.72

Contributor

Thanks to NVIDIA for providing the principle of correlation entropy and ncnn's author nihui sharing his neural network inference framework.

Thanks to the help from the following friends:

Optimization Instructor : Fugangping, bruce.zhang

Algorithm : xupengfeixupf, JansonZhu, wangxinwei, lengmm

Python : daquexian

License

BSD 3 Clause