How To Import The Tensorflow Lite Interpreter In Python?
I'm developing a Tensorflow embedded application using TF lite on the Raspberry Pi 3b, running Raspbian Stretch. I've converted the graph to a flatbuffer (lite) format and have bu
Solution 1:
Regarding using the TensorFlow Lite Interpreter from Python, the example below is copied from the documentation. The code is available on the master
branch of TensorFlow GitHub.
Using the interpreter from a model file
The following example shows how to use the TensorFlow Lite Python interpreter when provided a TensorFlow Lite FlatBuffer file. The example also demonstrates how to run inference on random input data. Run help(tf.contrib.lite.Interpreter)
in the Python terminal to get detailed documentation on the interpreter.
import numpy as np
import tensorflow as tf
# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)
Solution 2:
I was able to write python scripts to do classification 1, object-detection (tested with SSD MobilenetV{1,2}) 2, and image semantic segmentation 3 on an x86 running Ubuntu and an ARM64 board running Debian.
- How to build Python binding for TF Lite code: Build pip with recent TensorFlow master branch and install it (Yes, those binding was in TF 1.8. However, I don't know why they are not installed). See 4 for how to to build and install TensorFlow pip package.
Post a Comment for "How To Import The Tensorflow Lite Interpreter In Python?"