Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:edge2:npu:demos:rknn-tflite

TFLite Edge2 Demo - 8

We can run TFLite models by converting them into RKNN format and running them on the onboard NPU.

Get Source code

Clone the examples sravansenthiln1/rknn_tflite.

$ git clone https://github.com/sravansenthiln1/rknn_tflite
$ cd rknn_tflite

RKNN Conversion

You can convert TFLite models to run the NPU using the convert.py conversion script from the source.

Requires: Ubuntu 22.04/20.04/18.04 x86 Host computer.

After you have cloned the source code:

Get the necessary system packages

$ sudo apt-get install git python3 python3-dev python3-pip
$ sudo apt-get install libxslt1-dev zlib1g-dev libglib2.0 libsm6 libgl1-mesa-glx libprotobuf-dev gcc cmake

Clone the conversion tools

$ git clone https://github.com/rockchip-linux/rknn-toolkit2
$ cd rknn-toolkit2
$ git checkout b25dadacc24b88eb7dfcaa47c9c525ecca89b319

At this point of time, you can also create a virtual environment to store all the packages you need. This will keep your system packages clean and not disturb their package versions. for this you need to install Conda

$ conda create -n npu-env
$ conda activate npu-env

whenever you need to convert the models, you need to activate this env.

Find the appropriate python version

$ python3 --version

and run the command accordingly

python version command
3.11 version=cp311
3.10 version=cp310
3.9 version=cp39
3.8 version=cp38
3.7 version=cp37
3.6 version=cp36

Install the requirements

$ pip3 install -r rknn-toolkit2/packages/requirements_$version-*.txt

Install the appropriate toolkit wheel

$ pip3 install rknn-toolkit2/packages/rknn_toolkit2-*-$version-$version-linux_x86_64.whl
$ cd ../

Try using the conversion tool

$ python3 convert.py

eg. to convert a file such as detect_model.tflite, run

$ python3 convert.py detect_model

in the same directory, a file called detect_model.rknn will have been created.

RKNN Deployment

To run it on your board, you need to install appropriate RKNN API wheel

After cloning the source code:

Install pip

$ sudo apt-get install python3-pip

Install necessary python packages

$ pip3 install numpy pillow opencv-python librosa sounddevice

clone the toolkit

$ git clone https://github.com/rockchip-linux/rknn-toolkit2
$ cd rknn-toolkit2
$ git checkout b25dadacc24b88eb7dfcaa47c9c525ecca89b319

Find the system python version

$ python3 --version

and run the command accordingly

python version command
3.11 version=cp311
3.10 version=cp310
3.9 version=cp39
3.8 version=cp38
3.7 version=cp37
3.6 version=cp36

Install the appropriate toolkit wheel

$ pip3 install rknn_toolkit_lite2/packages/rknn_toolkit_lite2-*-$version-$version-linux_aarch64.whl

Copy the runtime library

$ sudo cp rknpu2/runtime/Linux/librknn_api/aarch64/librknnrt.so /usr/lib/
$ cd ../

Run Examples

Taking the Mobilenet v1 as example.

Enter the example directory

$ cd mobilenet_v1

Run the example

$ python3 run_npu_inference.py
Last modified: 2024/05/23 03:01 by sravan