What is ksnn?
Mean
The meaning of ksnn is Khadas Software Neural Network.
Why do this ?
The original SDK only has a C/C++ interface. Since its release in 2019, users have been asking whether it will support python api. Python is the most widely used programming tool in the AI field, so we decided to make a python version of the api to meet the needs of users for the python version.
v0.1
v0.1 is a test version. We hope that more users will participate and give us more feedback. In the future, we will release the official version after improving the API.
Instruction
Dependence
- For some special reasons, the current python api still relies on the nb files and case codes converted from the original SDK.
- The dependent python packages, in addition to the core packages, mainly include opencv and numpy.
Limit
- The current version only supports single input, and multiple inputs will be supported in the future
- Currently does not support hybrid quantization
- Like the original SDK, only the tensor and layer supported by the SDK can be used
How To Use
SDK
Get SDK
$ git clone https://gitlab.com/khadas/aml_npu_sdk.git
Use SDK to convert
The original model cannot directly call npu and needs to use the sdk conversion model. Here, taking mobilenet as an example, use the python conversion tool to obtain the required nb files and libraries.
$ cd acuity-toolkit/python/
$ ./convert --model-name mobilenet_v1 \
--convert-platform tensorflow \
--tf-inputs input --tf-input-size-list '224,224,3' \
--tf-outputs MobilenetV1/Predictions/Softmax \
--tf-model-file ../demo/model/mobilenet_v1.pb \
--source-file-path ../demo/data/validation_tf.txt \
--channel-mean-value '128 128 128 128' \
--quantized-dtype asymmetric_affine-u8 \
--reorder-channel '0 1 2' \
--kboard VIM3
Get the converted data
$ ls outputs/mobilenet_v1/
libnn_network.so mobilenet_v1.nb
Copy the data to the board.
ksnn package
Get
$ wget http://dl.khadas.com/development/ksnn-0.1-py3-none-linux_aarch64.whl
The test version of the wheel is placed on the khadas server.
Install
Only adapt to python3 here. You need to install Python3-pip
online before installing wheel
$ sudo apt install python3-pip
After the installation is successful, install the wheel,
$ pip3 install ksnn-0.1-py3-none-linux_aarch64.whl
Both opencv and numpy will be installed here.
verify
$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ksnn
>>>
The normal import means that the installation is successful.
Test examples
Get code
$ git clone https://gitlab.com/khadas/ksnn-examples.git
Run
Install dependence
$ pip3 install matplotlib
Run the inceptionv3 test example,
$ cd ksnn-examples/tensorflow/
khadas@Khadas:~/ksnn-example/tensorflow$ python3 inceptionv3.py --nb-file models/VIM3/inceptionv3.nb --input-picture data/1080p.bmp --so-lib libs/libnn_inceptionv3.so
|---+ KSNN Version: v0.1 +---|
Create Neural Network: 46ms or 46580us
set input time : 0.004050493240356445
Start run graph [1] times...
Run the 1 time: 20.00ms or 20854.00us
vxProcessGraph execution time:
Total 20.00ms or 20906.00us
Average 20.91ms or 20906.00us
get ouput time: 0.0007474422454833984
-----+ Show Top5 +-----
904: 0.41821
656: 0.11829
446: 0.09064
639: 0.05692
825: 0.03821
To run the yolov3 test code, you can refer to README
.
Docs
API documentation location:
More
- Any feedback and suggestions for testing are welcome, you can feed back in this post, or:
-
Each platform will add examples in the future.
-
In the future, the entire ksnn package will be open sourced, but it will not be open for the time being in the testing phase.