Obtaining quantized activations in tensorflow lite

2019-09-01 05:28发布

问题:

I'm trying to get intermediate feature map values in tf lite.

I load the quantized mobilenet v1 224 tflite model using the interpreter and call invoke using sample input data.

The network output seems correct but when I look at the output of get_tensor for intermediate outputs (written as images) some of them seem corrupted as if overwritten by later ops (see sample images).

Is there a way to retrieve the correct quantized outputs for all layers?

I built the current latest TF 1.10.1

Conv2d_1_pointwise-Relu6_chan_3

Conv2d_2_pointwise-Relu6_chan_11

回答1:

I managed to fix the problem by converting from the original frozen and making all ops be in the output list.

I see that sometimes the borders are wrong, e.g. the right side has a column of white pixels in the image below, but this is a different problem.

Conv2d_1_pointwise-Relu6_chan_13

bazel run //tensorflow/contrib/lite/python:tflite_convert -- \
--output_file=toco_mobilenet_v1_1.0_224_quant.tflite \
--graph_def_file=mobilenet_v1_1.0_224_quant/mobilenet_v1_1.0_224_quant_frozen.pb \
--inference_type=QUANTIZED_UINT8 \
--mean_values=128 \
--std_dev_values=127 \
--input_arrays=input \
--output_arrays=MobilenetV1/MobilenetV1/Conv2d_0/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_1_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_1_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_2_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_2_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_3_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_3_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_4_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_4_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_5_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_5_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_6_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_6_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_7_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_7_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_8_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_8_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_9_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_9_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_10_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_10_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_11_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_11_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_12_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_12_pointwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_13_depthwise/Relu6,\
MobilenetV1/MobilenetV1/Conv2d_13_pointwise/Relu6,\
MobilenetV1/Logits/AvgPool_1a/AvgPool,\
MobilenetV1/Logits/Conv2d_1c_1x1/BiasAdd,\
MobilenetV1/Logits/SpatialSqueeze,\
MobilenetV1/Predictions/Reshape_1