Skip to content

Commit ef4dc66

Browse files
authored
Merge pull request #513 from foss-for-synopsys-dwc-arc-processors/emnist_inst_upd
[Examples] Update EMNIST tutorial instructions and fix build process
2 parents fbe2c05 + 1a78f9a commit ef4dc66

File tree

6 files changed

+434
-397
lines changed

6 files changed

+434
-397
lines changed

examples/tutorial_emnist_tflm/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if (ARC)
2828
-Hxcheck
2929
-Hcrt_initbss
3030
)
31+
get_filename_component(TCF_FILE_NAME ${ARC_CFG_TCF_PATH} NAME_WE)
3132
else()
3233
set(EXAMPLE_FLAGS)
3334
endif()
@@ -41,7 +42,7 @@ add_executable(${TARGET}
4142
./src/test_samples.cc
4243
)
4344

44-
target_link_libraries(${TARGET} PUBLIC $ENV{TENSORFLOW_DIR}/tensorflow/lite/micro/tools/make/downloads/arc_mli_$ENV{TCF_FILE_NAME}_mli20/bin/arc/libmli.a)
45+
target_link_libraries(${TARGET} PUBLIC $ENV{TENSORFLOW_DIR}/tensorflow/lite/micro/tools/make/downloads/arc_mli_${TCF_FILE_NAME}_mli20/bin/arc/libmli.a)
4546
target_link_libraries(${TARGET} PRIVATE tensorflow-microlite)
4647

4748
target_include_directories(${TARGET} PRIVATE

examples/tutorial_emnist_tflm/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ ifeq ($(TENSORFLOW_DIR),)
1313
$(error cannot find tensorflow -- TENSORFLOW_DIR variable must be set)
1414
endif
1515

16+
BUILD_TARGET = tutorial_emnist_tflm
1617
BUILD_SUBDIR = examples$(PS)tutorial_emnist_tflm
1718
BIN_PATH = $(BUILD_DIR)$(PS)$(BUILD_SUBDIR)$(PS)bin
1819

1920
ifndef TCF_FILE
2021
RUN_APP_CMD =
2122
else
22-
RUN_APP_CMD = mdb -cl -nsim -cmd=run -off=cr_for_more -cmd=exit -tcf=$(TCF_FILE)
23+
RUN_APP_CMD = mdb64 -cl -nsim -cmd=run -off=cr_for_more -cmd=exit -tcf=$(TCF_FILE)
2324
BIN_EXT = .elf
2425
endif
2526

26-
export TCF_FILE_NAME = $(basename $(notdir $(TCF_FILE)))
2727
app: build
2828

2929
run:
30-
$(RUN_APP_CMD) $(BIN_PATH)$(PS)tutorial_emnist_tflm$(BIN_EXT) $(RUN_ARGS)
30+
$(RUN_APP_CMD) $(BIN_PATH)$(PS)$(BUILD_TARGET)$(BIN_EXT) $(RUN_ARGS)
3131

3232
.PHONY: app
Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,94 @@
1-
EMNIST Conversion Example
2-
========================================================================
3-
This example shows how to convert EMNIST Tensorflow model into Tensorflow Lite Micro format and use it in embARC MLI application.
1+
# EMNIST Example: Tensorflow Lite Micro Usage
2+
3+
This example shows how to use Tensorflow Lite Micro together with an embARC MLI application. It consists of two parts:
4+
5+
1) Example application. It demonstrates how to use the tflite-micro API and guides you through the building aspects of the application and libraries.
6+
7+
2) Conversion Tutorial. An independent part showing how the NN model for the application was converted into tflite format and adapted to be MLI compatible.
8+
9+
The first part is disclosed in this readme. The details of the conversion tutorial are covered in the separate readme file in the [*conversion_tutorial*](/examples/tutorial_emnist_tflm/conversion_tutorial) directory. Passing the second part is not necessary to run the example application.
410

