Skip to content

Commit b0da72f

Browse files
committed
Update readme. Add patch for hexagon_nn_3.5.4 shared libs. Add patch for ImagenetCv.
Signed-off-by: Jonathan Green <[email protected]>
1 parent 68b77ac commit b0da72f

File tree

13 files changed

+476
-0
lines changed

13 files changed

+476
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted
4+
provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright notice, this list of conditions
7+
and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright notice, this list of
9+
conditions and the following disclaimer in the documentation and/or other materials
10+
provided with the distribution.
11+
* Neither the name of the copyright holder nor the names of its contributors may be used to
12+
endorse or promote products derived from this software without specific prior written
13+
permission.
14+
15+
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
16+
LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22+
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Where there is uncertainty as to how, or
26+
where, to apply marks, open an OSR to escalate to OSG for review.
27+
28+
SPDX-License-Identifier: BSD-3-Clause-Clear
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
# Inception V3 on Qualcomm Robotics RB5 DSP
2+
3+
This project is designed to show how you can use the Qualcomm® Robotics RB5 development kit, specifically using the Qualcomm® Hexagon™ DSP with Hexagon Vector extensions (HVX), coupled with Hexagon DSP SDK to achieve high speed and high performance on device Machine Learning.
4+
5+
## Getting Started guide
6+
7+
### Step 1: Setting up the Host Linux Machine
8+
9+
Build Hexagon NN DSP library for the Qualcomm Robotics RB5 CDSP
10+
11+
- Download the Hexagon SDK version 3.5.4 for Linux from here.
12+
13+
- https://developer.qualcomm.com/software/hexagon-dsp-sdk
14+
15+
- Unzip the file and install the SDK using the following commands
16+
17+
```
18+
cd qualcomm_hexagon_sdk_3_5_4_linux/
19+
chmod +x qualcomm_hexagon_sdk_3_5_4_eval.bin
20+
./qualcomm_hexagon_sdk_3_5_4_eval.bin
21+
```
22+
23+
- Set the environment variable $HEXAGON_SDK_ROOT by the command
24+
25+
```
26+
source ~/Qualcomm/Hexagon_SDK/3.4.2/setup_sdk_env.source
27+
```
28+
29+
![image text](image/01_Set_ENV.png)
30+
31+
- Compile Hexagon NN using the following commands and generate a hexagon library
32+
33+
```
34+
cd $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/
35+
make tree VERBOSE=1 CDSP_FLAG=1 V=hexagon_Release_dynamic_toolv83_v66 V66=1
36+
```
37+
38+
![image text](image/02_make.png)
39+
40+
The libhexagon_nn_skel.so generated here is the shared library for the CDSP
41+
42+
### Step 2: Generate Testsig and sign the previously generated shared library-libhexagon_nn_skel.so with it.
43+
44+
- To generate a Testsig, the serial number of Target robotics development kit is needed.
45+
46+
- Get serial number from the robotics development kit ’s console using command.
47+
48+
```
49+
cd $HEXAGON_SDK_ROOT/tools/elfsigner/getserial/UbuntuARM_Release_aarch64
50+
adb push getserial /
51+
adb shell chmod 777 getserial
52+
adb shell
53+
./getserial
54+
```
55+
56+
The above command returns the serial number of the attached device:
57+
58+
```
59+
####################Serial number (see below)###########################
60+
61+
Serial Num : 0x7f7595b7
62+
63+
####################Serial number (see above)###########################
64+
```
65+
66+
- Pass the serial_number to elfsigner.py using the following commands:
67+
68+
```
69+
python $HEXAGON_SDK_ROOT/tools/elfsigner/elfsigner.py -t 0x<serial number>
70+
```
71+
72+
![image text](image/04_elfsigner.png)
73+
74+
- Sign the shared library libhexagon_nn_skel.so using elfsigner.py
75+
76+
```
77+
cd $HEXAGON_SDK_ROOT
78+
mkdir signed_libs
79+
python tools/elfsigner/elfsigner.py -i libs/hexagon_nn/2.10.1/hexagon_Release_dynamic_toolv83_v66/ship/libhexagon_nn_skel.so -o signed_libs/
80+
```
81+
82+
![image text](image/05_libhexagon_nn_skel.png)
83+
84+
### Step 3: Generate Inception v3 model graph for Hexagon DSP.
85+
86+
In this project, you will use an Inception v3 model’s frozen graph for the image classification. In the following steps, download TensorFlow, Bazel and generate a Hexagon DSP compatible version of the Inception V3 frozen graph.
87+
88+
- Download TensorFlow Inception V3 model Frozen graph using
89+
90+
```
91+
curl http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz -o /tmp/inceptionv3.tgz
92+
```
93+
94+
![image text](image/06_Download-TensorFlow.png)
95+
96+
and unzip it to /tmp directory.
97+
98+
```
99+
tar -xzf /tmp/inceptionv3.tgz -C /tmp/
100+
```
101+
102+
Download and install Bazel (version 0.20.0) using the following commands:
103+
104+
```
105+
wget https://github.com/bazelbuild/bazel/releases/download/0.20.0/bazel-0.20.0-installer-linux-x86_64.sh
106+
chmod +x bazel-0.20.0-installer-linux-x86_64.sh
107+
./bazel-0.20.0-installer-linux-x86_64.sh --user
108+
```
109+
110+
![image text](image/07_install_Bazel.png)
111+
![image text](image/08_make_Bazel.png)
112+
113+
Download and build TensorFlow(version 1.13.2) using the following commands
114+
115+
```
116+
wget https://github.com/tensorflow/tensorflow/archive/v1.13.2.tar.gz
117+
tar -zxvf v1.13.2.tar.gz
118+
cd tensorflow-1.13.2
119+
TF_ENABLE_XLA=y \
120+
TF_NEED_OPENCL_SYCL=n \
121+
TF_NEED_ROCM=n \
122+
TF_NEED_CUDA=n \
123+
TF_DOWNLOAD_CLANG=n \
124+
TF_NEED_MPI=n \
125+
CC_OPT_FLAGS='-march=native -Wno-sign-compare' \
126+
TF_SET_ANDROID_WORKSPACE=n \
127+
./configure
128+
```
129+
130+
Build transform_graph tool in TensorFlow using the following commands. The tensorflow binaries have changed, so we need up update the expected hash in the bazel file.
131+
132+
```
133+
touch WORKSPACE
134+
sed -i 's/e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761/86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181/g' third_party/icu/workspace.bzl
135+
bazel build tensorflow/tools/graph_transforms:transform_graph
136+
```
137+
138+
Quantize protobuf using Graph Transform Tool
139+
140+
```
141+
bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=/tmp/classify_image_graph_def.pb --out_graph=/tmp/inception_v3_quantized.pb --inputs="Mul" --outputs='softmax' --transforms='add_default_attributes strip_unused_nodes(type=float, shape="1,299,299,3") remove_nodes(op=Identity, op=CheckNumerics) fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms quantize_weights quantize_nodes fold_constants strip_unused_nodes sort_by_execution_order'
142+
```
143+
144+
Convert the quantized protobuf into a C file (iv3.c) using the following commands
145+
146+
```
147+
cd $HEXAGON_SDK_ROOT/examples/hexagon_nn/
148+
apt-get install virtualenv
149+
virtualenv -p python2 env2
150+
```
151+
152+
If this gives an error “virtualenv not found”/ “pip missing”, install pip using
153+
154+
```
155+
apt-get install python-pip
156+
apt-get install python-numpy
157+
```
158+
159+
and install virtualenv using
160+
161+
```
162+
sudo apt-get install python-virtualenv
163+
source env2/bin/activate
164+
pip install --upgrade 'setuptools<45.0.0'
165+
pip install -r $HEXAGON_SDK_ROOT/Hexagon_SDK/3.5.4/tools/python_venv/environments/req2.txt
166+
```
167+
168+
Enter the command below to generate Hexagon compatible iv3.c file,
169+
170+
```
171+
python $HEXAGON_SDK_ROOT/examples/hexagon_nn/scripts/tensorflow_to_hexagon_nn.py /tmp/inception_v3_quantized.pb $HEXAGON_SDK_ROOT/examples/hexagon_nn/tutorials/007-tensorflow-to-hexagon-nn/inceptionv3_v1.yaml > iv3.c
172+
```
173+
174+
And copy iv3.c to the folder as shown in the command
175+
176+
```
177+
cp iv3.c $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/
178+
```
179+
180+
### Step 4: Download linaro64
181+
182+
Please download gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu.tar.xz from http://releases.linaro.org/archive/14.11/components/toolchain/binaries/aarch64-linux-gnu
183+
184+
Extract the tar file and copy folder gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu to <Hexagon_SDK_ROOT>/tools/ folder.
185+
186+
Rename gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu folder to linaro64
187+
188+
```
189+
cd $HEXAGON_SDK_ROOT/tools
190+
wget https://releases.linaro.org/archive/14.11/components/toolchain/binaries/aarch64-linux-gnu/gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu.tar.xz
191+
tar -xf gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu.tar.xz
192+
mv gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu linaro64
193+
rm gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu.tar.xz
194+
```
195+
196+
### Step 5: Compile hexagon-nn shared user space library
197+
198+
```
199+
cd $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/
200+
```
201+
202+
- Patch Hexagon NN with the downloaded patch to compile as shared library for user space
203+
204+
```
205+
patch --binary -p1 < hexagon_nn_3.5.4_shared_lib.patch
206+
make tree V=UbuntuARM_Release_aarch64 CDSP_FLAG=1 V66=1 GRAPHINIT="iv3.c"
207+
```
208+
209+
![image text](image/12_make.png)
210+
211+
### Step 6: Push generated files to target and run it
212+
213+
```
214+
adb push $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/UbuntuARM_Release_aarch64/graph_app /data/
215+
adb push $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/UbuntuARM_Release_aarch64/libhexagon_nn.so /usr/lib
216+
adb push $HEXAGON_SDK_ROOT/signed_libs/libhexagon_nn_skel.so /usr/lib/rfsa/adsp/
217+
adb push $HEXAGON_SDK_ROOT/libs/hexagon_nn/2.10.1/test/panda_299x299.dat /data/
218+
adb shell chmod a+x /data/graph_app
219+
adb shell /data/graph_app --input_to_float 1 /data/panda_299x299.dat
220+
```
221+
222+
You should expect output like the following:
223+
224+
```
225+
Run!
226+
output size=4032
227+
Rank,Softmax,index,string
228+
0,0.940308,169,giant panda
229+
1,0.002943,7,lesser panda
230+
2,0.000613,61,brown bear
231+
3,0.000534,878,earthstar
232+
4,0.000424,374,lawn mower
233+
AppReported: 41682408
234+
```
235+
236+
### Part 2 - On the Qualcomm Robotics RB5 Development Kit (Target)
237+
238+
### Step 1: Make hardware connections and power up Robotics Development Kit
239+
240+
- Connect Robotics Development Kit with USB Camera and wireless USB for Keyboard and mouse.
241+
- Connect Robotics Development Kit with HDMI cable and the other end of the cable to the HDMI Screen/monitor that is powered on.
242+
- Connect the power cable to Robotics Development Kit and wait for the desktop to pop up.
243+
244+
### Step 2: Start cdsprpcd daemon
245+
246+
```
247+
adb shell
248+
cdsprpcd&
249+
```
250+
251+
### Step 3: Build OpenCV for Wayland
252+
253+
The Qualcomm Robotics RB5 development board supports Wayland/Weston as the display server, OpenCV now supports Wayland but needs to be compiled with support.
254+
255+
> :warning: OpenCV 4.0 had some kind of memory leak when using GStreamer that resulted in memory consumption growing continuously as images were captured. OpenCV 4.9 was tested and did not display the memory growth behavior.
256+
257+
```
258+
root@qrb5165-rb5:/# apt-get install build-essential curl unzip
259+
root@qrb5165-rb5:/# apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
260+
root@qrb5165-rb5:/# apt-get install libjpeg-dev libpng-dev
261+
root@qrb5165-rb5:/# apt-get install python-numpy libxkbcommon-dev libwayland-client0 libwayland-dev
262+
root@qrb5165-rb5:/# cd /data
263+
root@qrb5165-rb5:/# git clone https://github.com/opencv/opencv.git
264+
root@qrb5165-rb5:/# cd opencv/
265+
root@qrb5165-rb5:/# mkdir build
266+
root@qrb5165-rb5:/# cd build
267+
root@qrb5165-rb5:/# cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/usr/local -DWITH_IPP=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -DWITH_WAYLAND=ON -DWITH_GTK=OFF ..
268+
root@qrb5165-rb5:/# make -j8
269+
```
270+
271+
### Step 4: Run Weston desktop on Qualcomm Robotics RB5 Development kit
272+
273+
If the HDMI cable connects properly, after the device boot up, you will see the device boot up logs and then see a shell is ready for command input. Run the following command to launch Weston desktop.
274+
275+
```
276+
root@qrb5165-rb5:/# weston --connector=29
277+
```
278+
279+
If it fails to run, you can try the following commands with adb on an Ubuntu PC. Please make sure the Qualcomm Robotics RB5 development kit is connected to the Ubuntu PC with a USB cable. Please ignore this step if the above command runs successfully.
280+
281+
```
282+
root@qrb5165-rb5:/# mkdir -p /usr/bin/weston_socket
283+
root@qrb5165-rb5:/# chmod 700 /usr/bin/weston_socket
284+
root@qrb5165-rb5:/# export XDG_RUNTIME_DIR=/usr/bin/weston_socket
285+
root@qrb5165-rb5:/# export LD_LIBRARY_PATH=/usr/lib:/usr/lib/aarch64-linux-gnu/
286+
root@qrb5165-rb5:/# weston --tty=1 --connector=29 --backend=drm-backend.so
287+
```
288+
289+
### Step 5: Run OpenCV ImageNet application for camera
290+
291+
Download the application from the camera branch and build using the following commands. The ImagenetCv code is designed to use a USB camera using Video4Linux 2. The RB5 main MIPI cameras do not work with Video4Linux and are only compatible with GStreamer. So apply the patch to capture from a gstreamer pipeline.
292+
293+
```
294+
root@qrb5165-rb5:/# git clone https://git.linaro.org/people/srinivas.kandagatla/ImagenetCv.git
295+
root@qrb5165-rb5:/# cd ImagenetCv
296+
root@qrb5165-rb5:/# git branch -a
297+
root@qrb5165-rb5:/# git checkout camera
298+
patch --binary -p1 < imagenetcv.patch
299+
```
300+
301+
Build application using
302+
303+
```
304+
root@qrb5165-rb5:/# cmake .
305+
root@qrb5165-rb5:/# make
306+
```
307+
308+
Run the application using the command
309+
310+
```
311+
root@qrb5165-rb5:/# ./ImagenetCv
312+
```
313+
314+
If this shows an error like “install libgtk2.0-dev”
315+
316+
Install libgtk2.0-dev using the commands given below and run the application
317+
318+
```
319+
root@qrb5165-rb5:/# apt-get update
320+
root@qrb5165-rb5:/# apt-get upgrade
321+
root@qrb5165-rb5:/# apt-get install libgtk2.0-dev
322+
```
323+
324+
Running the application pops up a window that shows video captured by the camera and the image classification being done
325+
![image text](image/wayland-screenshot.png)
326+
327+
## License
328+
329+
This is licensed under the BSD 3-Clause-Clear “New” or “Revised” License. Check out the [LICENSE](LICENSE) for more details.

0 commit comments

Comments
 (0)