Run MNIST on MaixPy in 30 lines code

Run MNIST on MaixPy in 30 lines code

here is the preview version of MaixPy run Kmodel V3 MNIST demo.
mnist maixpy.zip (875.0 KB)

First Burn maixpy_kpu_preview.bin, then burn mnist.kfpkg (model)

Power on your Maix Board, and input following code, you will get MNIST run~
We will upload more models later~

import sensor,lcd,image
import KPU as kpu
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((224, 224))	#set to 224x224 input
sensor.set_hmirror(0)				#flip camera
task = kpu.load(0x200000)			#load model from flash address 0x200000
sensor.run(1)
while True:
	img = sensor.snapshot()
	lcd.display(img,oft=(0,0))		#display large picture
	img1=img.to_grayscale(1)		#convert to gray
	img2=img1.resize(28,28)			#resize to mnist input 28x28
	a=img2.invert()					#invert picture as mnist need
	a=img2.strech_char(1)			#preprocessing pictures, eliminate dark corner
	lcd.display(img2,oft=(224,32))	#display small 28x28 picture
	a=img2.pix_to_ai();				#generate data for ai
	fmap=kpu.forward(task,img2)		#run neural network model 
	plist=fmap[:]					#get result (10 digit's probability)
	pmax=max(plist)					#get max probability
	max_index=plist.index(pmax)		#get the digit
	lcd.draw_string(224,0,"%d: %.3f"%(max_index,pmax),lcd.WHITE,lcd.BLACK)	#show result

2 Likes

Hi Zepan,
I tried to run this demo. But with maixpy v0.5.0_12_g284ce83_minimum_with_ide_support.bin an error appears “AttributeError: ‘Image’ object has no attribute ‘invert’”.
In case I flash the maixpy_kpu_preview.bin, my Maixduino is blocked!

Any idea what wrent wrong?
Many thanks Walter

hi, you use the minimum firmware, it delete many methods, you need use normal firmware.

Hi Zepan,
Many thanks for your hint. With maixpy_v0.5.0_12_full it runs very well!

It would be extremly helpful for us newbies, that one may produces a step-by-step info from design and training under Keras/Tensorflow, conversion to tflite, complilation with nncase and implementation on Maix. Because demos are fine, real projects are better.
I will use in my article the Mnist NN.
Best regard Walter

2 Likes