python - 用于量化感知训练的 TF Lite 的 Toco 转换器参数说明

标签 python python-3.x tensorflow tensorflow-lite

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

我可以在没有 TPU 支持的情况下运行模型,但一旦启用量化,我就会迷失方向。

我处于以下情况:

  1. 创建模型并对其进行训练
  2. 创建了模型的评估图
  3. 卡住模型并将结果保存为 Protocol Buffer
  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)

这告诉我,到目前为止我的方法似乎没问题。现在,如果我想使用 Coral 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、min/max 的含义是什么以及它们如何相互作用?
  2. 我在转换过程中做错了什么?

非常感谢您的帮助或指导!

编辑:我打开了 github issue以及完整的测试代码。请随意尝试一下。

最佳答案

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

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

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

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

关于python - 用于量化感知训练的 TF Lite 的 Toco 转换器参数说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57077974/

相关文章:

python - 如何使用 math.atan2 函数检测一个对象是否在另一个对象的 "line of sight"中?

Python 无法在 Debian Lenny 上编译 _curses 模块

python - 奇怪的语法错误,我不知道如何解决

python - “顺序”对象没有属性 '_in_multi_worker_mode'

computer-vision - 在适度的硬件设置上培训Tensorflow Inception-v3 Imagenet

python - Pandas 数据框获取每组的第一行

python-3.x - 有什么方法可以附加相同的日志文件而不是在 python 3.x 中创建新文件?

python 3.4 :ImportError: no module named win32api

python - 如何在 TFRecord 中保存不同长度的列表列表?

python - Django - 电子邮件发送两次