Tensorflow Lite toco --mean_values --std_values?

2020-06-27 08:45发布

问题:

So I have trained a tensorflow model with fake quantization and froze it with a .pb file as output. Now I want to feed this .pb file to tensorflow lite toco for fully quantization and get the .tflite file.

I am using this tensorflow example: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/experimental/micro/examples/micro_speech

The part where I have question:

bazel run tensorflow/lite/toco:toco -- \
--input_file=/tmp/tiny_conv.pb --output_file=/tmp/tiny_conv.tflite \
--input_shapes=1,49,43,1 --input_arrays=Reshape_1 --output_arrays='labels_softmax' \
--inference_type=QUANTIZED_UINT8 --mean_values=0 --std_values=2 \
--change_concat_input_ranges=false

The above part calls toco and does the convert. Note that, the mean_values is set to 0, and std_values is set to 2 by Google. How did they calculate these 2 values? For this particular model, it is trained to recognize words "yes" and "no". What if I want to recognize the 10 digits, do I need to change the mean and std values in this case? I didn't find any official documentation illustrating this part. Any help would be appreciated.

回答1:

For uint8 quantized models the input values are expected to be in the range 0 to 255. Even with FakeQuantization, the input values during training are often float values in a different range (for example, 0.0 to 1.0). The mean_value and std_value controls how the uint8 values in the range 0 to 255 map to the float values used during training. You can use this heuristic to determine these values:

mean_value = the uint8 value in the range [0, 255] that corresponds to floating point 0.0. So if the float range is [0.0, 1.0], then mean_value = 0.

std_value = (uint8_max - uint8_min) / (float_max - float_min). So if the float range is [0.0, 1.0], then std_value = 255 / 1.0 = 255.

We are working on ways to make this simpler. Hope this helps!