google / lyra

A Very Low-Bitrate Codec for Speech Compression
Apache License 2.0
3.84k stars 355 forks source link

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

Open zachmoshe opened 1 year ago

zachmoshe commented 1 year ago

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 commented 1 year ago

I'm having the same problem.

trungd commented 1 year ago

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

mchinen commented 1 year ago

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'])