511
**Important notes:**
6-
* Example doesn’t work for VPX configurations without guard bits as it produces incorrect results due to accumulator overflow during calculations.
7-
* Example won't work with EM/HS platform. For EM/HS, please use [MLI 1.1 release version](https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_mli/tree/Release_1.1/examples/tutorial_emnist_tflm).
8-
9-
## Requirements
10-
---------------
11-
* MetaWare Development Tools
12-
* [Order the ARC MetaWare Development Toolkit](https://www.synopsys.com/dw/ipdir.php?ds=sw_metaware)
13-
* [Evaluation license](https://eval.synopsys.com/)
14-
* gmake (pre-installed as a part of MetaWare tools)
15-
* Text Editor
16-
* embARC MLI Library
17-
* `git clone https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_mli`
18-
* Tensorflow Lite for Microcontrollers
19-
* see the [Generate Tensorflow Lite Micro Library section](#generate-tensorflow-lite-micro-library).
20-
21-
22-
Installation process of the following dependencies is described in [Getting Started](#getting-started) section:
23-
* Python 3.7
24-
* virtualenv (optional)
25-
* Dependencies from requirements.txt
26-
* NumPy 1.19.2
27-
* Matplotlib
28-
* Jupyter Lab / Notebook
29-
* TensorFlow 2.5.0
30-
* Keras
31-
* emnist
32-
33-
## Getting started
34-
---------------
35-
36-
## Install Python and create a virtual environment
37-
38-
0. It is recommended that you uninstall your previous Python distribution.
39-
1. Download official [Python 3.7 distribution](https://www.python.org/ftp/python/3.7.4/python-3.7.4-amd64.exe).
40-
2. Install py launcher and pip. Do not add Python to the PATH. After this, the `py` command in command line is your entry point to Python interpreter.
41-
3. (optional) Install virtualenv with `py -m pip install --upgrade pip virtualenv`
42-
4. (optional) Create virtual environment with `py -m virtualenv py_env`.
43-
5. (optional) Activate virtual environment with `./py_env/Scripts/activate`.
44-
6. Execute ` cd ./embarc_mli/examples/tutorial_emnist_tflm`.
45-
46-
## Install pip requirements
47-
```bash
48-
pip install --upgrade pip setuptools
49-
pip install -r ./conversion_tutorial/requirements.txt
50-
python -c "import emnist; emnist.ensure_cached_data();"
51-
```
12+
13+
* Example is supported only for VPX configurations with guard bits. For EM/HS, please use [MLI 1.1 release version](https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_mli/tree/Release_1.1/examples/tutorial_emnist_tflm).
14+
5215
## Generate Tensorflow Lite Micro Library
53-
Tensorflow Lite for Microcontrollers is a separate project with specific set of requirements.
54-
Please first familiarize yourself with [TFLM ARC specific details](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/blob/main/tensorflow/lite/micro/tools/make/targets/arc/README.md) and make sure that your environment is set up appropriately.
5516

56-
Important information is listed inside [make tool section](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/tree/main/tensorflow/lite/micro/tools/make/targets/arc#make-tool) of the referred document.
57-
The main message is that native *nix environment is required to build the TFLM library.
58-
For Windows users there are no officially supported flow.
59-
You still may consider projects like [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) at your own risk.
17+
To build and run the example application at first you need to generate **tflite-micro** static library.
6018

61-
We recommend to use [embARC fork](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro) of Tensorflow Lite for Microcontrollers repository.
62-
The fork has been updating periodically from the [upstream repo](https://github.com/tensorflow/tflite-micro) using states that are stable in relation to ARC target:
19+
Tensorflow Lite for Microcontrollers is a separate project with specific set of requirements. We recommend to use [embARC fork](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro) of Tensorflow Lite for Microcontrollers repository:
6320

6421
git clone https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro.git
6522

23+
The fork has been updating periodically from the [upstream repo](https://github.com/tensorflow/tflite-micro) using states that are stable in relation to ARC target.
24+
25+
Please first familiarize yourself with [TFLM ARC specific details](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/blob/main/tensorflow/lite/micro/tools/make/targets/arc/README.md) and make sure that your environment is set up appropriately.
26+
27+
Important information is listed inside [make tool section](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/tree/main/tensorflow/lite/micro/tools/make/targets/arc#make-tool) of the referred document.
28+
The main message is that native *nix environment is required to build the TFLM library.
29+
For Windows users there are no officially supported flow.
30+
You still may consider projects like [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) at your own risk.
31+
32+
To build **tflite-micro** library please find the corresponding section in documentation specified for a [custom ARC platform](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/tree/main/tensorflow/lite/micro/tools/make/targets/arc#Custom-ARC-EMHSVPX-Platform). You need to copy the generated library to the *third_party* directory of this example and rename it to *libtensorflow-microlite.a* (see the same documentation on where the generated library can be found).
33+
34+
For the following example application build you should set ``TENSORFLOW_DIR``
35+
environment variable to point to the cloned tflite-micro repository:
36+
37+
# For Windows
38+
set TENSORFLOW_DIR=<your-path-to-tflite-micro>
39+
40+
# For Linux
41+
export TENSORFLOW_DIR=<your-path-to-tflite-micro>
42+
43+
## Building and Running Example
44+
45+
After you've passed ["Generate Tensorflow Lite Micro Library"](#generate-tensorflow-lite-micro-library) step
46+
you need to configure and build the library project for the desired VPX
47+
configuration. Please read the corresponding section on [building the package](/README.md#building-the-package). **Also make sure you didn't forget to set ``TENSORFLOW_DIR`` environment variable.**
48+
49+
Build artifacts of the application are stored in the `/obj/<project>/examples/tutorial_emnist_tflm` directory where `<project>` is defined according to your target platform.
50+
51+
After you've built and configured the whole library project, you can proceed with the following steps.
52+
You need to replace `<options>` placeholder in commands below with the same [build configuration options](/README.md#build-configuration-options) list you used for the library configuration and build.
53+
54+
1. Open command line in the root of the embARC MLI repo and change working directory to './examples/tutorial_emnist_tflm/'
55+
56+
cd ./examples/tutorial_emnist_tflm/
57+
58+
2. Clean previous build artifacts (optional).
59+
60+
gmake <options> clean
61+
62+
3. Build the example. This is an optional step as you may go to the next step which automatically invokes the build process.
63+
64+
gmake <options> build
65+
66+
4. Run the example
67+
68+
gmake <options> run
69+
70+
## ARC VPX Build Process Example
71+
72+
Assuming you've already built and copied *libtensorflow-microlite.a* and your environment satisfies all build requirements for ARC VPX platform, you can use the following script to build and run the application using the nSIM simulator.
73+
The first step is to open a command line and change working directory to the root of the embARC MLI repo.
74+
75+
1. Clean all previous artifacts for all platforms
76+
77+
gmake cleanall
78+
79+
1. Generate recommended TCF file for VPX
80+
81+
tcfgen -o ./hw/vpx5_integer_full.tcf -tcf=vpx5_integer_full -iccm_size=0x80000 -dccm_size=0x40000
82+
83+
1. Build project using generated TCF and appropriate built-in runtime library for it. Use multithreaded build process (4 threads):
84+
85+
gmake TCF_FILE=../../hw/vpx5_integer_full.tcf BUILDLIB_DIR=vpx5_integer_full JOBS=4 build
86+
87+
1. Change working directory and build the example:
88+
89+
cd ./examples/tutorial_emnist_tflm
90+
gmake TCF_FILE=../../hw/vpx5_integer_full.tcf BUILDLIB_DIR=vpx5_integer_full build
6691

67-
In your compatible environment open root directory of tflite-micro repo in terminal. Run:
68-
```bash
69-
make -f tensorflow/lite/micro/tools/make/Makefile\
70-
OPTIMIZED_KERNEL_DIR=arc_mli TARGET=arc_custom\
71-
TCF_FILE=<path_to_vpx_tcf_file>\
72-
ARC_TAGS=mli20_experimental microlite
73-
```
74-
Generated library *libtensorflow-microlite.a* can be found in *\{tensorflow-dir\}/tensorflow/lite/micro/tools/make/gen/\{target\}/lib*. Copy it to third_party directory of this example.
75-
76-
## Convert the model
77-
To convert the model, run the Jupyter Notebook:
78-
```bash
79-
jupyter notebook conversion_tutorial/model_conversion.ipynb
80-
```
81-
After completing the tutorial you should have model and test samples generated.
82-
83-
Please make sure you don't forget to use [Model Adaptation Tool](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/tree/arc_mli_20_temp/tensorflow/lite/micro/tools/make/targets/arc#model-adaptation-tool-experimental-feature) to convert `emnist_model_int8.tflite` you got before saving it as C array.
84-
85-
Copy *conversion_tutorial/generated/model.h* and *conversion_tutorial/generated/test_samples.cc* to *src* folder.
86-
87-
Before building you should set TENSORFLOW_DIR variable to point to your Tensorflow top folder. E.g. on Windows:
88-
```bash
89-
set TENSORFLOW_DIR=\{your-path-to-tflite-micro\}
90-
```
91-
92-
Now you can build the example using MetaWare Development Tools:
93-
```bash
94-
gmake app TCF_FILE=<path_to_vpx_tcf_file>
95-
```
96-
And run the example with DesignWare ARC nSIM simulator:
97-
```bash
98-
gmake run TCF_FILE=<path_to_vpx_tcf_file>
99-
```
92+
1. Run the example:
10093

94+
gmake TCF_FILE=../../hw/vpx5_integer_full.tcf BUILDLIB_DIR=vpx5_integer_full run
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Model Conversion Tutorial for EMNIST Example
2+
3+
This tutorial shows how to convert EMNIST Tensorflow model into Tensorflow Lite Micro format for further usage in application together with embARC MLI.
4+
5+
## Requirements
6+
7+
The following dependencies must be installed in your environment:
8+
9+
* Python 3.7
10+
* Dependencies from the [requirements.txt](/examples/tutorial_emnist_tflm/conversion_tutorial/requirements.txt) file
11+
* NumPy 1.19.2
12+
* Matplotlib
13+
* Jupyter Lab / Notebook
14+
* TensorFlow 2.5.1
15+
* Keras
16+
* emnist
17+
18+
If you are looking for instruction to set-up a compatible environment, then consider the comprehensive guide on [TensorFlow](https://www.tensorflow.org/install) installation. We recommend to follow ["Install TensorFlow with pip"](https://www.tensorflow.org/install/pip) and not ignore recommendation on usage of the virtual environment.
19+
20+
Make sure you have installed required version of dependencies listed in the *requirements.txt* file. In case you followed our recommendation on install TensorFlow with pip, for [step 3](https://www.tensorflow.org/install/pip#3.-install-the-tensorflow-pip-package) you can use the following commands instead the proposed ones. First change working directory in terminal to the [conversion_tutorial](/examples/tutorial_emnist_tflm/conversion_tutorial).
21+
22+
pip3 install -r ./requirements.txt
23+
python3 -c "import emnist; emnist.ensure_cached_data();"
24+
25+
## Running Conversion Tutorial
26+
27+
To convert the model, run the Jupyter Notebook (change working directory in terminal to the [conversion_tutorial](/examples/tutorial_emnist_tflm/conversion_tutorial) if you haven't done it):
28+
29+
jupyter notebook model_conversion.ipynb
30+
31+
After completing the tutorial, you should have model and test samples generated.
32+
33+
Please make sure you don't forget to use [Model Adaptation Tool](https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro/blob/main/tensorflow/lite/micro/tools/make/targets/arc/adaptation_tool.py) to convert `emnist_model_int8.tflite` you got before saving it as C array.
34+
35+
## Using Converted Model in Application
36+
37+
See [EMNIST example part](/examples/tutorial_emnist_tflm) on how a converted model might be used in application together with TFLM and embARC MLI. The example already contains converted model, hence this step is optional.
38+
39+
To use generated models in the example you need to copy */examples/tutorial_emnist_tflm/conversion_tutorial/generated/model.h* and */examples/tutorial_emnist_tflm/conversion_tutorial/generated/test_samples.cc* to */examples/tutorial_emnist_tflm/src* folder. Make sure that names of buffers and constants in copied files are aligned with the former ones and change them if not. Afterward you can clean, build and run example application according to instructions.

0 commit comments

Comments
 (0)