9951 explained code solutions for 126 technologies


python-tensorflowHow can I use TensorFlow Lite with XNNPACK in Python?


TensorFlow Lite (TFLite) is a lightweight version of TensorFlow designed for mobile and embedded devices. It can be used with XNNPACK to improve the performance of machine learning models on mobile and embedded devices.

To use TensorFlow Lite with XNNPACK in Python, you will need to install the TensorFlow Lite package and the XNNPACK package. You can then use the TFLite Python API to create a model and use the XNNPACK delegate to optimize the model for mobile and embedded devices.

Example code

import tensorflow as tf
import tflite_runtime.interpreter as tflite

# Create a TensorFlow Lite model
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(10, input_shape=(3,))
])

# Convert the model to TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Enable XNNPACK delegate for TensorFlow Lite
converter.experimental_new_converter = True
converter.experimental_new_converter_funcs = [tflite.experimental_add_xnnpack_delegate]
tflite_model = converter.convert()

# Use the TensorFlow Lite model
interpreter = tflite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

# ...

The code above creates a TensorFlow Lite model from a Keras model and enables the XNNPACK delegate for the TensorFlow Lite model. The XNNPACK delegate will optimize the model for mobile and embedded devices.

Code explanation

  1. import tensorflow as tf: imports the TensorFlow package
  2. import tflite_runtime.interpreter as tflite: imports the TensorFlow Lite interpreter
  3. model = tf.keras.models.Sequential([tf.keras.layers.Dense(10, input_shape=(3,))]): creates a Keras model
  4. converter = tf.lite.TFLiteConverter.from_keras_model(model): creates a TensorFlow Lite converter from the Keras model
  5. converter.experimental_new_converter = True: enables the experimental new converter
  6. converter.experimental_new_converter_funcs = [tflite.experimental_add_xnnpack_delegate]: adds the XNNPACK delegate to the TensorFlow Lite converter
  7. tflite_model = converter.convert(): converts the model to TensorFlow Lite
  8. interpreter = tflite.Interpreter(model_content=tflite_model): creates a TensorFlow Lite interpreter
  9. interpreter.allocate_tensors(): allocates the tensors for the TensorFlow Lite interpreter

Helpful links

Edit this code on GitHub