12

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?

bjuberchaub
  • 123
  • 1
  • 1
  • 5

2 Answers2

13

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)
Community
  • 1
  • 1
Nupur Garg
  • 524
  • 2
  • 9
  • @Napu, Thanks for your input. The problem is that the Tensorflow bindings are not defined in Raspbian. Without those bindings, it is not possible "import tensorflow as tf". – bjuberchaub Jul 01 '18 at 22:02
  • 2
    The Interpreter has moved from the contrib module directly to tensorflow (`tf.lite.Interpreter` instead of `tf.contrib.lite.Interpreter`). The documentation is now located at https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/convert/python_api.md – Morris Franken Mar 19 '19 at 13:21
6

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.
freedom
  • 241
  • 2
  • 5
  • I have tried installing TF from the nightly builds at http://ci.tensorflow.org/view/Nightly/job/nightly-pi-python3/ on Raspbian but get an error that "tensorlflow… is not a supported wheel on this platform" . Given that the Google testing on the RPi has been done on 64-bit Ubuntu (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/rpi.md) and other comments about Tensorflow not running on 32-bit targets (https://stackoverflow.com/questions/38646036/whl-file-for-tensorflow-installation-on-windows), I conclude that TF does not run on 32-bit targets. – bjuberchaub Jul 01 '18 at 22:18
  • I don't think it's 64-bit vs. 32-bit problem. I guess it's something like Python 3.5, the one use in current Raspbian, and 3.4 pip package. Maybe you can try [nightly Python 2.7 pip package](http://ci.tensorflow.org/view/Nightly/job/nightly-pi/) instead. – freedom Jul 03 '18 at 07:51
  • 1
    You are right about 64-bit. It seems the PyPi links have now been updated and I can just install all of Tensorflow using pip on the normal 32-bit RPi3. Problem solved. – bjuberchaub Jul 07 '18 at 15:46
  • @freedom Can you please provide the sample code for object-detection using camera feed instead of a single image? I am trying to use it but I am getting ValueError: Cannot set tensor: Dimension mismatch – Saurabh Kachhia Aug 22 '18 at 23:01