TF Lite的Toco转换器参数的描述,用于量化感知训练



这些天,我正在尝试追踪有关部署具有TPU支持的TF模型的错误。

我可以在没有 TPU 支持的情况下获得模型,但一旦启用量化,我就会迷路。

我处于以下情况:

  1. 创建模型并对其进行训练
  2. 创建了模型的评估图
  3. 冻结模型并将结果保存为协议缓冲区
  4. 在没有 TPU 支持的情况下成功转换和部署它

最后一点,我使用了TFLiteConverter的Python API。生成函数式 tflite 模型的脚本是

import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.FLOAT
input_arrays = converter.get_input_arrays()
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)

这告诉我,到目前为止,我的方法似乎还可以。现在,如果我想使用珊瑚 TPU 棒,我必须量化我的模型(我在训练期间考虑到了这一点)。我所要做的就是修改我的转换器脚本。我想我必须把它改成

import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.QUANTIZED_UINT8      ## Indicates TPU compatibility
input_arrays = converter.get_input_arrays()
converter.quantized_input_stats = {input_arrays[0]: (0., 1.)}     ## mean, std_dev
converter.default_ranges_stats = (-128, 127)                      ## min, max values for quantization (?)
converter.allow_custom_ops = True                                 ## not sure if this is needed
## REMOVED THE OPTIMIZATIONS ALTOGETHER TO MAKE IT WORK
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)

这个 tflite 模型在使用解释器的 Python API 加载时会产生结果,但我无法理解它们的含义。此外,没有(或者如果有,它隐藏得很好)关于如何选择平均值、std_dev和最小/最大范围的文档。此外,在使用edgetpu_compiler编译并部署它(使用 C++ API 加载它)后,我收到一个错误:

INFO: Initialized TensorFlow Lite runtime.
ERROR: Failed to prepare for TPU. generic::failed_precondition: Custom op already assigned to a different TPU.
ERROR: Node number 0 (edgetpu-custom-op) failed to prepare.
Segmentation fault

我想我在转换过程中错过了一个标志或其他东西。但是由于这里也缺少文档,我不能肯定地说。

总之:

  1. 参数是什么意思,std_dev,最小/最大值以及它们如何相互作用?
  2. 我在转换过程中做错了什么?

我非常感谢任何帮助或指导!

编辑:我已经打开了一个带有完整测试代码的github问题。随意玩这个。

您永远不需要手动设置量化统计信息。

您是否尝试过训练后量化教程?

https://www.tensorflow.org/lite/performance/post_training_integer_quant

基本上,他们设置了量化选项:

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

然后,他们将"代表性数据集"传递给转换器,以便转换器可以运行模型几批以收集必要的统计信息:

def representative_data_gen():
for input_value in mnist_ds.take(100):
yield [input_value]
converter.representative_dataset = representative_data_gen

虽然有量化训练的选项,但进行训练后量化总是更容易。

最新更新