Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 built the TFLite static library natively on the Pi. So far so good. But the application is Python and there seems to be no Python binding available. The Tensorflow Lite development guide (https://www.tensorflow.org/mobile/tflite/devguide) states "There are plans for Python bindings and a demo app." Yet there is wrapper code in /tensorflow/contrib/lite/python/interpreter_wrapper that has all the needed interpreter methods. Yet calling this from Python eludes me.

I have generated a SWIG wrapper but the build step fails with many errors. There is no readme.md describing the state of the interpreter_wrapper. So, I wonder if the wrapper has worked for others and I should persist or is it fundamentally broken and I should look elsewhere (PyTorch)? Has anyone found a path to the TFLite Python bindings for the Pi3?

like image 558
bjuberchaub Avatar asked Jun 18 '18 02:06

bjuberchaub


People also ask

What is TensorFlow Lite interpreter?

It lets you run machine-learned models on mobile devices with low latency, so you can take advantage of them to do classification, regression or anything else you might want without necessarily incurring a round trip to a server. It's presentl… TensorFlow Lite · Using TensorFlow Lite on Android.

Is TensorFlow Lite Python?

About the TensorFlow Lite runtime package We call this simplified Python package tflite_runtime . The tflite_runtime package is a fraction the size of the full tensorflow package and includes the bare minimum code required to run inferences with TensorFlow Lite—primarily the Interpreter Python class.

Is TensorFlow Lite a library?

TensorFlow Lite is a mobile library for deploying models on mobile, microcontrollers and other edge devices. Guides explain the concepts and components of TensorFlow Lite. Explore TensorFlow Lite Android and iOS apps. Learn how to use TensorFlow Lite for common use cases.

Is TensorFlow Lite same as TensorFlow?

The differences between TensorFlow Lite and TensorFlow Mobile are as follows: It is the next version of TensorFlow mobile. Generally, applications developed on TensorFlow Lite will have better performance and less binary file size than TensorFlow mobile.


2 Answers

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)
like image 136
Nupur Garg Avatar answered Oct 28 '22 04:10

Nupur Garg


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.
like image 41
freedom Avatar answered Oct 28 '22 04:10

freedom