Skip to content

Instantly share code, notes, and snippets.

@andriyadi
Last active November 16, 2019 12:44
Show Gist options
  • Save andriyadi/5d625d076abcca0945ed1b102ac1497f to your computer and use it in GitHub Desktop.
Save andriyadi/5d625d076abcca0945ed1b102ac1497f to your computer and use it in GitHub Desktop.
Managed to create Human Activity Recognition (HAR) model using Keras, and successfully converted to kmodel for K210 MCU. However.... The inference is still off, why oh why?
import image
import KPU as kpu
import lcd
lcd.clear()
lcd.draw_string(100,112,"Loading model...")
print('Loading model...')
task = kpu.load('/sd/HAR.kmodel')
# Sample data
data = [[22, 20, 15, 0],
[14, 20, 15, 0],
[13, 30, 23, 0],
[17, 9, 11, 0],
[ 8, 30, 21, 0],
[22, 30, 19, 0],
[ 5, 24, 18, 0],
[17, 19, 19, 0],
[ 8, 30, 27, 0],
[14, 13, 17, 0],
[13, 28, 29, 0],
[21, 24, 14, 0],
[ 5, 25, 19, 0],
[14, 19, 17, 0],
[11, 30, 27, 0],
[17, 13, 17, 0],
[15, 24, 27, 0],
[17, 14, 13, 0],
[13, 25, 20, 0],
[23, 18, 14, 0],
[17, 19, 15, 0],
[12, 30, 24, 0],
[15, 10, 12, 0],
[10, 29, 23, 0],
[18, 28, 17, 0],
[ 7, 23, 19, 0],
[15, 18, 19, 0],
[ 5, 30, 25, 0],
[17, 15, 19, 0],
[10, 25, 21, 0],
[14, 19, 19, 0],
[18, 20, 14, 0],
[17, 30, 23, 0],
[18, 9, 8, 0],
[12, 30, 21, 0],
[18, 28, 17, 0],
[ 8, 27, 20, 0],
[17, 18, 19, 0],
[20, 27, 14, 0],
[22, 30, 22, 0],
[17, 8, 8, 0],
[11, 30, 15, 0],
[22, 30, 20, 0],
[13, 22, 19, 0],
[17, 19, 14, 0],
[14, 30, 23, 0],
[12, 27, 29, 0],
[22, 18, 14, 0],
[ 5, 25, 21, 0],
[22, 18, 18, 0],
[23, 30, 22, 0],
[17, 17, 19, 0],
[ 9, 30, 10, 0],
[27, 30, 20, 0],
[13, 21, 19, 0],
[17, 17, 17, 0],
[10, 30, 27, 0],
[14, 14, 17, 0],
[13, 27, 29, 0],
[18, 15, 12, 0],
[ 4, 24, 20, 0],
[17, 17, 20, 0],
[ 4, 30, 22, 0],
[15, 15, 19, 0],
[11, 24, 28, 0],
[20, 12, 13, 0],
[ 5, 24, 20, 0],
[19, 18, 17, 0],
[ 9, 30, 13, 0],
[24, 27, 21, 0],
[18, 9, 4, 0],
[12, 28, 7, 0],
[23, 30, 23, 0],
[18, 20, 18, 0],
[19, 17, 19, 0],
[15, 8, 4, 0],
[13, 23, 27, 0],
[18, 29, 22, 0],
[17, 22, 13, 0],
[ 9, 21, 21, 0]]
# Preparing dummy 'image' for input as MaixPy expect image data
dummy = image.Image()
inputData = dummy.to_grayscale(1)
inputData = inputData.resize(4, 80)
# Fill the data to dummy image
for rowIdx, (x, y, z, a) in enumerate(data):
inputData.set_pixel(0, rowIdx, x)#(x,x,x))
inputData.set_pixel(1, rowIdx, y)#(y,y,y))
inputData.set_pixel(2, rowIdx, z)#(z,z,z))
inputData.set_pixel(3, rowIdx, a)#(a,a,a))
#print("orig: {}, {}, {}, {}".format(x, y, z, a))
#print("dest: {}, {}, {}, {}".format(inputData.get_pixel(0, rowIdx), inputData.get_pixel(1, rowIdx), inputData.get_pixel(2, rowIdx), inputData.get_pixel(3, rowIdx)))
# Dump to serial to check they're same
# for row in range(80):
# print("Row: {}, {}, {}, {}".format(inputData.get_pixel(0, row), inputData.get_pixel(1, row), inputData.get_pixel(2, row), inputData.get_pixel(3, row)))
# print("image w: {}, h:{}".format(inputData.width(), inputData.height()))
lcd.display(inputData)
# Prepare data for AI buffer
inputData.pix_to_ai()
# Forward
fmap = kpu.forward(task, inputData)
plist = fmap[:]
print(plist)
pmax=max(plist) #get max probability
max_index=plist.index(pmax)
lcd.draw_string(0,0,"%d: %.3f"%(max_index,pmax),lcd.WHITE,lcd.BLACK)
@andriyadi
Copy link
Author

@andriyadi
Copy link
Author

The issue is that, the output is always similar as:
(0.05627171, 0.01401903, 0.3184637, 0.4632558, 0.1154114, 0.03257844)
regardless the input. As we can see that 3rd index (0.4632558 ) is always bigger means the prediction always the same.

@andriyadi
Copy link
Author

I also add put the issue here: https://bbs.sipeed.com/t/topic/863 with the hope anyone can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment