首页 文章

tensorflow lite:转换量化graphdef的错误

提问于
浏览
2

我完成了教程(https://www.tensorflow.org/performance/quantization)来生成量化的graphdef文件:

curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" | tar -C tensorflow/examples/label_image/data -xz
bazel build tensorflow/tools/graph_transforms:transform_graph
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
  --in_graph=tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb \
  --out_graph=/tmp/inception_v3_quantized_graph.pb \
  --inputs=input \
  --outputs=InceptionV3/Predictions/Reshape_1 \
  --transforms='add_default_attributes strip_unused_nodes(type=float, shape="1,299,299,3")
remove_nodes(op=Identity, op=CheckNumerics) fold_constants(ignore_errors=true)
fold_batch_norms fold_old_batch_norms quantize_weights quantize_nodes
strip_unused_nodes sort_by_execution_order'

然后将量化的graphdef转换为tflite文件:

bazel-bin/tensorflow/contrib/lite/toco/toco \
  --input_file=/tmp/inception_v3_quantized_graph.pb\
  --output_file=/tmp/inception_v3_quantized_graph.lite \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TFLITE \
  --inference_type=QUANTIZED_UINT8 --input_type=QUANTIZED_UINT8\
  --input_shape=1,299,299,3 \
  --input_array=input \
  --output_array=InceptionV3/Predictions/Reshape_1 \
  --mean_value=128 \
  --std_value=127

它因错误而失败:

2017-11-23 12:36:40.637143: F tensorflow/contrib/lite/toco/tooling_util.cc:549] Check failed: model.arrays.count(input_array.name()) Input array not found: input
Aborted (core dumped)

运行summarize_graph工具

bazel-bin/tensorflow/tools/graph_transforms/summarize_graph \
  --in_graph=/tmp/inception_v3_quantized_graph.pb

输入节点存在 .

Found 1 possible inputs: (name=input, type=float(1), shape=[1,299,299,3])
No variables spotted.
Found 1 possible outputs: (name=InceptionV3/Predictions/Reshape_1, 
op=Dequantize)
Found 23824934 (23.82M) const parameters, 0 (0) variable parameters, and 268 
control_edges
Op types used: 673 Const, 214 Requantize, 214 RequantizationRange, 134 Reshape, 134 Max, 134 Min, 134 QuantizeV2, 95 QuantizedConv2D, 94 QuantizedRelu, 94 QuantizedAdd, 49 Dequantize, 24 QuantizedMul, 15 ConcatV2, 10 QuantizedAvgPool, 4 QuantizedMaxPool, 2 QuantizedReshape, 1 QuantizedBiasAdd, 1 Placeholder, 1 Softmax, 1 Squeeze

我错过了什么吗?将量化的graphdef文件转换为tflite文件的正确方法是什么?

谢谢!

1 回答

  • 2

    我用TF(precise commit)的干净结帐再现了你的命令行 .

    我收到了Toco错误,但与您不一样:

    F tensorflow/contrib/lite/toco/tooling_util.cc:1155] Array InceptionV3/InceptionV3/Conv2d_1a_3x3/BatchNorm/batchnorm/mul_eightbit/input__port__0/min, which is an input to the (Unsupported TensorFlow op: QuantizeV2) operator producing the output array InceptionV3/InceptionV3/Conv2d_1a_3x3/BatchNorm/batchnorm/mul_eightbit/input__port__0/quantize, is lacking min/max data, which is necessary for quantization. Either target a non-quantized output format, or change the input graph to contain min/max information, or pass --default_ranges_min= and --default_ranges_max= if you do not care about the accuracy of results.

    这不是一个toco bug,而是toco在这个文件中抱怨两个问题, inception_v3_quantized_graph.pb

    • 数组(TensorFlow用语中的'tensor')缺少最小 - 最大范围信息 . 这是此错误消息的直接原因 .

    • 此图中的运算符为 QuantizeV2 . Toco并不是这个错误消息的直接主题,但如果你超过这一点,那么这是一个真正的问题,你将在以后遇到这个问题 .

    TensorFlow Lite带来了一种新的量化方法,这与之前在您提到的现有TensorFlow文档和工具中所做的不同 . 您在这里遇到的错误归结为尝试使用TensorFlow Lite转换器,该转换器需要在新方法中量化的图形,并使用旧方法量化图形 .

    我们正在记录新的量化方法 .

    同时,您可以使用这些提示进行实验 . 新的量化方法需要在浮动训练图中插入"fake quantization"节点,如下所述:https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops/fake_quantization

    这些节点的目的是准确地模拟训练期间8位量化的准确度影响,并记录用于该量化的精确最小 - 最大范围 . 将这些节点放置在正确的位置是至关重要的,因为量化训练的目的是允许在推理中再现完全相同的算术,并且量化推断需要实现整个融合层(Conv BiasAdd ReLU),(完全连接的BiasAdd ReLU) )作为单个融合操作 . 因此,应该放置fake_quant节点:

    • 在激活函数之后的每个(融合的)层(例如Conv BiasAdd ReLU)的 output activations 上(例如,在ReLU的输出上) . 不是之前(不是在BiasAdd周围) .

    • 在Conv / FullyConnected操作消耗之前的Conv /完全连接 weights 上 .

    • 不要在偏置向量上放置fake_quantization节点 .

    这只是涉及这个复杂主题的表面,这就是为什么我们花了一些时间来获得关于它的良好文档!从好的方面来说,通常应该采用试错法,让toco错误消息指导您正确放置fake_quantization节点 .

    一旦你放置了fake_quantization节点,像往常一样在TensorFlow,freeze_graph中重新训练,然后在这个例子中运行toco:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/cmdline_examples.md#convert-a-tensorflow-graphdef-to-tensorflow-lite-for-quantized-inference

    另请注意,如果您在实际应用中使用了're only interested in evaluating performance and not concerned about actual accuracy, then you can use '虚拟量化' --- running toco quantization directly on a plain float graph without having to deal with fake quantization and retraining. Just don' t! https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/cmdline_examples.md#use-dummy-quantization-to-try-out-quantized-inference-on-a-float-graph

    祝好运!

相关问题