1*89c4ff92SAndroid Build Coastguard Worker# TfLite Delegate Quick Start Guide 2*89c4ff92SAndroid Build Coastguard WorkerIf you have downloaded the Arm NN Github binaries or built the TfLite delegate yourself, then this tutorial will show you how you can 3*89c4ff92SAndroid Build Coastguard Workerintegrate it into TfLite to run models using python. 4*89c4ff92SAndroid Build Coastguard Worker 5*89c4ff92SAndroid Build Coastguard WorkerHere is an example python script showing how to do this. In this script we are making use of the 6*89c4ff92SAndroid Build Coastguard Worker[external adaptor](https://www.tensorflow.org/lite/performance/implementing_delegate#option_2_leverage_external_delegate) 7*89c4ff92SAndroid Build Coastguard Workertool of TfLite that allows you to load delegates at runtime. 8*89c4ff92SAndroid Build Coastguard Worker```python 9*89c4ff92SAndroid Build Coastguard Workerimport numpy as np 10*89c4ff92SAndroid Build Coastguard Workerimport tflite_runtime.interpreter as tflite 11*89c4ff92SAndroid Build Coastguard Worker 12*89c4ff92SAndroid Build Coastguard Worker# Load TFLite model and allocate tensors. 13*89c4ff92SAndroid Build Coastguard Worker# (if you are using the complete tensorflow package you can find load_delegate in tf.experimental.load_delegate) 14*89c4ff92SAndroid Build Coastguard Workerarmnn_delegate = tflite.load_delegate( library="<path-to-armnn-binaries>/libarmnnDelegate.so", 15*89c4ff92SAndroid Build Coastguard Worker options={"backends": "CpuAcc,GpuAcc,CpuRef", "logging-severity":"info"}) 16*89c4ff92SAndroid Build Coastguard Worker# Delegates/Executes all operations supported by Arm NN to/with Arm NN 17*89c4ff92SAndroid Build Coastguard Workerinterpreter = tflite.Interpreter(model_path="<your-armnn-repo-dir>/delegate/python/test/test_data/mock_model.tflite", 18*89c4ff92SAndroid Build Coastguard Worker experimental_delegates=[armnn_delegate]) 19*89c4ff92SAndroid Build Coastguard Workerinterpreter.allocate_tensors() 20*89c4ff92SAndroid Build Coastguard Worker 21*89c4ff92SAndroid Build Coastguard Worker# Get input and output tensors. 22*89c4ff92SAndroid Build Coastguard Workerinput_details = interpreter.get_input_details() 23*89c4ff92SAndroid Build Coastguard Workeroutput_details = interpreter.get_output_details() 24*89c4ff92SAndroid Build Coastguard Worker 25*89c4ff92SAndroid Build Coastguard Worker# Test model on random input data. 26*89c4ff92SAndroid Build Coastguard Workerinput_shape = input_details[0]['shape'] 27*89c4ff92SAndroid Build Coastguard Workerinput_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8) 28*89c4ff92SAndroid Build Coastguard Workerinterpreter.set_tensor(input_details[0]['index'], input_data) 29*89c4ff92SAndroid Build Coastguard Worker 30*89c4ff92SAndroid Build Coastguard Workerinterpreter.invoke() 31*89c4ff92SAndroid Build Coastguard Worker 32*89c4ff92SAndroid Build Coastguard Worker# Print out result 33*89c4ff92SAndroid Build Coastguard Workeroutput_data = interpreter.get_tensor(output_details[0]['index']) 34*89c4ff92SAndroid Build Coastguard Workerprint(output_data) 35*89c4ff92SAndroid Build Coastguard Worker``` 36*89c4ff92SAndroid Build Coastguard Worker 37*89c4ff92SAndroid Build Coastguard Worker# Prepare the environment 38*89c4ff92SAndroid Build Coastguard WorkerPre-requisites: 39*89c4ff92SAndroid Build Coastguard Worker * Dynamically build Arm NN Delegate library or download the Arm NN binaries 40*89c4ff92SAndroid Build Coastguard Worker * python3 (Depends on TfLite version) 41*89c4ff92SAndroid Build Coastguard Worker * virtualenv 42*89c4ff92SAndroid Build Coastguard Worker * numpy (Depends on TfLite version) 43*89c4ff92SAndroid Build Coastguard Worker * tflite_runtime (>=2.5, depends on Arm NN Delegate) 44*89c4ff92SAndroid Build Coastguard Worker 45*89c4ff92SAndroid Build Coastguard WorkerIf you haven't built the delegate yet then take a look at the [build guide](./BuildGuideNative.md). Otherwise, you can download the binaries [here](https://github.com/ARM-software/armnn/releases/). Set the following environment variable to the location of the .so binary files: 46*89c4ff92SAndroid Build Coastguard Worker 47*89c4ff92SAndroid Build Coastguard Worker```bash 48*89c4ff92SAndroid Build Coastguard Workerexport LD_LIBRARY_PATH=<path_to_so_binary_files> 49*89c4ff92SAndroid Build Coastguard Worker``` 50*89c4ff92SAndroid Build Coastguard Worker 51*89c4ff92SAndroid Build Coastguard WorkerWe recommend creating a virtual environment for this tutorial. For the following code to work python3 is needed. Please 52*89c4ff92SAndroid Build Coastguard Workeralso check the documentation of the TfLite version you want to use. There might be additional prerequisites for the python 53*89c4ff92SAndroid Build Coastguard Workerversion. We will use Tensorflow Lite 2.5.0 for this guide. 54*89c4ff92SAndroid Build Coastguard Worker```bash 55*89c4ff92SAndroid Build Coastguard Worker# Install python3 (We ended up with python3.5.3) and virtualenv 56*89c4ff92SAndroid Build Coastguard Workersudo apt-get install python3-pip 57*89c4ff92SAndroid Build Coastguard Workersudo pip3 install virtualenv 58*89c4ff92SAndroid Build Coastguard Worker 59*89c4ff92SAndroid Build Coastguard Worker# create a virtual environment 60*89c4ff92SAndroid Build Coastguard Workercd your/tutorial/dir 61*89c4ff92SAndroid Build Coastguard Worker# creates a directory myenv at the current location 62*89c4ff92SAndroid Build Coastguard Workervirtualenv -p python3 myenv 63*89c4ff92SAndroid Build Coastguard Worker# activate the environment 64*89c4ff92SAndroid Build Coastguard Workersource myenv/bin/activate 65*89c4ff92SAndroid Build Coastguard Worker``` 66*89c4ff92SAndroid Build Coastguard Worker 67*89c4ff92SAndroid Build Coastguard WorkerNow that the environment is active we can install additional packages we need for our example script. As you can see 68*89c4ff92SAndroid Build Coastguard Workerin the python script at the start of this page, this tutorial uses the `tflite_runtime` rather than the whole tensorflow 69*89c4ff92SAndroid Build Coastguard Workerpackage. The `tflite_runtime` is a package that wraps the TfLite Interpreter. Therefore it can only be used to run inferences of 70*89c4ff92SAndroid Build Coastguard WorkerTfLite models. But since Arm NN is only an inference engine itself this is a perfect match. The 71*89c4ff92SAndroid Build Coastguard Worker`tflite_runtime` is also much smaller than the whole tensorflow package and better suited to run models on 72*89c4ff92SAndroid Build Coastguard Workermobile and embedded devices. 73*89c4ff92SAndroid Build Coastguard Worker 74*89c4ff92SAndroid Build Coastguard WorkerThe TfLite [website](https://www.tensorflow.org/lite/guide/python) shows you two methods to download the `tflite_runtime` package. 75*89c4ff92SAndroid Build Coastguard WorkerIn our experience, the use of the pip command works for most systems including debian. However, if you're using an older version of Tensorflow, 76*89c4ff92SAndroid Build Coastguard Workeryou may need to build the pip package from source. You can find more information [here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tools/pip_package/README.md). 77*89c4ff92SAndroid Build Coastguard WorkerBut in our case, with Tensorflow Lite 2.5.0, we can install through: 78*89c4ff92SAndroid Build Coastguard Worker 79*89c4ff92SAndroid Build Coastguard Worker``` 80*89c4ff92SAndroid Build Coastguard Workerpip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime==2.5.0 81*89c4ff92SAndroid Build Coastguard Worker``` 82*89c4ff92SAndroid Build Coastguard Worker 83*89c4ff92SAndroid Build Coastguard WorkerYour virtual environment is now all setup. Copy the final python script into a python file e.g. 84*89c4ff92SAndroid Build Coastguard Worker`ExternalDelegatePythonTutorial.py`. Modify the python script above and replace `<path-to-armnn-binaries>` and 85*89c4ff92SAndroid Build Coastguard Worker`<your-armnn-repo-dir>` with the directories you have set up. If you've been using the [native build guide](./BuildGuideNative.md) 86*89c4ff92SAndroid Build Coastguard Workerthis will be `$BASEDIR/armnn/build` and `$BASEDIR/armnn`. 87*89c4ff92SAndroid Build Coastguard Worker 88*89c4ff92SAndroid Build Coastguard WorkerFinally, execute the script: 89*89c4ff92SAndroid Build Coastguard Worker```bash 90*89c4ff92SAndroid Build Coastguard Workerpython ExternalDelegatePythonTutorial.py 91*89c4ff92SAndroid Build Coastguard Worker``` 92*89c4ff92SAndroid Build Coastguard WorkerThe output should look similar to this: 93*89c4ff92SAndroid Build Coastguard Worker```bash 94*89c4ff92SAndroid Build Coastguard WorkerInfo: Arm NN v28.0.0 95*89c4ff92SAndroid Build Coastguard Worker 96*89c4ff92SAndroid Build Coastguard WorkerInfo: Initialization time: 0.56 ms 97*89c4ff92SAndroid Build Coastguard Worker 98*89c4ff92SAndroid Build Coastguard WorkerINFO: TfLiteArmnnDelegate: Created TfLite Arm NN delegate. 99*89c4ff92SAndroid Build Coastguard Worker[[ 12 123 16 12 11 14 20 16 20 12]] 100*89c4ff92SAndroid Build Coastguard WorkerInfo: Shutdown time: 0.28 ms 101*89c4ff92SAndroid Build Coastguard Worker``` 102*89c4ff92SAndroid Build Coastguard Worker 103*89c4ff92SAndroid Build Coastguard WorkerFor more details of the kind of options you can pass to the Arm NN delegate please check the parameters of function tflite_plugin_create_delegate. 104*89c4ff92SAndroid Build Coastguard Worker 105*89c4ff92SAndroid Build Coastguard WorkerYou can also test the functionality of the external delegate adaptor by running some unit tests: 106*89c4ff92SAndroid Build Coastguard Worker```bash 107*89c4ff92SAndroid Build Coastguard Workerpip install pytest 108*89c4ff92SAndroid Build Coastguard Workercd armnn/delegate/python/test 109*89c4ff92SAndroid Build Coastguard Worker# You can deselect tests that require backends that your hardware doesn't support using markers e.g. -m "not GpuAccTest" 110*89c4ff92SAndroid Build Coastguard Workerpytest --delegate-dir="<path-to-armnn-binaries>/libarmnnDelegate.so" -m "not GpuAccTest" 111*89c4ff92SAndroid Build Coastguard Worker``` 112