【问题标题】:convert from saved model to quant. tflite, 'Quantization not yet supported for op: CUSTOM'从保存的模型转换为量化。 tflite, 'op: CUSTOM 尚不支持量化'
【发布时间】:2021-04-20 09:48:28
【问题描述】:

我读过类似的问题,Tensorflow (TF2) quantization to full integer error with TFLiteConverter RuntimeError: Quantization not yet supported for op: 'CUSTOM'
但是它无法在 TF 2.4.1 中解决此问题。

我推荐这个 tensorflow 网站使用仅整数量化进行转换。 https://tensorflow.google.cn/lite/performance/post_training_integer_quant
但是,它返回此错误:

RuntimeError:操作尚不支持量化:'CUSTOM'。

代码:

import tensorflow as tf
import numpy as np

def representative_data_gen():
   for input_value in tf.data.Dataset.from_tensor_slices(train_images).batch(1).take(100):
       yield [input_value]

converter = tf.lite.TFLiteConverter.from_saved_model(model)

# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# Set the input and output tensors to uint8
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
# set the representative dataset for the converter so we can quantize the activations
converter.representative_dataset = representative_data_gen
tflite_model = converter.convert()
#write the quantized tflite model to a file
with open('my_quant.tflite', 'wb') as f:
  f.write(tflite_model)

如何解决这个问题?
谢谢

【问题讨论】:

标签: tensorflow-lite quantization


【解决方案1】:

你可以尝试使用标志

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
converter.experimental_new_quantizer = True

改为。

“TFLITE_BUILTINS_INT8”表示完全量化的操作集,我们没有自定义操作的量化内核。

【讨论】:

  • 谢谢冯,我得到了这个结果:` tflite_model = converter.convert() ...ValueError: 模型输出没有去量化。 ` 可能对我不起作用。
【解决方案2】:

CUSTOM 操作通常是 TFLitepostProcess,有 3 种解决方法:

**请注意,我使用 tf 2.4.0 作为解决方法,我没有在 tf 1.15 上测试过,但是 tf.compat.v1 应该与我通过冻结图协议量化的相同

1/可以在tensorflow图中去掉这个op,然后在numpy中正常做NMS。 可以查看here如何删除tensorflow图中的一个节点

2/ 因为TFLitepostProcess替换了NMS,所以不应该量化。如果你坚持量化它,你应该放置converter._experimental_new_quantizer = True。然而,这可能会导致结果不佳,因为 NMS 激活后的边界框可能会有很大差异

3/ 否则,您应该尝试使用converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8,tf.lite.OpsSet.TFLITE_BUILTINS] 进行转换,这将允许TFLitepostProcess 以非量化值运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多