[TOC]

## 1. 环境搭建

### 1.1 启动docker

```shell
$ cd /path/to/k230_sdk
$ docker run -u root -it --rm -v $(pwd):/mnt -v $(pwd)/toolchain:/opt/toolchain -w /mnt k230_docker /bin/bash
```

默认挂载了两个目录

- 将k230_sdk目录挂载到docker的/mnt
- 将交叉编译工具链k230_sdk/toolchain目录挂载到docker的/opt/toolchain



### 1.2 安装wheel包

- **linux平台**支持在线安装nncase和nncase-kpu
- **windows平台**只支持nncase在线安装， nncase-kpu需要到[nncase github release](https://github.com/kendryte/nncase/releases)单独下载

```shell
root@c08eb1760d50:/mnt# pip install -i https://pypi.org/simple nncase nncase-kpu
```



查看安装的nncase版本信息

```shell
root@c08eb1760d50:/mnt/# pip3 list|grep nncase
nncase            2.3.0
nncase-kpu        2.3.0

root@c08eb1760d50:/mnt/# python3
Python 3.8.10 (default, May 26 2023, 14:05:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _nncase
>>> print(_nncase.__version__)
2.3.0-e325238
>>> quit()
```



## 2. 编译模型

### 2.1 编译

```shell
root@c08eb1760d50:/mnt/# cd src/big/nncase/examples/
root@c08eb1760d50:/mnt/src/big/nncase/examples# ./build_model.sh
```



### 2.2 查看编译结果

```shell
root@c08eb1760d50:/mnt/src/big/nncase/examples# ls -l tmp/
total 12
drwxr-xr-x 11 root root 4096 Aug 28 14:51 mbv2_tflite
drwxr-xr-x 11 root root 4096 Aug 28 14:52 mobile_retinaface
drwxr-xr-x 11 root root 4096 Aug 28 14:51 yolov5s_onnx
```



## 3. 编译App

###  3.1 App介绍

| App               | 备注                                                         |
| ----------------- | ------------------------------------------------------------ |
| image_classify    | 图片分类demo, 输入是RGB图片, 推理结果打印到串口              |
| object_detect     | 目标检测demo, 输入是RGB图片, 推理结果打印到串口              |
| image_face_detect | 人脸检测demo, 输入是RGB图片, 推理结果会输出到画了人脸box和landmark的图片 |



### 3.2 编译

```shell
root@c08eb1760d50:/mnt/src/big/nncase/examples# ./build_app.sh
```



### 3.3 查看编译结果

编译app结束后, 默认会将demo及其运行所需文件拷贝到当前目录下的k230_bin子目录

```shell
root@8c6c27a92c6c:/mnt/src/big/nncase/examples# ls -l k230_bin/
total 12
drwxrwxr-x 2 1000 1000 4096 Aug 28 14:53 image_classify
drwxrwxr-x 2 1000 1000 4096 Aug 28 14:53 image_face_detect
drwxrwxr-x 2 1000 1000 4096 Aug 28 14:53 object_detect
```



### 3.4 传送demo

- 将k230_bin目录拷贝到本地的nfsroot目录
- 在小核串口搭建sharefs环境, 并将本地nfsroot目录mount到/sharefs
- 在大核串口下进入/sharefs执行相应demo



## 4. 上板运行

**前置条件**

- gnne频率: 800MHZ

- noc限速: 3.2GB/s


### 4.1 图片分类demo

```shell
msh /sharefs/k230/nncase_k230_v2.3.0/image_classify>./cpp.sh
case ./image_classify.elf built at Aug 28 2023 14:53:18
interp.run() took: 2.182 ms
image classify result: tabby(0.453891)
```



### 4.2 目标检测demo

```shell
msh /sharefs/k230/nncase_k230_v2.3.0/object_detect>./cpp.sh
case ./object_detect.elf built at Aug 28 2023 14:53:18
od set_input took 0.172926 ms
od run took 16.4906 ms
od get output took 12.8104 ms
post process took 57.7382 ms
text = truck:0.300000
text = dog:0.250000
text = bicycle:0.230000
draw result took 30.8547 ms
```

推理结果(画框)会生成到od_result.jpg



### 4.3 人脸检测demo

```shell
msh /sharefs/k230/nncase_k230_v2.3.0/image_face_detect>./cpp.sh
case ./image_face_detect.elf built at Aug 28 2023 14:53:18
Press 'q + enter' to exit!!!
g
```

推理结果(人脸box和landmark)会生成到face_500x500_result_x.jpg

