tf.lite.TFLiteConverter

Converts a TensorFlow model into TensorFlow Lite model.

Used in the notebooks

Used in the guide Used in the tutorials

Example usage:

# Converting a SavedModel to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

# Converting a tf.Keras model to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Converting ConcreteFunctions to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_concrete_functions([func])
tflite_model = converter.convert()

funcs List of TensorFlow ConcreteFunctions. The list should not contain duplicate elements.
trackable_obj tf.AutoTrackable object associated with funcs. A reference to this object needs to be maintained so that Variables do not get garbage collected since functions have a weak reference to Variables. This is only required when the tf.AutoTrackable object is not maintained by the user (e.g. from_saved_model).

allow_custom_ops Boolean indicating whether to allow custom operations. When False, any unknown operation is an error. When True, custom ops are created for any op that is unknown. The developer needs to provide these to the TensorFlow Lite runtime with a custom resolver. (default False)
optimizations Experimental flag, subject to change. A list of optimizations to apply when converting the model. E.g. [Optimize.DEFAULT]
representative_dataset A representative dataset that can be used to generate input and output samples for the model. The converter can use the dataset to evaluate different optimizations. Note that this is an optional attribute but it is necessary if INT8 is the only support builtin ops in target ops.
target_spec Experimental flag, subject to change. Specification of target device.
inference_input_type Data type of the input layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8})
inference_output_type Data type of the output layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8})
experimental_new_converter Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)

Methods

convert

View source

Converts a TensorFlow GraphDef based on instance variables.

Returns
The converted data in serialized format.

Raises
ValueError No concrete functions is specified. Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.

from_concrete_functions

View source

Creates a TFLiteConverter object from ConcreteFunctions.

Args
funcs List of TensorFlow ConcreteFunctions. The list should not contain duplicate elements. Currently converter can only convert a single ConcreteFunction. Converting multiple functions is under development.

Returns
TFLiteConverter object.

Raises
Invalid input type.

from_keras_model

View source

Creates a TFLiteConverter object from a Keras model.

Args
model tf.Keras.Model

Returns
TFLiteConverter object.

from_saved_model

View source

Creates a TFLiteConverter object from a SavedModel directory.

Args
saved_model_dir SavedModel directory to convert.
signature_keys List of keys identifying SignatureDef containing inputs and outputs. Elements should not be duplicated. By default the signatures attribute of the MetaGraphdef is used. (default saved_model.signatures)
tags Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. (default set(SERVING))

Returns
TFLiteConverter object.

Raises
Invalid signature keys.