aboutsummaryrefslogtreecommitdiff
path: root/21.02/_integrate_delegate_into_python_8md_source.xhtml
diff options
context:
space:
mode:
authorJan Eilers <jan.eilers@arm.com>2021-02-25 17:44:00 +0000
committerJan Eilers <jan.eilers@arm.com>2021-02-25 18:27:49 +0000
commitfd627ffaec8fd8801d980b4c91ee7c0607ab6aaf (patch)
treeeb4bc8f9b411f30c7655616142b5a4bdd3a1acd0 /21.02/_integrate_delegate_into_python_8md_source.xhtml
parentfb14ebbd68e04876809145296af96f6f41857418 (diff)
downloadarmnn-fd627ffaec8fd8801d980b4c91ee7c0607ab6aaf.tar.gz
IVGCVSW-5687 Update Doxygen Docu
* Update Doxygen Documentation for 21.02 release Signed-off-by: Jan Eilers <jan.eilers@arm.com> Change-Id: I9ed2f9caab038836ea99d7b378d7899fe431a4e5
Diffstat (limited to '21.02/_integrate_delegate_into_python_8md_source.xhtml')
-rw-r--r--21.02/_integrate_delegate_into_python_8md_source.xhtml113
1 files changed, 113 insertions, 0 deletions
diff --git a/21.02/_integrate_delegate_into_python_8md_source.xhtml b/21.02/_integrate_delegate_into_python_8md_source.xhtml
new file mode 100644
index 0000000000..cc1a7ec05f
--- /dev/null
+++ b/21.02/_integrate_delegate_into_python_8md_source.xhtml
@@ -0,0 +1,113 @@
+<!-- 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/IntegrateDelegateIntoPython.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">21.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('_integrate_delegate_into_python_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/IntegrateDelegateIntoPython.md</div> </div>
+</div><!--header-->
+<div class="contents">
+<a href="_integrate_delegate_into_python_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;# Integrate the TfLite delegate into TfLite using Python</div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;If you have built the TfLite delegate as a separate dynamic library 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 in 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;your-armnn-build-dir&gt;/delegate/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 ArmNN to/with ArmNN</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</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.0, 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).</div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160;</div><div class="line"><a name="l00047"></a><span class="lineno"> 47</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="l00048"></a><span class="lineno"> 48</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="l00049"></a><span class="lineno"> 49</span>&#160;version.</div><div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160;```bash</div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160;# Install python3 (We ended up with python3.5.3) and virtualenv</div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160;sudo apt-get install python3-pip</div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160;sudo pip3 install virtualenv</div><div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160;</div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160;# create a virtual environment</div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160;cd your/tutorial/dir</div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160;# creates a directory myenv at the current location</div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160;virtualenv -p python3 myenv </div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;# activate the environment</div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160;source myenv/bin/activate</div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;```</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;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="l00064"></a><span class="lineno"> 64</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="l00065"></a><span class="lineno"> 65</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="l00066"></a><span class="lineno"> 66</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="l00067"></a><span class="lineno"> 67</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="l00068"></a><span class="lineno"> 68</span>&#160;mobile and embedded devices.</div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160;</div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160;At the time of writing, there are no packages of either `tensorflow` or `tflite_runtime` available on `pypi` that </div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160;are built for an arm architecture. That means installing them using `pip` on your development board is currently not </div><div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160;possible. The TfLite [website](https://www.tensorflow.org/lite/guide/python) points you at prebuilt `tflite_runtime` </div><div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160;packages. However, that limits you to specific TfLite and Python versions. For this reason we will build the </div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;`tflite_runtime` from source.</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;You will have downloaded the tensorflow repository in order to build the Arm NN delegate. In there you can find further </div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;instructions on how to build the `tflite_runtime` under `tensorflow/lite/tools/pip_package/README.md`. This tutorial </div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160;uses bazel to build it natively but there are scripts for cross-compilation available as well.</div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160;```bash</div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160;# Add the directory where bazel is built to your PATH so that the script can find it</div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160;PATH=$PATH:your/build/dir/bazel/output</div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160;# Run the following script to build tflite_runtime natively.</div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160;tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh</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;The execution of the script creates a `.whl` file which can be used by `pip` to install the TfLite Runtime package. </div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160;The build-script produces some output in which you can find the location where the `.whl` file was created. Then all that is </div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160;left to do is to install all necessary python packages with `pip`.</div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;```bash</div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;pip install tensorflow/lite/tools/pip_package/gen/tflite_pip/python3/dist/tflite_runtime-2.3.1-py3-none-any.whl numpy</div><div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;```</div><div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160;</div><div class="line"><a name="l00092"></a><span class="lineno"> 92</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="l00093"></a><span class="lineno"> 93</span>&#160;`ExternalDelegatePythonTutorial.py`. Modify the python script above and replace `&lt;your-armnn-build-dir&gt;` and </div><div class="line"><a name="l00094"></a><span class="lineno"> 94</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="l00095"></a><span class="lineno"> 95</span>&#160;this will be `$BASEDIR/armnn/build` and `$BASEDIR/armnn`.</div><div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160;</div><div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160;Finally, execute the script:</div><div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160;```bash</div><div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160;python ExternalDelegatePythonTutorial.py</div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160;```</div><div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160;The output should look similar to this:</div><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160;```bash</div><div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160;Info: ArmNN v23.0.0</div><div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160;</div><div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160;Info: Initialization time: 0.56 ms</div><div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160;</div><div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160;INFO: TfLiteArmnnDelegate: Created TfLite ArmNN delegate.</div><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160;[[ 12 123 16 12 11 14 20 16 20 12]]</div><div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160;Info: Shutdown time: 0.28 ms</div><div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160;```</div><div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160;</div><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160;For more details on what kind of options you can pass to the Arm NN delegate please check </div><div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160;[armnn_delegate_adaptor.cpp](src/armnn_external_delegate.cpp).</div><div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160;</div><div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160;You can also test the functionality of the external delegate adaptor by running some unit tests:</div><div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160;```bash</div><div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160;pip install pytest</div><div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160;cd armnn/delegate/python/test</div><div class="line"><a name="l00119"></a><span class="lineno"> 119</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="l00120"></a><span class="lineno"> 120</span>&#160;pytest --delegate-dir=&quot;&lt;your-armnn-build-dir&gt;/armnn/delegate/libarmnnDelegate.so&quot; -m &quot;not GpuAccTest&quot; </div><div class="line"><a name="l00121"></a><span class="lineno"> 121</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="_integrate_delegate_into_python_8md.xhtml">IntegrateDelegateIntoPython.md</a></li>
+ <li class="footer">Generated on Thu Feb 25 2021 17:27:28 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>