在tensorflow lite中获得量化的激活

时间:2018-09-17 13:39:10

标签: tensorflow-lite

我正在尝试在tf lite中获取中间特征图值。

我使用解释程序加载量化的mobilenet v1 224 tflite模型,并使用样本输入数据调用调用。

网络输出似乎正确,但是当我查看get_tensor的中间输出(写为图像)的输出时,其中一些似乎已损坏,好像被以后的操作覆盖了一样(请参见示例图像)。

是否有一种方法可以检索所有层的正确量化输出?

我构建了最新的TF 1.10.1

Conv2d_1_pointwise-Relu6_chan_3

Conv2d_2_pointwise-Relu6_chan_11

2 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,方法是从原来的冻结状态转换为所有操作,然后将其添加到输出列表中。

我看到有时边界是错误的,例如右图在下图中有一列白色像素,但这是一个不同的问题。

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

答案 1 :(得分:0)

从 tensorflow-2.5.0 开始,您可以选择“experimental_preserve_all_tensors”来配置您的解释器,例如

interpreter = tf.lite.Interpreter(
    model_path="test.tflite",
    experimental_preserve_all_tensors=True)

它将在您调用它后保留所有中间激活。