op 尚不支持量化:'DEQUANTIZE' for tensorflow 2.x

Quantization not yet supported for op: 'DEQUANTIZE' for tensorflow 2.x

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

期间使用量化注释模型进行 Batch Normalization 量化

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

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)

如果我通过将 tf.lite.OpsSet.TFLITE_BUILTINS 添加到“target_spec.supported_ops”来绕过这个错误,我仍然在 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.

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