blob: 7b7d50787cdd839a9c4b770f2caefeb8819be55f [file] [log] [blame]
<!-- Copyright (c) 2020 ARM Limited. -->
<!-- -->
<!-- SPDX-License-Identifier: MIT -->
<!-- -->
<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="robots" content="NOINDEX, NOFOLLOW" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>ArmNN: delegate/DelegateQuickStartGuide.md Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script><script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<img alt="ArmNN" src="Arm_NN_horizontal_blue.png" style="max-width: 10rem; margin-top: .5rem; margin-left 10px"/>
<td style="padding-left: 0.5em;">
<div id="projectname">
&#160;<span id="projectnumber">22.02</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('_delegate_quick_start_guide_8md.xhtml','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">delegate/DelegateQuickStartGuide.md</div> </div>
</div><!--header-->
<div class="contents">
<a href="_delegate_quick_start_guide_8md.xhtml">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;# TfLite Delegate Quick Start Guide</div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;If you have downloaded the Arm NN Github binaries or built the TfLite delegate yourself, then this tutorial will show you how you can</div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;integrate it into TfLite to run models using python.</div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;Here is an example python script showing how to do this. In this script we are making use of the </div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;[external adaptor](https://www.tensorflow.org/lite/performance/implementing_delegate#option_2_leverage_external_delegate) </div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;tool of TfLite that allows you to load delegates at runtime.</div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;```python</div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;import numpy as np</div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;import tflite_runtime.interpreter as tflite</div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;</div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;# Load TFLite model and allocate tensors.</div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;# (if you are using the complete tensorflow package you can find load_delegate in tf.experimental.load_delegate)</div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;armnn_delegate = tflite.load_delegate( library=&quot;&lt;path-to-armnn-binaries&gt;/libarmnnDelegate.so&quot;,</div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160; options={&quot;backends&quot;: &quot;CpuAcc,GpuAcc,CpuRef&quot;, &quot;logging-severity&quot;:&quot;info&quot;})</div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;# Delegates/Executes all operations supported by Arm NN to/with Arm NN</div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;interpreter = tflite.Interpreter(model_path=&quot;&lt;your-armnn-repo-dir&gt;/delegate/python/test/test_data/mock_model.tflite&quot;, </div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160; experimental_delegates=[armnn_delegate])</div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;interpreter.allocate_tensors()</div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;# Get input and output tensors.</div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;input_details = interpreter.get_input_details()</div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;output_details = interpreter.get_output_details()</div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;</div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;# Test model on random input data.</div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;input_shape = input_details[0][&#39;shape&#39;]</div><div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160;input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)</div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160;interpreter.set_tensor(input_details[0][&#39;index&#39;], input_data)</div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160;</div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160;interpreter.invoke()</div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;</div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160;# Print out result</div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160;output_data = interpreter.get_tensor(output_details[0][&#39;index&#39;])</div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160;print(output_data)</div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160;```</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160;</div><div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160;# Prepare the environment</div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160;Pre-requisites:</div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; * Dynamically build Arm NN Delegate library or download the Arm NN binaries</div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; * python3 (Depends on TfLite version)</div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; * virtualenv</div><div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; * numpy (Depends on TfLite version)</div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; * tflite_runtime (&gt;=2.5, depends on Arm NN Delegate)</div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160;</div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160;If you haven&#39;t built the delegate yet then take a look at the [build guide](./BuildGuideNative.md). Otherwise, </div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160;you can download the binaries [here](https://github.com/ARM-software/armnn/releases/)</div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160;</div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160;We recommend creating a virtual environment for this tutorial. For the following code to work python3 is needed. Please</div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160;also check the documentation of the TfLite version you want to use. There might be additional prerequisites for the python</div><div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160;version. We will use Tensorflow Lite 2.5.0 for this guide.</div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160;```bash</div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160;# Install python3 (We ended up with python3.5.3) and virtualenv</div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160;sudo apt-get install python3-pip</div><div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160;sudo pip3 install virtualenv</div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160;</div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160;# create a virtual environment</div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160;cd your/tutorial/dir</div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160;# creates a directory myenv at the current location</div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;virtualenv -p python3 myenv </div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160;# activate the environment</div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;source myenv/bin/activate</div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160;```</div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160;</div><div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160;Now that the environment is active we can install additional packages we need for our example script. As you can see </div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160;in the python script at the start of this page, this tutorial uses the `tflite_runtime` rather than the whole tensorflow </div><div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160;package. The `tflite_runtime` is a package that wraps the TfLite Interpreter. Therefore it can only be used to run inferences of </div><div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160;TfLite models. But since Arm NN is only an inference engine itself this is a perfect match. The </div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160;`tflite_runtime` is also much smaller than the whole tensorflow package and better suited to run models on </div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160;mobile and embedded devices.</div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160;</div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160;The TfLite [website](https://www.tensorflow.org/lite/guide/python) shows you two methods to download the `tflite_runtime` package. </div><div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160;In our experience, the use of the pip command works for most systems including debian. However, if you&#39;re using an older version of Tensorflow, </div><div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160;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).</div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;But in our case, with Tensorflow Lite 2.5.0, we can install through:</div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160;</div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160;```</div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime</div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160;```</div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160;</div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160;Your virtual environment is now all setup. Copy the final python script into a python file e.g. </div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160;`ExternalDelegatePythonTutorial.py`. Modify the python script above and replace `&lt;path-to-armnn-binaries&gt;` and </div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160;`&lt;your-armnn-repo-dir&gt;` with the directories you have set up. If you&#39;ve been using the [native build guide](./BuildGuideNative.md) </div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160;this will be `$BASEDIR/armnn/build` and `$BASEDIR/armnn`.</div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160;</div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160;Finally, execute the script:</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160;```bash</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160;python ExternalDelegatePythonTutorial.py</div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;```</div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;The output should look similar to this:</div><div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;```bash</div><div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160;Info: Arm NN v28.0.0</div><div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160;</div><div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160;Info: Initialization time: 0.56 ms</div><div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160;</div><div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160;INFO: TfLiteArmnnDelegate: Created TfLite Arm NN delegate.</div><div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160;[[ 12 123 16 12 11 14 20 16 20 12]]</div><div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160;Info: Shutdown time: 0.28 ms</div><div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160;```</div><div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160;</div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160;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.</div><div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160;</div><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160;You can also test the functionality of the external delegate adaptor by running some unit tests:</div><div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160;```bash</div><div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160;pip install pytest</div><div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160;cd armnn/delegate/python/test</div><div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160;# You can deselect tests that require backends that your hardware doesn&#39;t support using markers e.g. -m &quot;not GpuAccTest&quot;</div><div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160;pytest --delegate-dir=&quot;&lt;path-to-armnn-binaries&gt;/libarmnnDelegate.so&quot; -m &quot;not GpuAccTest&quot;</div><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160;```</div></div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="_delegate_quick_start_guide_8md.xhtml">DelegateQuickStartGuide.md</a></li>
<li class="footer">Generated on Tue Feb 22 2022 11:38:54 for ArmNN by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
</ul>
</div>
</body>
</html>