Skip to content

Int8 static quantization on Mediapipe SelfieMulticlass model requiring signature_key #266

@BarbaraCorrieroArm

Description

@BarbaraCorrieroArm

I am trying to quantize the .tflite model from mediapipe segmentation task SelfieMulticlass (256 x 256) using the recipe static i8 weight and i8 activations.
I have added this to recipe.py:

def static_wi8_ai8():
  """Returns a static quantization recipe with int8 weights and int8 activation."""
  return [
      dict({
          'regex': '.*',
          'operation': '*',
          'algorithm_key': 'min_max_uniform_quantize',
          'op_config': {
              'weight_tensor_config': {
                  'num_bits': 8,
                  'symmetric': True,
                  'granularity': 'CHANNELWISE',
                  'dtype': 'INT',
                  'block_size': 0,
              },
              'activation_tensor_config': {
                  'num_bits': 8,
                  'symmetric': True,
                  'granularity': 'CHANNELWISE',
                  'dtype': 'INT',
                  'block_size': 0,
              },
              'compute_precision': 'INTEGER',
              'explicit_dequantize': False,
              'skip_checks': False,
          },
      })
  ]  

and I am trying to use a calibration test dataset to calibrate.

    # Define the representative dataset generator
    def representative_data_gen():
        for _ in range(100):
            # Replace "input_29" with your input name
            yield {"default": np.random.rand(1, 256, 256, 3).astype(np.float32)}

    # Use a fallback signature key ("default") and pass our generator
    calibration_result = qt.calibrate({
        "None": representative_data_gen()    #tried "default", "input_29", "main", "serving_default", "Conv2D"
                                         # "" - Exception has occurred: ValueError None signature_key provided.
                                         # None - 'SignatureDef signature_key is None and model has {0} Signatures. '
                                         #'None is only allowed when the model has 1 SignatureDef'.
    })
    print("Calibration completed successfully.")

I keep getting errors on pretty much every signature key I have tried. The point is the model doesn't have one as I see when I try to print it:
=== Inputs:
name: input_29, shape: [ 1 256 256 3], dtype: <class 'numpy.float32'>

===Outputs:
name: Identity, shape: [ 1 256 256 6], dtype: <class 'numpy.float32'>

=== Signatures:
No signatures found in the model.
{}

I have also tried "None" but I get the error listed above in the comment.
I'm just wondering why we need a signature key. My understanding is that we need signature keys only when a model has multiple inputs/outputs and there is only one here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions