Skip to content

Missing input quantization #1

Description

@CIPop

Thank you for the sample!

I'm not 100% sure, but I believe that the processor is missing input quantization as described at https://www.tensorflow.org/lite/performance/post_training_integer_quant#run_the_tensorflow_lite_models

Something like

Edited with corrected code based on this discussion:

# 1. Read image as 300x300x3 uint8
res_im = im.resize((300, 300))
np_res_im = np.array(res_im)

#2 Transform from input uint8 RGB [0..255] to float [-1, 1] 
np_res_im = (np_res_im / 255) * 2 - 1

#3 Apply quantization based on https://www.tensorflow.org/lite/performance/post_training_integer_quant#run_the_tensorflow_lite_models
# Check if the input type is quantized, then rescale input data to uint8
if input_details['dtype'] == np.uint8:
    input_scale, input_zero_point = input_details["quantization"]
    np_res_im = np_res_im / input_scale + input_zero_point
 
np_res_im = np.expand_dims(np_res_im, axis=0).astype(input_details["dtype"])

# Quantized input [0..255] is still 300x300x3 but the quantized values are different from the original image:
print(np_res_im)

This is based on this output from the first cell:

[{'name': 'normalized_input_image_tensor',
  'index': 175,
  'shape': array([  1, 300, 300,   3], dtype=int32),
  'shape_signature': array([  1, 300, 300,   3], dtype=int32),
  'dtype': numpy.uint8,
  'quantization': (0.0078125, 128),
  'quantization_parameters': {'scales': array([0.0078125], dtype=float32),
   'zero_points': array([128], dtype=int32),
   'quantized_dimension': 0},
  'sparsity_parameters': {}}]

The dtype is uint8 and quantization and zero-point information is available.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions