ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

tf.compat.v1.lite.TFLiteConverter

Convert a TensorFlow model into output_format.

Used in the notebooks

Used in the guide

This is used to convert from a TensorFlow GraphDef, SavedModel or tf.keras model into either a TFLite FlatBuffer or graph visualization.

Example usage:

# Converting a GraphDef from session.
converter = tf.compat.v1.lite.TFLiteConverter.from_session(
  sess, in_tensors, out_tensors)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

# Converting a GraphDef from file.
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

# Converting a SavedModel.
converter = tf.compat.v1.lite.TFLiteConverter.from_saved_model(
    saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

# Converting a tf.keras model.
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(
    keras_model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

graph_def Frozen TensorFlow GraphDef.
input_tensors List of input tensors. Type and shape are computed using foo.shape and foo.dtype.
output_tensors List of output tensors (only .name is used from this).
input_arrays_with_shape Tuple of strings representing input tensor names and list of integers representing input shapes (e.g., [("foo" : [1, 16, 16, 3])]). Use only when graph cannot be loaded into TensorFlow and when input_tensors and output_tensors are None. (default None)
output_arrays List of output tensors to freeze graph with. Use only when graph cannot be loaded into TensorFlow and when input_tensors and output_tensors are None. (default None)
experimental_debug_info_func An experimental function to retrieve the graph debug info for a set of nodes from the graph_def.

ValueError Invalid arguments.

optimizations Experimental flag, subject to change. Set of optimizations to apply. e.g {tf.lite.Optimize.DEFAULT}. (default None, must be None or a set of values of type tf.lite.Optimize)
representative_dataset A generator function used for integer quantization where each generated sample has the same order, type and shape as the inputs to the model. Usually, this is a small subset of a few hundred samples randomly chosen, in no particular order, from the training or evaluation dataset. This is an optional attribute, but required for full integer quantization, i.e, if tf.int8 is the only supported type in target_spec.supported_types. Refer to tf.lite.RepresentativeDataset. (default None)
target_spec Experimental flag, subject to change. Specifications of target device, including supported ops set, supported types and a set of user's defined TensorFlow operators required in the TensorFlow Lite runtime. Refer to tf.lite.TargetSpec.
inference_type Data type of numeric arrays, excluding the input layer. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8})
inference_input_type Data type of the numeric arrays in the input layer. If inference_input_type is in {tf.int8, tf.uint8}, then quantized_input_stats must be provided. (default is the value assigned to inference_type, must be in {tf.float32, tf.int8, tf.uint8})
inference_output_type Data type of the numeric arrays in the output layer. (default is the value assigned to inference_type, must be in {tf.float32, tf.int8, tf.uint8})
quantized_input_stats Map of input tensor names to a tuple of floats representing the mean and standard deviation of the training data. (e.g., {"foo" : (0., 1.)}). Required if