How to run a simple sequential NN on kpu?

After running the demo programs for object and face recognition, I want to implement a simple example starting from the very beginning on the Maixduino. For this I took the pima-indians-diabetes case for diabetes classification, because it only has 768 simple training records.

Each record has 8 input data plus label, e.g.

For this, I use the simple keras-model, which runs successfully on my Ubuntu-PC:
model = Sequential()
model.add(Dense(12, input_dim=8, activation=‘relu’))
model.add(Dense(8, activation=‘relu’))
model.add(Dense(1, activation=‘sigmoid’))

Then the generated h5-file is converted to tflite by the following program:
#Convert Keras to tflite
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file( ‘Keras1.h5’ )
tflite_model = converter.convert()
open(“Keras1.tflite”, “wb”).write(tflite_model)

In order to get it on the kpu, I use nncase with the following command:
./ncc/ncc compile Keras1.tflite Keras1.kmodel -i tflite -o kmodel -t k210 --dataset-format raw --inference-type float

And indeed a kmodel has been generated:
0 dense_1_input 1x8
0 dense_3/Sigmoid 1x1

Now I have some questions:

  1. Is it possible to transfer sequential models to kpu or is the device restricted to CNNs?
  2. Is the nncase command OK?
  3. Must the kmodel format before flashing converted to kfpkg or can it be placed directly on 0x300000?
  4. Which micropy instruction I have to perform after task = kpu.load(0x300000) in order to test the model?

Because I am writing now an article for a well known electronic magazine in Germany (and Europe), it would be very helpful to find a solution.

So any help is very welcome!
Many thanks and best regards

  1. it is ok , maixpy can run model generate by nncase, in the other word, you have generate the kmodel, and you can run it.
  2. it is basically ok, but you set --inference-type float, it will use cpu to calculate, will slow. but your model is small, it is still ok.
  3. when you use our kflash_gui, it can assign the addr, you needn’t package kmodel to kfpkg.
  4. kpu.forward(task), please refer to maixpy’s basic mnist tutorial.

Hi Zepan,

Many thanks for your kind support.

The instruction task = kpu.load(0x300000) generates the following msg:
ValueError: [MAIXPY]kpu: load error:2, ERR_KMODEL_VERSION: only support kmodel V3 now

Is the used nncase too new? Must I use another version or a different firmware (used v05)?

Best regards

Sorry, just seen “MaixPy_support_Kmodel”! Will try it.

Have flashed the new firmware with IDE support.
Now I get the msg:
ValueError: [MAIXPY]kpu: load error:2, ERR_KMODEL_VERSION: only support kmodel V3/V4 now

It seems my model is incorrect. Any idea?

can you put on your kmodel and script?

The kmodel, script and even the h5-files are attached in zip format.
Best regards Walter (6,2 KB)

I can load the kmodel correctly, but you script is very wrong…

Hello Zepan, you are my hero!
It would be very nice, if you would let me know, how you did it and what is wrong in my test script.
Many thanks and best regards

ummm, I have some trouble with pneumonia epidemic situation here,
I have to deal with some other things this week, I will come back next week…

A solution regarding pneumonia epidemic is much more important.

BTW: The first part of my article about Maixduino is ready to print and will appear in the May issue.
The second part with MicroPy and NNs should be ready end of Feb. Currently I am writing the ESP32 chapter, than the diabetes part should come.
If you wish, I will provide you with copies, if the print set is ready. My text is in German, but the publisher will translate it to other languages too.
Best regards Walter

Hello Zepan,

Meanwhile I designed a new small NN with 4 inputs and 4 labels.
With nncase ist has been converted to kmodel.
According to the output of the script, it has been loaded properly.
But how to get the input to that NN? Tuple and array have been refused.

Do you think, that kpu can handle non-image NNs as well?

Best regards Walter
import KPU as kpu

task = kpu.load(0x300000)

#fmap = kpu.forward(task,(1,1,1,1)) # TypeError: Can’t convert tuple to type

output_idx = 0
fmap = kpu.get_output(task, output_idx)

#fmap = kpu.netinfo(task) #OSError 14
a = kpu.deinit(task)

{“model_addr”: 3145728, “model_size”: 1264, “model_path”: “(null)”, “net_args”: “(null)”}
{“fmap”: “data”=0x801b3d90, “size”=16, “index”: 6, “w”: 0, “h”: 0, “ch”: 0, “typecode”: f}
MicroPython v0.5.0-14-g7a46bb0cf-dirty on 2020-01-28; Sipeed_M1 with kendryte-k210
Type “help()” for more information.