【问题标题】:TFLite input/output quantization in multiple signatures多个签名中的 TFLite 输入/输出量化
【发布时间】:2021-09-24 11:34:34
【问题描述】:

TFLite 模型转换允许自动量化或反量化模型的输入或输出。您可以像这样适当地设置inference_input_typeinference_output_type 来做到这一点。

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

但是,根据 TensorFlow 2.7 TFLite models finally support multiple signatures。这些可以从保存的模型、Keras 模型或具体函数中自动检索。然而,这提出了一个问题:如何在签名级别为输入和输出设置量化/去量化?此外,如果一个签名有多个输入或输出,你怎么做?

似乎inference_input_typeinference_output_type 仅限于模型通过其调用方法导出的任何单输入(可能也是单输出?)函数。任何关于如何处理不同签名中特定参数的量化/去量化的提示,即使是手动的,都将受到欢迎。

【问题讨论】:

  • 一点更新。我发现在多个签名的情况下,representative_dataset(我也想inference_input_typeinference_output_type)针对的签名是第一个按字母顺序您从具体函数列表转换。它确实取决于您的函数的名称。 TF 开发人员:这确实需要改进设计。

标签: tensorflow tensorflow-lite quantization


【解决方案1】:

TensorFlow Lite 转换器也可以量化多个启用签名的模型。输入/输出覆盖也适用于他们。

def representative_dataset():
  # Feed data set for the "encode" signature.
  for data in encode_signature_dataset:
    yield (
      "encode", {
        "image": data.image,
        "bias": data.bias,
      }
    )

  # Feed data set for the "decode" signature.
  for data in decode_signature_dataset:
    yield (
      "decode", {
        "image": data.image,
        "hint": data.hint,
      },
    )

详情请见https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization

【讨论】:

  • 所以要么所有参数都量化了,要么没有?关于仅量化一个签名以及如何确定量化哪个签名(显然按名称字母顺序)的问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2018-04-08
相关资源
最近更新 更多