ArmNN
 22.11
delegate/DelegateQuickStartGuide.md
Go to the documentation of this file.
1 # TfLite Delegate Quick Start Guide
2 If you have downloaded the Arm NN Github binaries or built the TfLite delegate yourself, then this tutorial will show you how you can
3 integrate it into TfLite to run models using python.
4 
5 Here is an example python script showing how to do this. In this script we are making use of the
6 [external adaptor](https://www.tensorflow.org/lite/performance/implementing_delegate#option_2_leverage_external_delegate)
7 tool of TfLite that allows you to load delegates at runtime.
8 ```python
9 import numpy as np
10 import tflite_runtime.interpreter as tflite
11 
12 # Load TFLite model and allocate tensors.
13 # (if you are using the complete tensorflow package you can find load_delegate in tf.experimental.load_delegate)
14 armnn_delegate = tflite.load_delegate( library="<path-to-armnn-binaries>/libarmnnDelegate.so",
15  options={"backends": "CpuAcc,GpuAcc,CpuRef", "logging-severity":"info"})
16 # Delegates/Executes all operations supported by Arm NN to/with Arm NN
17 interpreter = tflite.Interpreter(model_path="<your-armnn-repo-dir>/delegate/python/test/test_data/mock_model.tflite",
18  experimental_delegates=[armnn_delegate])
19 interpreter.allocate_tensors()
20 
21 # Get input and output tensors.
22 input_details = interpreter.get_input_details()
23 output_details = interpreter.get_output_details()
24 
25 # Test model on random input data.
26 input_shape = input_details[0]['shape']
27 input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)
28 interpreter.set_tensor(input_details[0]['index'], input_data)
29 
30 interpreter.invoke()
31 
32 # Print out result
33 output_data = interpreter.get_tensor(output_details[0]['index'])
34 print(output_data)
35 ```
36 
37 # Prepare the environment
38 Pre-requisites:
39  * Dynamically build Arm NN Delegate library or download the Arm NN binaries
40  * python3 (Depends on TfLite version)
41  * virtualenv
42  * numpy (Depends on TfLite version)
43  * tflite_runtime (>=2.5, depends on Arm NN Delegate)
44 
45 If 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 
47 ```bash
48 export LD_LIBRARY_PATH=<path_to_so_binary_files>
49 ```
50 
51 We recommend creating a virtual environment for this tutorial. For the following code to work python3 is needed. Please
52 also check the documentation of the TfLite version you want to use. There might be additional prerequisites for the python
53 version. We will use Tensorflow Lite 2.5.0 for this guide.
54 ```bash
55 # Install python3 (We ended up with python3.5.3) and virtualenv
56 sudo apt-get install python3-pip
57 sudo pip3 install virtualenv
58 
59 # create a virtual environment
60 cd your/tutorial/dir
61 # creates a directory myenv at the current location
62 virtualenv -p python3 myenv
63 # activate the environment
64 source myenv/bin/activate
65 ```
66 
67 Now that the environment is active we can install additional packages we need for our example script. As you can see
68 in the python script at the start of this page, this tutorial uses the `tflite_runtime` rather than the whole tensorflow
69 package. The `tflite_runtime` is a package that wraps the TfLite Interpreter. Therefore it can only be used to run inferences of
70 TfLite models. But since Arm NN is only an inference engine itself this is a perfect match. The
71 `tflite_runtime` is also much smaller than the whole tensorflow package and better suited to run models on
72 mobile and embedded devices.
73 
74 The TfLite [website](https://www.tensorflow.org/lite/guide/python) shows you two methods to download the `tflite_runtime` package.
75 In 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 you 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 But in our case, with Tensorflow Lite 2.5.0, we can install through:
78 
79 ```
80 pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime==2.5.0
81 ```
82 
83 Your virtual environment is now all setup. Copy the final python script into a python file e.g.
84 `ExternalDelegatePythonTutorial.py`. Modify the python script above and replace `<path-to-armnn-binaries>` and
85 `<your-armnn-repo-dir>` with the directories you have set up. If you've been using the [native build guide](./BuildGuideNative.md)
86 this will be `$BASEDIR/armnn/build` and `$BASEDIR/armnn`.
87 
88 Finally, execute the script:
89 ```bash
90 python ExternalDelegatePythonTutorial.py
91 ```
92 The output should look similar to this:
93 ```bash
94 Info: Arm NN v28.0.0
95 
96 Info: Initialization time: 0.56 ms
97 
98 INFO: TfLiteArmnnDelegate: Created TfLite Arm NN delegate.
99 [[ 12 123 16 12 11 14 20 16 20 12]]
100 Info: Shutdown time: 0.28 ms
101 ```
102 
103 For 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 
105 You can also test the functionality of the external delegate adaptor by running some unit tests:
106 ```bash
107 pip install pytest
108 cd armnn/delegate/python/test
109 # You can deselect tests that require backends that your hardware doesn't support using markers e.g. -m "not GpuAccTest"
110 pytest --delegate-dir="<path-to-armnn-binaries>/libarmnnDelegate.so" -m "not GpuAccTest"
111 ```