Skip to content

Commit

Permalink
1. add k210 pipeline docs in USAGE.md
Browse files Browse the repository at this point in the history
2. add simulate.ipynb for compile and inference
  • Loading branch information
yanghaoqi committed Jul 26, 2023
1 parent b57f35b commit 1d71270
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 7 deletions.
52 changes: 48 additions & 4 deletions docs/USAGE_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,54 @@ if __name__ == '__main__':

## Deploy nncase runtime

### K210

1. Download `k210-runtime.zip` from [Release](https://github.com/kendryte/nncase/releases) page.
2. Unzip to your [kendryte-standalone-sdk](https://github.com/kendryte/kendryte-standalone-sdk) 's `lib/nncase/v1` directory.
### Inference on K210 development board

1. Download [SDK](https://github.com/kendryte/kendryte-standalone-sdk)

```shell
$ git clone https://github.com/kendryte/kendryte-standalone-sdk.git
$ cd kendryte-standalone-sdk
$ export KENDRYTE_WORKSPACE=`pwd`
```

2. Download the cross-compile toolchain and extract it

```shell
$ wget https://github.com/kendryte/kendryte-gnu-toolchain/releases/download/v8.2.0-20190409/kendryte-toolchain-ubuntu-amd64-8.2.0-20190409.tar.xz -O $KENDRYTE_WORKSPACE/kendryte-toolchain.tar.xz
$ cd $KENDRYTE_WORKSPACE
$ mkdir toolchain
$ tar -xf kendryte-toolchain.tar.xz -C ./toolchain
```

3. Update nncase runtime

Download `k210-runtime.zip` from [Release](https://github.com/kendryte/nncase/releases) and extract it into [kendryte-standalone-sdk](https://github.com/kendryte/kendryte-standalone-sdk) 's `lib/nncase/v1`.

4. Compile App

```shell
# 1.copy your programe into `$KENDRYTE_WORKSPACE/src`
# e.g. copy ($NNCASE_WORK_DIR/examples/facedetect_landmark/k210/facedetect_landmark_example) into PATH_TO_SDK/src.
$ cp -r $NNCASE_WORK_DIR/examples/facedetect_landmark/k210/facedetect_landmark_example $KENDRYTE_WORKSPACE/src/

# 2. compile
$ cd $KENDRYTE_WORKSPACE
$ mkdir build
$ cmake .. -DPROJ=facedetect_landmark_example -DTOOLCHAIN=$KENDRYTE_WORKSPACE/toolchain/kendryte-toolchain/bin && make
```

`facedetect_landmark_example` and`FaceDETECt_landmark_example.bin` will be generated.

5. Write the program to the K210 development board

```shell
# 1. Check available USB ports
$ ls /dev/ttyUSB*
# /dev/ttyUSB0 /dev/ttyUSB1

# 2. Write your App by kflash
$ kflash -p /dev/ttyUSB0 -t facedetect_landmark_example.bin
```

## nncase inference APIs

Expand Down
53 changes: 50 additions & 3 deletions docs/USAGE_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,57 @@ if __name__ == '__main__':

## 部署 nncase runtime

### K210
### K210上板推理流程

1. 下载官方[SDK](https://github.com/kendryte/kendryte-standalone-sdk)

```shell
git clone https://github.com/kendryte/kendryte-standalone-sdk.git
cd kendryte-standalone-sdk
export KENDRYTE_WORKSPACE=`pwd`
```

2. 下载交叉编译工具链,并解压

```shell
wget https://github.com/kendryte/kendryte-gnu-toolchain/releases/download/v8.2.0-20190409/kendryte-toolchain-ubuntu-amd64-8.2.0-20190409.tar.xz -O $KENDRYTE_WORKSPACE/kendryte-toolchain.tar.xz
cd $KENDRYTE_WORKSPACE
mkdir toolchain
tar -xf kendryte-toolchain.tar.xz -C ./toolchain
```

3. 更新runtime

从 [Release](https://github.com/kendryte/nncase/releases) 页面下载 `k210-runtime.zip`。解压到 [kendryte-standalone-sdk](https://github.com/kendryte/kendryte-standalone-sdk) 's `lib/nncase/v1` 目录。

4. 编译App

````shell
# 1.将自己的App工程放在`$KENDRYTE_WORKSPACE/src`目录下
# 例如,将[example的示例程序]($NNCASE_WORK_DIR/examples/facedetect_landmark/k210/facedetect_landmark_example)目录,拷贝到SDK的src目录下。
cp -r $NNCASE_WORK_DIR/examples/facedetect_landmark/k210/facedetect_landmark_example $KENDRYTE_WORKSPACE/src/

# 2.cmake 编译App
cd $KENDRYTE_WORKSPACE
mkdir build
cmake .. -DPROJ=facedetect_landmark_example -DTOOLCHAIN=$KENDRYTE_WORKSPACE/toolchain/kendryte-toolchain/bin && make
````

之后会在当前目录下生成`facedetect_landmark_example``facedetect_landmark_example.bin`

5. 烧写App

```shell
# 1. 检查可用的USB端口
ls /dev/ttyUSB*
# > /dev/ttyUSB0 /dev/ttyUSB1
# 2. 使用kflash进行烧录
kflash -p /dev/ttyUSB0 -t facedetect_landmark_example.bin
```

烧写过程缓慢,需要耐心等待。


1. 从 [Release](https://github.com/kendryte/nncase/releases) 页面下载 `k210-runtime.zip`
2. 解压到 [kendryte-standalone-sdk](https://github.com/kendryte/kendryte-standalone-sdk) 's `lib/nncase/v1` 目录。

## nncase 推理模型APIs

Expand Down
201 changes: 201 additions & 0 deletions examples/user_guide/k210_simulate.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "82a8f9c1-c2bf-4270-9f1f-ac25c9fdd898",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (23.2.1)\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0mCollecting nncase==1.9.0.20230322\n",
" Downloading nncase-1.9.0.20230322-cp38-cp38-manylinux_2_24_x86_64.whl (10.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.0/10.0 MB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m0m\n",
"\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (from nncase==1.9.0.20230322) (1.23.5)\n",
"Installing collected packages: nncase\n",
" Attempting uninstall: nncase\n",
" Found existing installation: nncase 2.1.0.20230703\n",
" Uninstalling nncase-2.1.0.20230703:\n",
" Successfully uninstalled nncase-2.1.0.20230703\n",
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"nncase-kpu 2.1.0.20230703 requires nncase>=2.1, but you have nncase 1.9.0.20230322 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0mSuccessfully installed nncase-1.9.0.20230322\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install --upgrade pip\n",
"# !pip uninstall -y nncase\n",
"# !pip install nncase==1.8.0.20220929 --timeout=1000\n",
"!pip install nncase==1.9.0.20230322 --timeout=1000\n",
"#from versions: 1.0.0.20211029, 1.1.0.20211203, 1.3.0.20220127, 1.4.0.20220303, 1.5.0.20220331, \n",
"# 1.6.0.20220505, 1.7.0.20220530, 1.7.1.20220701, 1.8.0.20220929, 1.9.0.20230322, 2.0.0.20230602, 2.1.0.20230703)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "44f95a15-936d-46f7-8740-9f7432a3231c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: PATH=$PATH:/usr/local/lib/python3.8/site-packages\n"
]
}
],
"source": [
"# export nncase lib path into PATH\n",
"%env PATH=$PATH:/usr/local/lib/python3.8/site-packages"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a7eff82e-295c-4cce-afbc-ce64c84dc40a",
"metadata": {},
"outputs": [],
"source": [
"import nncase\n",
"from nncase_base_func import *\n",
"# from parse_model import *\n",
"\n",
"\n",
"def compile_kmodel(model_path, dump_path, calib_data):\n",
" \"\"\"\n",
" Set compile options and ptq options.\n",
" Compile kmodel.\n",
" Dump the compile-time result to 'compile_options.dump_dir'\n",
" \"\"\"\n",
" print(\"----------model simplify----------\")\n",
" model_file = model_simplify(model_path)\n",
"\n",
" print(\"---------- set options ----------\")\n",
" # import_options\n",
" import_options = nncase.ImportOptions()\n",
" \n",
" # compile_options\n",
" compile_options = nncase.CompileOptions()\n",
" compile_options.target = \"k210\" # \"cpu\"\n",
" compile_options.dump_ir = True # if False, will not dump the compile-time result.\n",
" compile_options.dump_asm = True\n",
" compile_options.dump_dir = dump_path\n",
"\n",
" # preprocess args\n",
" compile_options.preprocess = True\n",
" if compile_options.preprocess:\n",
" compile_options.input_type = \"uint8\" # \"uint8\"\n",
" compile_options.swapRB = False\n",
" compile_options.input_shape = [1,224,320,3]\n",
" compile_options.input_range = [0,1]\n",
" compile_options.mean = [0,0,0]\n",
" compile_options.std = [1,1,1]\n",
" compile_options.input_layout = \"NHWC\" # \"NHWC\"\n",
" compile_options.output_layout = \"NHWC\" # \"NHWC\"\n",
" \n",
" # quant args\n",
" compile_options.quant_type = \"uint8\" \n",
" compile_options.w_quant_type = \"uint8\"\n",
" compile_options.use_mse_quant_w = True\n",
" compile_options.split_w_to_act = False\n",
"\n",
" # quant options\n",
" ptq_options = nncase.PTQTensorOptions()\n",
" ptq_options.calibrate_method = \"no_clip\" # \"kld_m2\" \"l2\" \"cdf\"\n",
" ptq_options.samples_count = 80\n",
" # print(len(calib_data[0]))\n",
" # ptq_options.set_tensor_data(np.array(calib_data).tobytes())\n",
" ptq_options.set_tensor_data(calib_data[:].tobytes())\n",
"\n",
" \n",
" # set options\n",
" compiler = nncase.Compiler(compile_options)\n",
" compiler.use_ptq(ptq_options)\n",
" \n",
" print(\"---------- compile ----------\")\n",
" # import\n",
" model_content = read_model_file(model_file)\n",
" if model_path.split(\".\")[-1] == \"onnx\":\n",
" compiler.import_onnx(model_content, import_options)\n",
" elif model_path.split(\".\")[-1] == \"tflite\":\n",
" compiler.import_tflite(model_content, import_options)\n",
"\n",
" # compile\n",
" compiler.compile()\n",
" kmodel = compiler.gencode_tobytes()\n",
" \n",
" kmodel_path = os.path.join(dump_path, \"test.kmodel\")\n",
" with open(kmodel_path, 'wb') as f:\n",
" f.write(kmodel)\n",
" print(\"---------- compile end ----------\")\n",
" return kmodel_path\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c957fe20-99c9-4a54-bae8-38361a8f8830",
"metadata": {},
"outputs": [],
"source": [
"# compile kmodel\n",
"model_path = \"./model_f32.tflite\"\n",
"dump_path = \"./tmp_tflite\"\n",
"\n",
"# If model has multi inputs, calib_data format is \"[[x1, x2,...], [y1, y2,...], ...]\"\n",
"# e.g. Model has three inputs (x, y, z), the calib_data is '[[x1, x2, x3],[y1, y2, y3],[z1, z2, z3]]'\n",
"\n",
"calib_data = [[np.random.rand(1,224,320,3).astype(np.float32), np.random.rand(1,224,320,3).astype(np.float32)]]\n",
"# calib_data = np.load(\"./qual_data.npy\")\n",
"# print(calib_data.shape)\n",
"kmodel_path = compile_kmodel(model_path, dump_path, calib_data)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f617edc-781c-4b8b-b45d-fef2f0b36a46",
"metadata": {},
"outputs": [],
"source": [
"# run kmodel(simulate)\n",
"kmodel_path = \"./tmp_tflite/test.kmodel\"\n",
"# input_data = [np.random.rand(1,224,320,3).astype(np.float32)]\n",
"input_data = [np.load(\"./qual_data.npy\")[:1]]\n",
"\n",
"result = run_kmodel(kmodel_path, input_data)\n",
"for idx, i in enumerate(result):\n",
" print(i.shape)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 1d71270

Please sign in to comment.