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

How to create a representative dataset using TFrecords for tensorflow lite post training quantize conversion? #48552

Open
Ethylbenzol opened this issue Apr 16, 2021 · 3 comments
Assignees
Labels
ModelOptimizationToolkit TF Model Optimization Toolkit

Comments

@Ethylbenzol
Copy link

1. System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • TensorFlow installation (pip package or built from source): pip
  • TensorFlow library (version, if pip package or github SHA, if built from source): tf-nightly 2.6 dev

I am trying to create a representative data set for conversion to tflite and perform a int8 quantize operations. I was wondering, is it possible to use TFrecords as a input data to be used to create a representative_dataset instead of looping through each images to generate a representative_dataset? This is due to no examples of using TFrecords in the API but the API just says you can load TFrecord and create a dataset for manipulation.

@Ethylbenzol Ethylbenzol added the TFLiteConverter For issues related to TFLite converter label Apr 16, 2021
@abattery abattery added ModelOptimizationToolkit TF Model Optimization Toolkit and removed TFLiteConverter For issues related to TFLite converter labels Apr 16, 2021
@abattery
Copy link
Contributor

@teijeong could you triage this issue?

@liufengdb
Copy link
Contributor

Can you try the following code?

def representative_dataset_generator():
    """Dataset generator that generates random tensor with the same shape as the input"""
    filenames = [filename]
    raw_dataset = tf.data.TFRecordDataset(filenames) 
    for raw_record in raw_dataset.take(10):
       # manipulate the raw_record to get the right shape.
      yield raw_record    # raw_record is just a tensor

@liufengdb liufengdb self-assigned this Apr 21, 2021
@Ethylbenzol
Copy link
Author

Can you try the following code?

Just tried it. Threw a error TypeError: 'generator' object is not callable in tensorflow/tensorflow/lite/python/optimize/calibrator.py at:

    for sample in dataset_gen():
      if not initialized:
        initialized = True
        self._calibrator.Prepare([list(s.shape) for s in sample])
      self._calibrator.FeedTensor(sample)
    return self._calibrator.Calibrate()

I will try debug and see what went wrong with the conversion process.

@UsharaniPagadala UsharaniPagadala removed their assignment May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ModelOptimizationToolkit TF Model Optimization Toolkit
Projects
None yet
Development

No branches or pull requests

4 participants