Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load TFLite output_details for quantizer model (encode) #123

Open
zachmoshe opened this issue Feb 5, 2023 · 3 comments
Open

Can't load TFLite output_details for quantizer model (encode) #123

zachmoshe opened this issue Feb 5, 2023 · 3 comments

Comments

@zachmoshe
Copy link

Hi,
I'm not sure if I'm using it correctly, so could be that the problem is on my side. Consider the following code:

!git clone --branch v1.3.2 https://github.com/google/lyra.git
import tensorflow as tf

quantizer = tf.lite.Interpreter("lyra/lyra/model_coeffs/quantizer.tflite")
quantizer.allocate_tensors()

print(quantizer.get_signature_runner("decode").get_input_details())  # works
print(quantizer.get_signature_runner("decode").get_output_details()) # works
print(quantizer.get_signature_runner("encode").get_input_details())  # works
print(quantizer.get_signature_runner("encode").get_output_details()) # ERROR: ValueError: Invalid tensor index 703 exceeds max tensor index 422

The last line produces the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-181-2b5d6f9aa635>](https://localhost:8080/#) in <module>
      4 print(quantizer.get_signature_runner("decode").get_output_details())
      5 print(quantizer.get_signature_runner("encode").get_input_details())
----> 6 print(quantizer.get_signature_runner("encode").get_output_details())

1 frames
[/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/interpreter.py](https://localhost:8080/#) in _get_tensor_details(self, tensor_index)
    591     tensor_index = int(tensor_index)
    592     tensor_name = self._interpreter.TensorName(tensor_index)
--> 593     tensor_size = self._interpreter.TensorSize(tensor_index)
    594     tensor_size_signature = self._interpreter.TensorSizeSignature(tensor_index)
    595     tensor_type = self._interpreter.TensorType(tensor_index)

ValueError: Invalid tensor index 703 exceeds max tensor index 422

I'm not really sure why...

Is that the proper way to use? Any TFLite example code that I can follow?

Thanks,
Zach

@tsuffb
Copy link

tsuffb commented Feb 5, 2023

I'm having the same problem.

@trungd
Copy link

trungd commented Mar 1, 2023

Same problem. Can we have an example of how to use these tflite files?

@mchinen
Copy link
Collaborator

mchinen commented Mar 1, 2023

Hi, apologies for the late reply.

Our library only uses the TFLite models with the C++ interface (examples in encoder_lib_main.cc and decoder_lib_main.cc).

It's possible this is related to the TF version you are running in the python environment. I tried this from bleeding edge and got this output without errors:

{'encoding_indices': {'name': 'decode_encoding_indices:0', 'index': 0, 'shape': array([46,  1,  1], dtype=int32), 'shape_signature': array([46,  1,  1], dtype=int32), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}}
{'output_0': {'name': 'StatefulPartitionedCall:0', 'index': 421, 'shape': array([ 1,  1, 64], dtype=int32), 'shape_signature': array([ 1,  1, 64], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}}
{'input_frames': {'name': 'encode_input_frames:0', 'index': 1, 'shape': array([ 1,  1, 64], dtype=int32), 'shape_signature': array([ 1,  1, 64], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, 'num_quantizers': {'name': 'encode_num_quantizers:0', 'index': 0, 'shape': array([], dtype=int32), 'shape_signature': array([], dtype=int32), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}}
{'output_0': {'name': 'StatefulPartitionedCall_1:0', 'index': 703, 'shape': array([46,  1,  1], dtype=int32), 'shape_signature': array([46,  1,  1], dtype=int32), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, 'output_1': {'name': 'StatefulPartitionedCall_1:1', 'index': 96, 'shape': array([], dtype=int32), 'shape_signature': array([], dtype=int32), 'dtype': <class 'numpy.int32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}}

We haven't explored python usage, but this is an example of how you could call the quantizer encode/decode (in practice you would use the output of the lyra encoder instead of quantizing zeros):

encoder = quantizer.get_signature_runner("encode")
encoded = encoder(input_frames=tf.zeros(1, 1, 64), num_quantizers=tf.constant(46))
decoder = quantizer.get_signature_runner("decode")
decoded = decoder(encoding_indices=encoded['output_0'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants