Skip to content

Commit acf1102

Browse files
committed
add tflite_classification sample
Signed-off-by: craft <convercraft@gmail.com>
1 parent 949de5e commit acf1102

12 files changed

+223
-479
lines changed

Makefile

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@
1414

1515
TARGET_TOOLCHAIN_PREFIX = aarch64-linux-gnu-
1616

17-
FLATBUFFERS_INC_DIR = /home/craft/workspace/gem5/tensorflow_src/build/flatbuffers/include/
18-
TFLITE_INC_DIR = /home/craft/workspace/gem5/tensorflow_src/
19-
OPENCV_INC_DIR = /home/craft/workspace/gem5/opencv/build/install/include/opencv4/
17+
TF_SRC_DIR ?=/home/craft/workspace/gem5/tensorflow_src
18+
TFLITE_INC_DIR ?=$(TF_SRC_DIR)
19+
FLATBUFFERS_INC_DIR ?=$(TF_SRC_DIR)/build/flatbuffers/include
20+
TFLITE_LIB_DIR ?=$(TF_SRC_DIR)/build
21+
TFLITE_FLATBUF_LIB_DIR ?=$(TF_SRC_DIR)/build/_deps/flatbuffers-build
22+
TFLITE_RUY_LIB_DIR ?=$(TF_SRC_DIR)/build/_deps/ruy-build/ruy
23+
TFLITE_FARMHASH_LIB_DIR ?=$(TF_SRC_DIR)/build/_deps/farmhash-build
24+
TFLITE_FFT2D_DIR ?=$(TF_SRC_DIR)/build/_deps/fft2d-build
25+
TFLITE_CPUINFO_DIR ?=$(TF_SRC_DIR)/build/_deps/cpuinfo-build
26+
TFLITE_CLOG_DIR ?=$(TF_SRC_DIR)/build/_deps/clog-build
2027

21-
TFLITE_LIB_DIR = /home/craft/workspace/gem5/tensorflow_src/build/
22-
TFLITE_FLATBUF_LIB_DIR = /home/craft/workspace/gem5/tensorflow_src/build/_deps/flatbuffers-build/
23-
TFLITE_RUY_LIB_DIR = /home/craft/workspace/gem5/tensorflow_src/build/_deps/ruy-build/ruy
24-
OPENCV_LIB_DIR = /home/craft/workspace/gem5/opencv/build/install/lib/
25-
OPENCV_LIB_3RDPARTY_DIR = /home/craft/workspace/gem5/opencv/build/install/lib/opencv4/3rdparty/
28+
OPENCV_SRC_DIR ?=/home/craft/workspace/gem5/opencv
29+
OPENCV_INC_DIR ?=$(OPENCV_SRC_DIR)/build/install/include/opencv4/
30+
OPENCV_LIB_DIR ?=$(OPENCV_SRC_DIR)/build/install/lib/
31+
OPENCV_LIB_3RDPARTY_DIR ?=$(OPENCV_SRC_DIR)/build/install/lib/opencv4/3rdparty/
2632

2733
PHONY: all
2834

29-
all: classification segmentation
35+
all: classification/tflite_classification segmentation/tflite_segmentation
3036

3137
LIBS = $(TFLITE_LIB_DIR)/libtensorflow-lite.a
3238
LIBS += $(TFLITE_RUY_LIB_DIR)/libruy_ctx.a \
@@ -51,17 +57,20 @@ LIBS += $(TFLITE_RUY_LIB_DIR)/libruy_ctx.a \
5157
$(TFLITE_RUY_LIB_DIR)/libruy_prepare_packed_matrices.a \
5258
$(TFLITE_RUY_LIB_DIR)/libruy_denormal.a
5359

54-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/farmhash-build/libfarmhash.a
55-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/flatbuffers-build/libflatbuffers.a
56-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/fft2d-build/libfft2d_fftsg.a
57-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/fft2d-build/libfft2d_fftsg2d.a
58-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/cpuinfo-build/libcpuinfo.a
59-
LIBS += /home/craft/workspace/gem5/tensorflow_src/build/_deps/clog-build/libclog.a
60+
LIBS += $(TFLITE_FLATBUF_LIB_DIR)/libflatbuffers.a
61+
LIBS += $(TFLITE_FARMHASH_LIB_DIR)/libfarmhash.a
62+
LIBS += $(TFLITE_FFT2D_DIR)/libfft2d_fftsg.a
63+
LIBS += $(TFLITE_FFT2D_DIR)/libfft2d_fftsg2d.a
64+
LIBS += $(TFLITE_CPUINFO_DIR)/libcpuinfo.a
65+
LIBS += $(TFLITE_CLOG_DIR)/libclog.a
6066

6167
LIBS += $(OPENCV_LIB_DIR)/libopencv_world.a
62-
# -Wl,-rpath-link,$(OPENCV_LIB_DIR)
63-
LIBS += $(OPENCV_LIB_3RDPARTY_DIR)/liblibjpeg-turbo.a $(OPENCV_LIB_3RDPARTY_DIR)/liblibpng.a $(OPENCV_LIB_3RDPARTY_DIR)/libtegra_hal.a $(OPENCV_LIB_3RDPARTY_DIR)/libzlib.a
64-
# -Wl,-rpath-link,$(OPENCV_LIB_3RDPARTY_DIR)
68+
69+
LIBS += $(OPENCV_LIB_3RDPARTY_DIR)/liblibjpeg-turbo.a \
70+
$(OPENCV_LIB_3RDPARTY_DIR)/liblibpng.a \
71+
$(OPENCV_LIB_3RDPARTY_DIR)/libtegra_hal.a \
72+
$(OPENCV_LIB_3RDPARTY_DIR)/libzlib.a
73+
6574
LIBS += -lstdc++ -lpthread -lm -ldl -lrt -static
6675

6776
INCLUDES := -I. -I$(FLATBUFFERS_INC_DIR) -I$(TFLITE_INC_DIR) -I$(OPENCV_INC_DIR)
@@ -72,11 +81,13 @@ CXX := ${TARGET_TOOLCHAIN_PREFIX}g++
7281

7382
COMMON_SRC = model_utils.cc utils.cc
7483

75-
classification: classification.cc $(COMMON_SRC)
76-
$(CXX) classification.cc $(COMMON_SRC) -o tflite_classification $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
84+
classification/tflite_classification: classification.cc $(COMMON_SRC)
85+
mkdir -p classification
86+
$(CXX) classification.cc $(COMMON_SRC) -o classification/tflite_classification $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
7787

78-
segmentation: segmentation.cc $(COMMON_SRC)
79-
$(CXX) segmentation.cc $(COMMON_SRC) -o tflite_segmentation $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
88+
segmentation/tflite_segmentation: segmentation.cc $(COMMON_SRC)
89+
mkdir -p segmentation
90+
$(CXX) segmentation.cc $(COMMON_SRC) -o segmentation/tflite_segmentation $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
8091

8192
clean:
82-
rm -rf classification segmentation
93+
rm -rf classification/tflite_classification segmentation/tflite_segmentation

README

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
Forked from TI Repo https://git.ti.com/git/apps/tensorflow-lite-examples.git
22

3-
Tensorflow Lite demos with input and display via OpenCV and TIDL offload for AM5
3+
Tensorflow Lite demos with input via OpenCV and used for gem5 full system simulation
44

55
Procedure to build
66
----------------------------
77
Step 1: Set environment variables
88
* Arm only:
9-
- export SYSROOT_INCDIR (if all dependent header files are under the same directoy), or
10-
export FLATBUFFERS_INC_DIR, TFLITE_INC_DIR, and OPENCV_INC_DIR
11-
- export SYSROOT_LIBDIR (if all dependent libs are under the same directoy), or
12-
export TFLITE_LIB_DIR and OPENCV_LIB_DIR
13-
- export TARGET_TOOLCHAIN_PREFIX=arm-linux-gnueabihf-
14-
- export PATH=/[path]/gcc-arm-2019.03/bin:$PATH
15-
16-
* Add the following for TIDL offload to AM5
17-
- export TIDL_ACC=yes
18-
- export TIDL_API_DIR
19-
- export TIDL_API_LIB_DIR (if tidl-api lib is not under SYSROOT_LIBDIR)
9+
- export TARGET_TOOLCHAIN_PREFIX
10+
- export TF_SRC_DIR
11+
- export OPENCV_SRC_DIR
2012

2113
Step 2: Run "make" from the top-level directory to build the demos
2214

2315
Example:
24-
$ export SYSROOT_INCDIR="/oe-layersetup/build/arago-tmp-external-arm-toolchain/work/am57xx_evm-linux-gnueabi/tensorflow-lite-demo/01.00.00-r1/recipe-sysroot/usr/include"
25-
$ export SYSROOT_LIBDIR="/oe-layersetup/build/arago-tmp-external-arm-toolchain/work/am57xx_evm-linux-gnueabi/tensorflow-lite-demo/01.00.00-r1/recipe-sysroot/usr/lib"
26-
$ export TARGET_TOOLCHAIN_PREFIX=arm-linux-gnueabihf-
27-
$ export PATH=/tools/gcc-arm-2019.03/bin:$PATH
28-
$ export TIDL_API_DIR="/oe-layersetup/build/arago-tmp-external-arm-toolchain/work/am57xx_evm-linux-gnueabi/tensorflow-lite-demo/01.00.00-r1/recipe-sysroot/usr/share/ti/tidl"
29-
$ export TIDL_ACC=yes
16+
$ export TARGET_TOOLCHAIN_PREFIX=aarch64-linux-gnu-
17+
$ export TF_SRC_DIR="/home/craft/workspace/gem5/tensorflow_src"
18+
$ export OPENCV_SRC_DIR="/home/craft/workspace/gem5/opencv"
3019

3120
$ make
3221

@@ -53,17 +42,3 @@ Binaries to run on target
5342
--input_std, -s: input standard deviation
5443
--profiling, -p: [0|1], profiling or not
5544
--threads, -t: number of threads
56-
57-
Demos packaged with Processor SDK
58-
---------------------------------
59-
* Classification demo with Arm only, full TIDL offload, and partial TIDL offload (heterogeneous execution)
60-
# cd /usr/share/tensorflow-lite/demos
61-
# ./run_tidl_compiler.sh [This is creating mobilenet_v1_1.0_224_tidl_am5.tflite, .bin files, and subgraph0.cfg for full TIDL offload]
62-
# ./run_classification.sh [This runs Arm only classification first, and then with full TIDL offload]
63-
# ./run_tidl_compiler1.sh [This is creating mobilenet_v1_1.0_224_tidl_am5.tflite, .bin files, and subgraph0.cfg for partial TIDL offload,]
64-
[specifying MobilenetV1/MobilenetV1/Conv2d_13_pointwise/Relu6 as the output tensor of the TIDL subgraph]
65-
# ./run_classification.sh [This runs Arm only classification first, and then with partial TIDL offload]
66-
67-
* Arm only segmentation demo:
68-
# cd /usr/share/tensorflow-lite/demos
69-
# ./run_segmentation.sh

Software_Manifest.html

Lines changed: 0 additions & 311 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

doc/Cheat-Sheet.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Home
2+
## Cheat Sheet
3+
This Cheat Sheet contains all commands to run a tflite demo in gem5 fs mode.
4+
5+
## Get Started
6+
Follow these instructions to download all the required materials, build a gem5 binary and run simple examples in both System Call Emulation (SE) and Full System (FS) modes.
7+
8+
## Get GEM5
9+
- Follow this [tutorial](https://www.gem5.org/getting_started/) to build a excutable gem5 binary.
10+
- We use the v22.0.0.1 version.
11+
12+
## Simulate Arm in gem5
13+
14+
### SE Simulation
15+
16+
#### Run simple SE:
17+
```
18+
$ ./build/ARM/gem5.opt configs/example/arm/starter_se.py --cpu="minor" "tests/test-progs/hello/bin/arm/linux/hello" _# Hello world!_
19+
```
20+
#### Run multicore SE:
21+
22+
Note: each core runs its own program
23+
```
24+
$ ./build/ARM/gem5.opt configs/example/arm/starter_se.py --cpu="minor" --num-cores=2 "tests/test-progs/hello/bin/arm/linux/hello" "tests/test-progs/hello/bin/arm/linux/hello"
25+
```
26+
### FS Simulation
27+
28+
Get an Arm full system disk image from the gem5 downloads page, and set $M5_PATH:
29+
30+
> Note: the download URLs and filenames below are examples, and may change as new binaries are released by the gem5 project. Please also use a suitable $M5_PATH of your choice.
31+
```
32+
$ export M5_PATH="${PWD}/../m5_binaries"
33+
34+
$ mkdir -p "${M5_PATH}"
35+
36+
$ wget -P "${M5_PATH}" http://dist.gem5.org/dist/v22-0/arm/aarch-system-20220707.tar.bz2
37+
38+
$ tar -xjf "${M5_PATH}/aarch-system-20210904.tar.bz2" -C "${M5_PATH}"
39+
40+
$ wget -P "${M5_PATH}/disks" http://dist.gem5.org/dist/v22-0/arm/disks/ubuntu-18.04-arm64-docker.img.bz2
41+
42+
$ bunzip2 "${M5_PATH}/disks/ubuntu-18.04-arm64-docker.img.bz2"
43+
44+
$ echo "export M5_PATH=${M5_PATH}" >> ~/.bashrc
45+
```
46+
#### Run simple FS with disk image:
47+
```
48+
$ ./build/ARM/gem5.opt configs/example/arm/starter_fs.py --cpu="minor" --num-cores=1 \
49+
--disk-image=$M5_PATH/disks/ubuntu-18.04-arm64-docker.img --root-device=/dev/vda1
50+
```
51+
#### Create a checkpoint after boot:
52+
```
53+
$ telnet localhost 3456
54+
55+
$ m5 checkpoint
56+
```
57+
> Note: 3456 is the default port number used by the gem5 simulator. If this does not work, the correct port number can be found in the gem5 output:
58+
59+
system.terminal: Listening for connections on port 3456
60+
61+
A more modern alternative is to use the m5term utility provided with gem5.
62+
63+
#### Restore from a checkpoint:
64+
65+
> Note: Arm starter_fs.py allows you to specify checkpoints with --restore=m5out/cpt.TICKNUMBER/. Make sure the --num-cores and --disk-image arguments are the same as the original simulation; the --cpu type may be changed.
66+
```
67+
$ ./build/ARM/gem5.opt configs/example/arm/starter_fs.py --restore=m5out/cpt.TICKNUMBER/ --cpu="minor" --num-cores=1 --disk-image=$M5_PATH/disks/ubuntu-18.04-arm64-docker.img --root-device=/dev/vda1
68+
```
69+
## FS Benchmarks
70+
71+
### Get and Compile OpenCV
72+
73+
Install ccmake
74+
```
75+
$ sudo apt-get install cmake-curses-gui
76+
```
77+
Install the Arm cross compiler toolchain
78+
```
79+
$ sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
80+
```
81+
82+
Download OpenCV 4.6.0:
83+
```
84+
$ wget https://github.com/opencv/opencv/archive/refs/tags/4.6.0.tar.gz
85+
$ tar -xvzf opencv-4.6.0.tar.gz && cd opencv-4.6.0
86+
$ mkdir build && cd build && ccmake -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/aarch64-gnu.toolchain.cmake ..
87+
```
88+
We build the opencv world static library
89+
![image](images/1e4152e9-bb59-439f-9fff-b7a35173c0a6.png)
90+
91+
```
92+
$ make -j install
93+
```
94+
95+
### Get and Compile TFLITE
96+
97+
- Follow this [tutorial](https://www.tensorflow.org/lite/guide/build_cmake) to build a static tensorflow-lite library.
98+
- We use the v2.9.1 version.
99+
100+
### Cross-Compile Demo on x86
101+
102+
[link](../README)
103+
104+
### Run TFLITE demo
105+
To run TFLITE demo on Arm, we need to enlarge the disk image and copy the directory of compiled demo to it. Then, we need to pass the demo runscripts via the --script option to the simulation script starter_fs.py.
106+
107+
### Copy classification to Disk Image
108+
109+
#### Expand and resize the disk image:
110+
```
111+
$ cp ubuntu-18.04-arm64-docker.img expanded-ubuntu-18.04-arm64-docker.img
112+
113+
$ dd if=/dev/zero bs=1G count=20 >> ./expanded-ubuntu-18.04-arm64-docker.img # add 20G zeros
114+
115+
$ sudo parted expanded-ubuntu-18.04-arm64-docker.img resizepart 1 100% # grow partition 1
116+
```
117+
#### Mount the expanded disk image, resize the filesystem, and copy PARSEC to it:
118+
119+
#### In order to mount the disk image, we must calculate the offset of the first partition.
120+
121+
#### Run fdisk to find the start offset (in sectors) and the sector size (in bytes) of the partition.
122+
```
123+
$ fdisk -l expanded-ubuntu-18.04-arm64-docker.img
124+
```
125+
Disk expanded-ubuntu-18.04-arm64-docker.img: 21.9 GiB, 23474836480 bytes, 45849290 sectors
126+
127+
Units: sectors of 1 * 512 = 512 bytes
128+
129+
Sector size (logical/physical): 512 bytes / 512 bytes
130+
131+
I/O size (minimum/optimal): 512 bytes / 512 bytes
132+
133+
Disklabel type: dos
134+
135+
Disk identifier: 0x93d4895b
136+
137+
Device Boot Start End Sectors Size Id Type
138+
139+
expanded-ubuntu-18.04-arm64-docker.img1 128 45849289 45849162 21.9G 83 Linux
140+
141+
#### In this example the start offset (in sectors) is 128 and the sector size (in bytes) is 512, giving a start offset of 128x512=65536 bytes.
142+
143+
#### Now mount the disk image, resize the filesystem, and copy PARSEC.
144+
145+
> Note: set /path_to_compiled_parsec-3.0_dir/, and change 32256 to the offset in bytes calculted above
146+
```
147+
$ mkdir disk_mnt
148+
149+
$ sudo mount -o loop,offset=65536 expanded-ubuntu-18.04-arm64-docker.img disk_mnt
150+
151+
$ df # find /dev/loopX for disk_mnt
152+
153+
$ sudo resize2fs /dev/loopX # resize filesystem
154+
155+
$ df # check that the Available space for disk_mnt is increased
156+
157+
$ mkdir -p disk_mnt/root/tflite
158+
159+
$ sudo cp -r classification/ disk_mnt/root/tflite # copy the compiled tflite_classification demo and the other files to the image
160+
161+
$ sudo ls disk_mnt/root/tflite/classification # check the demo contents
162+
163+
$ sudo umount disk_mnt
164+
```
165+
### Run classification on HPI
166+
167+
We only show how to run classification on the HPI model. Reading results from stats.txt files can be done by using the read_results.sh script, as shown for the SE mode.
168+
169+
#### benchmark runscripts
170+
171+
[link](../scripts/run_classification.sh)
172+
173+
#### Run starter_fs using the expanded image and benchmark runscript
174+
175+
$ ./build/ARM/gem5.opt -d fs_results/<benchmark> configs/example/arm/starter_fs.py --cpu="hpi" --num-cores=1 --disk-image=$M5_PATH/disks/expanded-ubuntu-18.04-arm64-docker.img --root-device=/dev/vda1 --script=../scripts/run_classification.sh
56.7 KB
Loading

scripts/run_classification.sh

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

3-
cd /usr/share/tensorflow-lite/demos
3+
pushd /root/tflite/classification
44

55
echo -e "-----------------------------------------------------------------"
66
echo -e "Running classification using mobilenet_v1_1.0_224.tflite model..."
77
echo -e "-----------------------------------------------------------------"
88

99
# Run tflite_classification binary with the tensorflow lite model, the input image, and the label file specified.
10-
# Classification label is overlayed with the input image. Exit the application after 60 seconds.
11-
./tflite_classification -m ./mobilenet_v1_1.0_224.tflite -i ../examples/grace_hopper.bmp -l ../examples/labels.txt -p 1 & pid=$!
12-
{ sleep 2; kill $pid; }
1310

14-
echo -e
15-
echo -e "--------------------------------------------------------------------------------------------"
16-
echo -e "Running classification using the TIDL compiled model mobilenet_v1_1.0_224_tidl_am5.tflite..."
17-
echo -e "--------------------------------------------------------------------------------------------"
18-
# Run classification with the TIDL compiled tensorflow lite model
19-
# (e.g., mobilenet_v1_1.0_224_tidl_am5.tflite generated from run_tidl_compiler.sh)
20-
# TIDL API uses subgraph0.cfg created from run_tidl_compiler.sh to identify the TIDL network files,
21-
# as well as the boundary conversion parameters between TIDL and Arm
22-
# Classification label is overlayed with the input image. Right click the image display window to exit.
23-
cd /usr/share/tensorflow-lite/demos
24-
./tflite_classification -m /usr/share/ti/tidl/utils/test/testvecs/config/tflite_models/mobilenet_v1_1.0_224_tidl_am5.tflite \
25-
-i ../examples/grace_hopper.bmp -l ../examples/labels.txt -p 1
11+
set -v
12+
13+
./tflite_classification -m ./mobilenet_v1_1.0_224.tflite -i ./grace_hopper.bmp -l ./labels.txt -p 1 -c 2
14+
15+
m5 exit

scripts/run_segmentation.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)