PINTO0309 / tflite2json2tflite

Convert tflite to JSON and make it editable in the IDE. It also converts the edited JSON back to tflite binary.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tflite to JSON to tflite quantization error

tforgaard opened this issue · comments

Hi!

When I convert my int8 quantized tflite model to JSON and back the quantization scale parameters are different (they are correct up to ~4 decimals).

This makes the edgetpu compiler return an error when I try to convert it

edgetpu_compiler -m 13 -sa model_unroll.tflite
Edge TPU Compiler version 16.0.384591198
Started a compilation timeout timer of 180 seconds.
ERROR: :514 output->params.scale == 1. / 256 was not true.
ERROR: Node number 6 (LOGISTIC) failed to prepare.

Compilation failed: Model failed in Tflite interpreter. Please ensure model can be loaded/run in Tflite interpreter.
Compilation child process completed within timeout period.
Compilation failed! 

Any idea of how to fix this?

Inputs and outputs of the model before conversion
Skjermbilde 2023-02-20 kl  12 40 11

Inputs and outputs of the model after conversion
Skjermbilde 2023-02-20 kl  12 40 42

This tool (environment) only provides a simple mechanism to call Google's official tool, flatc. I don't know what flatc did when you generated the JSON from tflite.

  1. Have you checked the contents of the generated JSON file?
  2. What kind of processing did you do to the JSON file?

If you are only shown the results, it doesn't tell you anything. Therefore, it is a good idea to read and look at the source code for flatbuffers.
https://github.com/google/flatbuffers

  1. I see now that the scale parameters in the JSON file only contain six decimals
  2. I originally tried to remove an unnecessary Unpack operation, but I tried to convert to JSON and back without doing any processing as a sanity check as well

I think I found a related issue to work with: google/flatbuffers#5371

I see. This issue seems to have stopped the discussion in the middle. It doesn't look like a good idea to go through a JSON file to process the quantization parameters.

I wish there was a way to read and process the binary value of flatbuffer directly.
Btw, I have tried to convert a byte array of flatbuffer as follows. 2 years ago.

https://github.com/PINTO0309/tflite2tensorflow/blob/c13504df2f82dc234f1009e34dbab9c8b65c7ce4/tflite2tensorflow/tflite2tensorflow.py#L5278-L5509

I am parsing the binary values read by TFLite runtime, but I have used JSON for intermediate files, so the same phenomenon is likely occurring.

It might not be the best solution, but I think I'm gonna go for changing the source code for flatbuffers to use more decimals when parsing floats as described here google/flatbuffers#5371 (comment) .

Excellent.

So you are saying that you intend to rewrite and recompile here.

return FloatToString(t, 6); 

Yes, I will let you know if it works or not.

Worked like a charm!

OK. 17 digits confirmed it for me too. I will update the repository.
image

Nice! Thanks for updating the repo