|
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. |
4 | 10 |
|
5 | 11 | **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 | + |
52 | 15 | ## 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. |
55 | 16 |
|
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. |
60 | 18 |
|
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: |
63 | 20 |
|
64 | 21 | git clone https://github.com/foss-for-synopsys-dwc-arc-processors/tflite-micro.git |
65 | 22 |
|
| 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 |
66 | 91 |
|
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: |
100 | 93 |
|
| 94 | + gmake TCF_FILE=../../hw/vpx5_integer_full.tcf BUILDLIB_DIR=vpx5_integer_full run |
0 commit comments