## 模型量化

### 什幺是量化

XNOR网络：过滤器和卷积层的输入是二进制的。 XNOR 网络主要使用二进制运算来近似卷积。

### 量化的原理

float的范围为-2^128 ~ +2^128. 可以看到float的值域分布是极其广的。

### 如何做量化

$$x_{float} = x_{scale} \times (x_{quantized} – x_{zero\_point})$$

$$x_{scale}=2/255$$

,

$$x_{zero\_point}=255-1/(2/255)=127$$

$$z_{quantized}=127$$

,再将其转换回float32,即0.0.

## tflite_convert

tensorflow中使用tflite_convert做模型量化.用法:

tflite_convert \
--output_file=/tmp/foo.cc \
--graph_def_file=/tmp/mobilenet_v1_0.50_128/frozen_graph.pb \
--inference_type=QUANTIZED_UINT8 \
--input_arrays=input \
--output_arrays=MobilenetV1/Predictions/Reshape_1 \
--default_ranges_min=0 \
--default_ranges_max=6 \
--mean_values=128 \
--std_dev_values=127

https://www.tensorflow.org/lite/convert/cmdline_reference

(mean, std_dev)
(zero_point, scale)
(min,max)

std_dev = 1.0 / scale
mean = zero_point
mean = 255.0*min / (min - max)
std_dev = 255.0 / (max - min)

range (0,255) then mean = 0, std_dev = 1
range (-1,1) then mean = 127.5, std_dev = 127.5
range (0,1) then mean = 0, std_dev = 255

https://heartbeat.fritz.ai/8-bit-quantization-and-tensorflow-lite-speeding-up-mobile-inference-with-low-precision-a882dfcafbbd

https://stackoverflow.com/questions/54830869/understanding-tf-contrib-lite-tfliteconverter-quantization-parameters/58096430#58096430

https://arleyzhang.github.io/articles/923e2c40/

https://zhuanlan.zhihu.com/p/79744430

https://zhuanlan.zhihu.com/p/58182172