9951 explained code solutions for 126 technologies


python-tensorflowHow do I use the Python TensorFlow Lite Interpreter?


The Python TensorFlow Lite Interpreter is a tool for running TensorFlow Lite models on mobile, embedded, and IoT devices. It enables low-latency inference of on-device machine learning models with a small binary size and fast performance.

To use the Python TensorFlow Lite Interpreter, you first need to install the TensorFlow Lite package:

pip install tensorflow-lite

Then, you can use the Interpreter class to run inference on a TensorFlow Lite model. For example, to load and run a model on an image:

# Load TFLite model and allocate tensors
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load image and resize it to input shape
image = Image.open("image.jpg")
image_resized = image.resize(input_details[0]['shape'][1:3])

# Set input tensor
interpreter.set_tensor(input_details[0]['index'], image_resized)

# Run inference
interpreter.invoke()

# Get output
output_data = interpreter.get_tensor(output_details[0]['index'])

The above code will load and run a TensorFlow Lite model on an image, and the output data will be stored in the output_data variable.

The following list contains the main parts of the code:

  1. Install TensorFlow Lite package: pip install tensorflow-lite
  2. Create Interpreter object: interpreter = tf.lite.Interpreter(model_path="model.tflite")
  3. Allocate tensors: interpreter.allocate_tensors()
  4. Get input and output tensors: input_details = interpreter.get_input_details() and output_details = interpreter.get_output_details()
  5. Load image and resize it to input shape: image_resized = image.resize(input_details[0]['shape'][1:3])
  6. Set input tensor: interpreter.set_tensor(input_details[0]['index'], image_resized)
  7. Run inference: interpreter.invoke()
  8. Get output: output_data = interpreter.get_tensor(output_details[0]['index'])

For more information, please refer to the TensorFlow Lite Python API documentation.

Edit this code on GitHub