忘れそうな内容をメモしています

忘れっぽいのでメモ帳がわりに色々書いてます

nncaseのiris分類のサンプルコードをmaix bitで動かしてみた

以前、Maixbitで音の分類モデルを作ろうとkmodel形式のモデルまで作成しましたが、 maixpyのkpu.forwardが画像データ以外に対応していないようです。 shintarof.hatenablog.com

調べてみるとnncaseにIris分類のサンプルコードがあり、それが参考になりそうだったのでmaix bitで動かしてみました。 github.com

以下手順です。ちなみに環境はDebian10.4です。

1. kendryte-gnu-toolchainのビルド

riscv-gnu-toolchainのreadmeを元に以下手順でビルドしました。

$git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
$sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
$./configure --prefix=/opt/kendryte-toolchain --with-cmodel=medany --with-arch=rv64imafc --with-abi=lp64f
$sudo make -j8
2. iris分類のサンプルコードのビルド
$git clone https://github.com/kendryte/kendryte-standalone-sdk
$git clone https://github.com/kendryte/nncase
$cd kendryte-standalone-sdk/
$cp -r ../nncase/examples/iris ../src
$mkdir build
$cd build
$cmake ../ -DPROJ=iris -DTOOLCHAIN=/opt/kendryte-toolchain/bin
$ sudo make
Scanning dependencies of target kendryte
[  2%] Building C object lib/CMakeFiles/kendryte.dir/bsp/crt.S.obj
[  5%] Building C object lib/CMakeFiles/kendryte.dir/bsp/entry.c.obj
[  8%] Building C object lib/CMakeFiles/kendryte.dir/bsp/entry_user.c.obj
[ 11%] Building C object lib/CMakeFiles/kendryte.dir/bsp/interrupt.c.obj
[ 14%] Building C object lib/CMakeFiles/kendryte.dir/bsp/locks.c.obj
[ 17%] Building C object lib/CMakeFiles/kendryte.dir/bsp/printf.c.obj
[ 20%] Building C object lib/CMakeFiles/kendryte.dir/bsp/sleep.c.obj
[ 22%] Building C object lib/CMakeFiles/kendryte.dir/bsp/syscalls.c.obj
[ 25%] Building C object lib/CMakeFiles/kendryte.dir/drivers/aes.c.obj
[ 28%] Building C object lib/CMakeFiles/kendryte.dir/drivers/apu.c.obj
[ 31%] Building C object lib/CMakeFiles/kendryte.dir/drivers/clint.c.obj
[ 34%] Building C object lib/CMakeFiles/kendryte.dir/drivers/dmac.c.obj
[ 37%] Building C object lib/CMakeFiles/kendryte.dir/drivers/dvp.c.obj
[ 40%] Building C object lib/CMakeFiles/kendryte.dir/drivers/fft.c.obj
[ 42%] Building C object lib/CMakeFiles/kendryte.dir/drivers/fpioa.c.obj
[ 45%] Building C object lib/CMakeFiles/kendryte.dir/drivers/gpio.c.obj
[ 48%] Building C object lib/CMakeFiles/kendryte.dir/drivers/gpiohs.c.obj
[ 51%] Building C object lib/CMakeFiles/kendryte.dir/drivers/i2c.c.obj
[ 54%] Building C object lib/CMakeFiles/kendryte.dir/drivers/i2s.c.obj
[ 57%] Building C object lib/CMakeFiles/kendryte.dir/drivers/kpu.c.obj
[ 60%] Building C object lib/CMakeFiles/kendryte.dir/drivers/plic.c.obj
[ 62%] Building C object lib/CMakeFiles/kendryte.dir/drivers/pwm.c.obj
[ 65%] Building C object lib/CMakeFiles/kendryte.dir/drivers/rtc.c.obj
[ 68%] Building C object lib/CMakeFiles/kendryte.dir/drivers/sha256.c.obj
[ 71%] Building C object lib/CMakeFiles/kendryte.dir/drivers/spi.c.obj
[ 74%] Building C object lib/CMakeFiles/kendryte.dir/drivers/sysctl.c.obj
[ 77%] Building C object lib/CMakeFiles/kendryte.dir/drivers/timer.c.obj
[ 80%] Building C object lib/CMakeFiles/kendryte.dir/drivers/uart.c.obj
[ 82%] Building C object lib/CMakeFiles/kendryte.dir/drivers/uarths.c.obj
[ 85%] Building C object lib/CMakeFiles/kendryte.dir/drivers/utils.c.obj
[ 88%] Building C object lib/CMakeFiles/kendryte.dir/drivers/wdt.c.obj
[ 91%] Linking C static library libkendryte.a
[ 91%] Built target kendryte
[ 94%] Linking C executable iris
Generating .bin file ...
[100%] Built target iris
3. maix bitへの書き込み

ビルドした実行ファイル(iris.bin)を書き込みます。

$kflash iris.bin -p /dev/ttyUSB0
[INFO] COM Port Selected Manually:  /dev/ttyUSB0 
[INFO] Default baudrate is 115200 , later it may be changed to the value you set. 
[INFO] Trying to Enter the ISP Mode... 
._
[INFO] Automatically detected goE/kd233 

[INFO] Greeting Message Detected, Start Downloading ISP 
Downloading ISP: |===================================================================================================================| 100.0% 10kiB/s
[INFO] Booting From 0x80000000 
[INFO] Wait For 0.1 second for ISP to Boot 
[INFO] Boot to Flashmode Successfully 
[INFO] Selected Flash:  On-Board 
[INFO] Initialization flash Successfully 
Programming BIN: |===================================================================================================================| 100.0% 10kiB/s
[INFO] Rebooting... 

次にモデルもkflashで書き込みます。
モデルはnncase/examples/iris/k210/kpu_iris_example/kfpkg/kfpkg.kfpkg にありました。

$kflash ../../c/nncase/examples/iris/k210/kpu_iris_example/kfpkg/kfpkg.kfpkg  -p /dev/ttyUSB0
[INFO] COM Port Selected Manually:  /dev/ttyUSB0 
[INFO] Default baudrate is 115200 , later it may be changed to the value you set. 
[INFO] Trying to Enter the ISP Mode... 
._
[INFO] Automatically detected goE/kd233 

[INFO] Greeting Message Detected, Start Downloading ISP 
Downloading ISP: |===================================================================================================================| 100.0% 10kiB/s
[INFO] Booting From 0x80000000 
[INFO] Wait For 0.1 second for ISP to Boot 
[INFO] Boot to Flashmode Successfully 
[INFO] Selected Flash:  On-Board 
[INFO] Initialization flash Successfully 
[INFO] Extracting KFPKG ...  
[INFO] Writing kpu_iris_example.bin into 0x00000000 
Programming BIN: |===================================================================================================================| 100.0% 10kiB/s
[INFO] Writing iris.kmodel into 0x00c00000 
Programming BIN: |===================================================================================================================| 100.0% 10kiB/s
[INFO] Rebooting... 
4.実行

cuで接続した状態で、maixbitのresetボタン(写真の右上の白いボタン)を押すと以下のように判定結果が出力されました。

$ cu -s 115200 -l /dev/ttyUSB0 
Connected.
flash init

          16.077793, 5.607656, -2.071745, 
                                          setosa

f:id:shintarof:20200822132548j:plain

とりあえずサンプルコードを動かせたので、次はこのコードを参考に音分類のモデルを作ってみようと思います。