Closed fjzhangcr closed 2 years ago
in my concept, the Quantize Op is just for data type transformer. Since Quantize Op is just a bias shifter to re-range the [0,255] to [-128,127], the interpreter.get_tensor() from inputs and outpus of Quantize Op, should be a constant of 128. but this Quantize Op acts abnormal
a0=image_batch_uint8[0].numpy()
a1=interpreter.get_tensor(0)[0]
a2=interpreter.get_tensor(276)[0]
It is because by default TFLite doesn't preserve intermediate tensors this is because it optimizes memory usage and reuse allocated memory of a tensor based on the data flow dependency. You can use the newly added debugging feature to preserve all tensors.
interpreter = tflite.Interpreter(model_path=model_path, experimental_preserve_all_tensors=True)
https://stackoverflow.com/questions/53109990/tflite-get-tensor-on-non-output-tensors-gives-random-values
Feel free to reach out to tflite team for any additional questions on this one: https://github.com/tensorflow/tflite-support
thank you
Description
hi, i am checking inside a tflite model which give me fault result. The first wired thing i discover was the behavior of Quantize Ops. I have make a toy model and make sure the Quantize Ops not change the value but just modify the Quantization. But in my a big model, when i use interpreter.get_tensor() to get the Quantize Ops's input and output, i found they are not equal. my keras model yolov4_rand.h5 in h5 format is here yolov4_rand.h5 the model is loaded in to memeory as keras model and the dimention is forced to 1:
representative_dataset is ranging from 0 to 1, the converter.convert() code are here
the tflite file took me 1000s until it is generated. the tflite file accepts 0 to 255 inputs in UINT8 format, yolov4_rand_wired_0P1_QuanOps.tflite is here yolov4_rand_wired_0P1_QuanOps.tflite
the netron of the generated tflite file is here
check the input output details
print outputs:
i feed the tflite model with data of tf.random.uniform([1,512,512,3],0,255,tf.int32), then cast it to uint8 since uint8 input of [0,255] is identical with float input of [0,1], that means 0.5 in float32 is identical with 128 in UINT8 i record the float input as check_a0
then wait 100s until interpreter invoke() done
then record input of Quantize Ops as check_a1
i get the Quantize Ops output as check_a2
the input of Quantize operation(check_a1) EQUALS the real float inputs(check_a0), but the output of Quantize Ops.(check_a2) did not equal with them
is it a bug, or it is normal? Did I 1, mis-understand the behavior of Quantize? or 2, do the converter.convert() wrong? or 3, get_tensor in a wrong way?
thanks~~
Click to expand!
### Issue Type Bug ### Operating System Windows 10 ### Coral Device Dev Board ### Other Devices _No response_ ### Programming Language Python 3.7 ### Relevant Log Output _No response_