tf.quantization.experimental.TfRecordRepresentativeDatasetSaver
Stay organized with collections
Save and categorize content based on your preferences.
Representative dataset saver in TFRecord format.
tf.quantization.experimental.TfRecordRepresentativeDatasetSaver(
path_map: Mapping[str, os.PathLike[str]]
)
Saves representative datasets for quantization calibration in TFRecord format.
The samples are serialized as RepresentativeDataSample
.
The save
method return a signature key to RepresentativeDatasetFile
map,
which can be used for QuantizationOptions.
Example usage:
# Creating the representative dataset.
representative_dataset = [{"input": tf.random.uniform(shape=(3, 3))}
for _ in range(256)]
# Saving to a TFRecord file.
dataset_file_map = (
tf.quantization.experimental.TfRecordRepresentativeDatasetSaver(
path_map={'serving_default': '/tmp/representative_dataset_path'}
).save({'serving_default': representative_dataset})
)
# Using in QuantizationOptions.
quantization_options = tf.quantization.experimental.QuantizationOptions(
signature_keys=['serving_default'],
representative_datasets=dataset_file_map,
)
tf.quantization.experimental.quantize_saved_model(
'/tmp/input_model',
'/tmp/output_model',
quantization_options=quantization_options,
)
Args |
path_map
|
Signature def key -> path mapping. Each path is a TFRecord file
to which a RepresentativeDataset is saved. The signature def keys
should be a subset of the SignatureDef keys of the
representative_dataset argument of the save() call.
|
Methods
save
View source
save(
representative_dataset: RepresentativeDatasetMapping
) -> Mapping[str, _RepresentativeDatasetFile]
Saves the representative dataset.
Args |
representative_dataset
|
Signature def key -> representative dataset
mapping. Each dataset is saved in a separate TFRecord file whose path
matches the signature def key of path_map .
|
Raises |
ValueError
|
When the signature def key in representative_dataset is not
present in the path_map .
|
Returns |
A map from signature key to the RepresentativeDatasetFile instance
contains the path to the saved file.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.quantization.experimental.TfRecordRepresentativeDatasetSaver\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/compiler/mlir/quantization/tensorflow/python/representative_dataset.py#L81-L202) |\n\nRepresentative dataset saver in TFRecord format.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.quantization.experimental.TfRecordRepresentativeDatasetSaver`](https://www.tensorflow.org/api_docs/python/tf/quantization/experimental/TfRecordRepresentativeDatasetSaver)\n\n\u003cbr /\u003e\n\n tf.quantization.experimental.TfRecordRepresentativeDatasetSaver(\n path_map: Mapping[str, os.PathLike[str]]\n )\n\nSaves representative datasets for quantization calibration in TFRecord format.\nThe samples are serialized as `RepresentativeDataSample`.\n\nThe `save` method return a signature key to `RepresentativeDatasetFile` map,\nwhich can be used for QuantizationOptions.\n\n#### Example usage:\n\n # Creating the representative dataset.\n representative_dataset = [{\"input\": tf.random.uniform(shape=(3, 3))}\n for _ in range(256)]\n\n # Saving to a TFRecord file.\n dataset_file_map = (\n tf.quantization.experimental.TfRecordRepresentativeDatasetSaver(\n path_map={'serving_default': '/tmp/representative_dataset_path'}\n ).save({'serving_default': representative_dataset})\n )\n\n # Using in QuantizationOptions.\n quantization_options = tf.quantization.experimental.QuantizationOptions(\n signature_keys=['serving_default'],\n representative_datasets=dataset_file_map,\n )\n tf.quantization.experimental.quantize_saved_model(\n '/tmp/input_model',\n '/tmp/output_model',\n quantization_options=quantization_options,\n )\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `path_map` | Signature def key -\\\u003e path mapping. Each path is a TFRecord file to which a `RepresentativeDataset` is saved. The signature def keys should be a subset of the `SignatureDef` keys of the `representative_dataset` argument of the `save()` call. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `save`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/compiler/mlir/quantization/tensorflow/python/representative_dataset.py#L173-L202) \n\n save(\n representative_dataset: RepresentativeDatasetMapping\n ) -\u003e Mapping[str, _RepresentativeDatasetFile]\n\nSaves the representative dataset.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `representative_dataset` | Signature def key -\\\u003e representative dataset mapping. Each dataset is saved in a separate TFRecord file whose path matches the signature def key of `path_map`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|------------------------------------------------------------------------------------------|\n| `ValueError` | When the signature def key in `representative_dataset` is not present in the `path_map`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A map from signature key to the RepresentativeDatasetFile instance contains the path to the saved file. ||\n\n\u003cbr /\u003e"]]