kendryte / nncase

Open deep learning compiler stack for Kendryte AI accelerators ✨
Apache License 2.0
752 stars 183 forks source link

How to compile K210model with kendryte-standalone-sdk #5

Closed xhsonny closed 5 years ago

xhsonny commented 5 years ago

With previous nncase, I had a workflow to use it to convert model to k210code and compile kendryte-standalone-sdk. Now nncase only ouputs k210modeo. What is the workflow to build a bin file?

sunnycase commented 5 years ago

What's the bin file you mean?

xhsonny commented 5 years ago

Thanks for the quick reply.

https://github.com/kendryte/kendryte-standalone-sdk/blob/develop/README.md like hello_world.bin that is built by sdk.

xhsonny commented 5 years ago

Another example is https://github.com/kendryte/tensorflow-workspace/tree/master/K210code. It has a mobilenetv1.c which is k210code format and sdk will build a .bin file from it.

sunnycase commented 5 years ago

You can write the .kmodel to the flash and read it to memory in your program. Or you can use INCBIN to embed the .kmodel in your code.

xhsonny commented 5 years ago

Could you point me to an example how to load .kmodel in C code? And an example for INCBIN as well. Thanks!

sunnycase commented 5 years ago

For loading from flash: https://github.com/kendryte/kendryte-standalone-demo/blob/develop/face_detect_kmodel_flash/main.c

incbin.h: https://github.com/kendryte/kendryte-standalone-demo/blob/develop/face_detect_kmodel_flash/incbin.h Usage:

#define INCBIN_STYLE INCBIN_STYLE_SNAKE
#define INCBIN_PREFIX
#include "incbin.h"

INCBIN(model, "../src/yourproject/model.kmodel");

kpu_model_load_from_buffer(&task, model_data, NULL);
xhsonny commented 5 years ago

After kpu_model_load_from_buffer(&task, model_data, NULL);, task is initialized the same as kpu_task_mobilenetv1_init(&task); (https://github.com/kendryte/tensorflow-workspace/blob/master/K210code/main.c#L84), right?

xhsonny commented 5 years ago

Hmm, another question. I am using the mobilenet_v1 model you guys released and the last layer is computed by manually calling kpu_global_average_pool (https://github.com/kendryte/tensorflow-workspace/blob/master/K210code/main.c#L59) because of unsupported 7x7 pooling. In this case, the code needs scale and bias like https://github.com/kendryte/tensorflow-workspace/blob/master/K210code/main.c#L58. How can I get these numbers from .kmodel?

sunnycase commented 5 years ago

If you are using preview5, you can convert model to .c and find scale and bias in the comments in that .c. If you are using RC1, you don't need to do this manually, but the SDK compatible with RC1 isn't released yet.

xhsonny commented 5 years ago

Got it. Thanks a lot!

xhsonny commented 5 years ago

Hi,

I am reopening this issue because I want to check in if tensorflow-workspace will be updated soon to use .kmodel. It is using a .c file now. Thanks.

sunnycase commented 5 years ago

Hi, I would suggest you raise an issue at https://github.com/kendryte/tensorflow-workspace. Also you can check https://github.com/kendryte/kendryte-standalone-demo/tree/develop/face_detect for kmodel usage.

xhsonny commented 5 years ago

OK