tensorflow2.0tensorflow-litequantizationquantization-aware-training

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


I am conducting QAT by keras on a resnet model and I got this problem while converting to tflite full integer model. I have tried the newest version tf-nightly, but it does not solve the problem. I use quantization annotated model for Batch Normalization quantization during QAT

The annotate model

Here is the code I use to convert my model:

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)

if I bypass this error by adding tf.lite.OpsSet.TFLITE_BUILTINS to "target_spec.supported_ops", I still got this DEQUANTIZE problem at edge_tpu compiler

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.

Solution

  • The reason is DEQUANTIZE of is not yet supported in tf before tf2.4 for fully 8-bit integers inference. Therefore, the solution is to go back to tf.1x or using tf2.4 instead