Path: blob/master/site/en-snapshot/lite/models/convert/convert_models.md
25118 views
Convert TensorFlow models
This page describes how to convert a TensorFlow model to a TensorFlow Lite model (an optimized FlatBuffer format identified by the .tflite
file extension) using the TensorFlow Lite converter.
Note: This guide assumes you've both installed TensorFlow 2.x and trained models in TensorFlow 2.x. If your model is trained in TensorFlow 1.x, considering migrating to TensorFlow 2.x. To identify the installed TensorFlow version, run print(tf.__version__)
.
Conversion workflow
The diagram below illustrations the high-level workflow for converting your model:
Figure 1. Converter workflow.
You can convert your model using one of the following options:
Python API (recommended): This allows you to integrate the conversion into your development pipeline, apply optimizations, add metadata and many other tasks that simplify the conversion process.
Command line: This only supports basic model conversion.
Note: In case you encounter any issues during model conversion, create a GitHub issue.
Python API
Helper code: To learn more about the TensorFlow Lite converter API, run print(help(tf.lite.TFLiteConverter))
.
Convert a TensorFlow model using tf.lite.TFLiteConverter
. A TensorFlow model is stored using the SavedModel format and is generated either using the high-level tf.keras.*
APIs (a Keras model) or the low-level tf.*
APIs (from which you generate concrete functions). As a result, you have the following three options (examples are in the next few sections):
tf.lite.TFLiteConverter.from_saved_model()
(recommended): Converts a SavedModel.tf.lite.TFLiteConverter.from_keras_model()
: Converts a Keras model.tf.lite.TFLiteConverter.from_concrete_functions()
: Converts concrete functions.
Convert a SavedModel (recommended)
The following example shows how to convert a SavedModel into a TensorFlow Lite model.
Convert a Keras model
The following example shows how to convert a Keras model into a TensorFlow Lite model.
Convert concrete functions
The following example shows how to convert concrete functions into a TensorFlow Lite model.
Other features
Apply optimizations. A common optimization used is post training quantization, which can further reduce your model latency and size with minimal loss in accuracy.
Add metadata, which makes it easier to create platform specific wrapper code when deploying models on devices.
Conversion errors
The following are common conversion errors and their solutions:
Error:
Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select. TF Select ops: ..., .., ...
Solution: The error occurs as your model has TF ops that don't have a corresponding TFLite implementation. You can resolve this by using the TF op in the TFLite model (recommended). If you want to generate a model with TFLite ops only, you can either add a request for the missing TFLite op in Github issue #21526 (leave a comment if your request hasn’t already been mentioned) or create the TFLite op yourself.
Error:
.. is neither a custom op nor a flex op
Solution: If this TF op is:
Supported in TF: The error occurs because the TF op is missing from the allowlist (an exhaustive list of TF ops supported by TFLite). You can resolve this as follows:
Unsupported in TF: The error occurs because TFLite is unaware of the custom TF operator defined by you. You can resolve this as follows:
Create the TFLite op and run inference by linking it to the TFLite runtime.
Command Line Tool
Note: It is highly recommended that you use the Python API listed above instead, if possible.
If you've installed TensorFlow 2.x from pip, use the tflite_convert
command. To view all the available flags, use the following command:
If you have the TensorFlow 2.x source donwloaded and want to run the converter from that source without building and installing the package, you can replace 'tflite_convert
' with 'bazel run tensorflow/lite/python:tflite_convert --
' in the command.
Converting a SavedModel
Converting a Keras H5 model
Next Steps
Use the TensorFlow Lite interpreter to run inference on a client device (e.g. mobile, embedded).