tensorflow: Max and min for dynamic tensors should be recorded during calibration: Failed for tensor Shape
System information
- OS Platform and Distribution: Linux Ubuntu 18.04
- TensorFlow installed using pip, it’s actually tf-nightly-gpu
- TensorFlow version: tf-nightly-gpu==2.3.0.dev20200527
Command used to run the converter or code if you’re using the Python API
import numpy
import tensorflow as tf
def data_reader(size=900):
return numpy.ones((size, 32, 86, 1), numpy.uint8) * 255
data = data_reader()
data = tf.cast(data, tf.float32)
repr_ds = tf.data.Dataset.from_tensor_slices((data)).batch(1)
def representative_data_gen():
for input_value in repr_ds.take(100):
yield [input_value]
converter = tf.lite.TFLiteConverter.from_saved_model("./exported")
converter.experimental_new_converter = True
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()
open("exported.tflite", "wb").write(tflite_model)
**The output from the converter invocation**
Traceback (most recent call last): File “…”, line 29, in <module> tflite_model = converter.convert() File “venv/lib/python3.6/site-packages/tensorflow/lite/python/lite.py”, line 920, in convert return super(TFLiteConverterV2, self).convert() File “venv/lib/python3.6/site-packages/tensorflow/lite/python/lite.py”, line 752, in convert self).convert(graph_def, input_tensors, output_tensors) File “venv/lib/python3.6/site-packages/tensorflow/lite/python/lite.py”, line 502, in convert constants.FLOAT, False) File venv/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 349, in _calibrate_quantize_model inference_output_type, allow_float) File “venv/lib/python3.6/site-packages/tensorflow/lite/python/optimize/calibrator.py”, line 93, in calibrate_and_quantize np.dtype(output_type.as_numpy_dtype()).num, allow_float) RuntimeError: Max and min for dynamic tensors should be recorded during calibration: Failed for tensor Shape Empty min/max for tensor Shape
**Also, please include a link to the saved model or GraphDef**
https://drive.google.com/file/d/1dSDVWjM2fJbT71r-EiUqkwygtYr0a5aE/view?usp=sharing
**Failure details**
I am trying to convert this model by performing full integer quantization described [here](https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization_of_weights_and_activations), but it fails as described above. If I attempt to convert it without a representative dataset it works ok, but the input/output tensors remain float32 and I need int8. Maybe I am missing something, but I'd like to know if this is solvable. Thanks in advance.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 15 (5 by maintainers)
I am able to replicate this issue, please find gist here. Thanks!
It seems that there was a
tf.tileoperation in my graph that cannot be quantized and probably was caught by this min/max exception. The correct exception seems to have been fixed intf-nightly-gpu==2.3.0.dev20200615. Thanks for your support though!This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.
hey @jvishnuvardhan thanks for your answer. I have attempted the conversion as suggested on that answer but unfortunately I am still getting the same error. I updated the gist so you can see it as well.
@vladdders Can you please try the solution mentioned here and let us know whether it worked for you. Thanks!
It doesn’t work for me. Did you try my code by any chance and got it to work?
Sure sir…will do it now
I have the same issue… I saw here that installing tf-nightly-gpu==2.3.0.dev2020061 can solve the issue,but I’m unable to install a specific version of tf-nightly-gpu using pip,it always installs the latest version. How can tf-nightly-gpu==2.3.0.dev2020061 be installed?
Are you satisfied with the resolution of your issue? Yes No
@Saduf2019 I have tried what @xchani suggested and sadly the same issue occurs. I have installed the stable version
2.2.0as mentioned previously. Please find the gist here.@vladdders Can you please try it on version as suggested by xchani and share a colab gist in case you face any issues.
I think it’s due to some latest commit. Since I also encounter this error with the nightly version, but the same code works with stable version.
@Saduf2019 I have tried with tensorflow 2.2.0, but unfortunately I get the same error 😕
@vladdders Please refer to this issue with similar error#39718 and let us know if it helps.