2

我正在通过 keras 在 resnet 模型上进行 QAT,在转换为 tflite 全整数模型时遇到了这个问题。我已经尝试过最新版本的 tf-nightly,但它并没有解决问题。我在 QAT 期间使用量化注释模型进行批量标准化量化

注释模型

这是我用来转换模型的代码:

converter = tf.lite.TFLiteConverter.from_keras_model(layer)
def representative_dataset_gen():
    for _ in range(50):
        batch = next(train_generator)
        img = np.expand_dims(batch[0][0],0).astype(np.float32)
        yield [img]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]
converter.experimental_new_converter = True

# converter.target_spec.supported_types = [tf.int8]
converter.inference_input_type = tf.int8  # or tf.uint8
converter.inference_output_type = tf.int8  # or tf.uint8
quantized_tflite_model = converter.convert()
with open("test_try_v2.tflite", 'wb') as f:
    f.write(quantized_tflite_model)

如果我通过添加到“target_spec.supported_ops”来绕过这个错误tf.lite.OpsSet.TFLITE_BUILTINS,我仍然在 edge_tpu 编译器遇到这个 DEQUANTIZE 问题

ERROR: :61 op_context.input->type == kTfLiteUInt8 || op_context.input->type == kTfLiteInt8 || op_context.input->type == kTfLiteInt16 || op_context.input->type == kTfLiteFloat16 was not true.
ERROR: Node number 3 (DEQUANTIZE) failed to prepare.

ERROR: :61 op_context.input->type == kTfLiteUInt8 || op_context.input->type == kTfLiteInt8 || op_context.input->type == kTfLiteInt16 || op_context.input->type == kTfLiteFloat16 was not true.
ERROR: Node number 3 (DEQUANTIZE) failed to prepare.
4

1 回答 1

0

原因是在 tf2.4 之前的 tf 中尚不支持 DEQUANTIZE 用于完全 8 位整数推断。因此,解决方案是回到 tf.1x 或改用 tf2.4

于 2020-11-15T11:58:44.033 回答