Last issue recap: (Click here to jump to the previous issue)
This issue will introduce how to run the Mnist Demo (handwritten digit recognition) on the RT-Thread operating system, allowing for verification of your own handwritten digits.
MNIST holds the same position in the field of artificial intelligence as “Hello World” does in various programming languages.Therefore, this experiment will focus on MNIST to further understand the mysterious connection between artificial intelligence and embedded systems.This issue implements:By reproducing the neural network using the CMSIS NN library, importing int type weight files, and successfully implementing Mnist inference in the RT-Thread system.
How to run Mnist on RT-Thread:
Pull Mnist_CMSIS or Mnist_CMSIS.7z from Github to local, Mnist_CMSIS is 520M, it is recommended to download the compressed package, only 66.4M
Run method, choose one:
Scons
MDK5 Compile
CMSIS + RT-Thread inference success interface
The folder on Github already contains the CMSIS packages required for the experiment, download to run, and the newly created project needs to enable the CMSIS package in RT-Thread’s Menuconfig.
1. PC Training Model
File: mnist.ipynb
1.1 PC Environment
Tensorflow: 2.3.0-dev20200515
Numpy: 1.16.4
Keras: 2.2.4-tf
1.2 Dataset
File: ./data/mnist.npz
The MNIST dataset consists of 60,000 (training set) + 10,000 (test set) handwritten characters, each image size is , dataset manual download address http://yann.lecun.com/exdb/mnist/ .
1.3 Network Structure
Two layers of convolution + one fully connected layer
3. Use CMSIS Software Package to Generate Network Structure
By calling the CMSIS API, implement the network structure, this step requires certain deep learning basics.
In addition, during the reconstruction process, int is used instead of float.
This project implements a method for passing data, currently a basic method, only defining an array of size 784 in the main.c file to store the 28*28 size handwritten digit image, the format requirement is Int type. The specific code for reading image data is implemented in mnist.ipynb.
Since the input is a simple 28*28 = 784 (one-dimensional) array, it can support custom handwritten digit recognition verification. It is recommended to first perform custom handwritten digit recognition verification in mnist.ipynb.
Custom handwritten digit saved images should be similar to those in the training set, if the saved image is not 28*28 in size, you can refer to the code in mnist.ipynb to resize it to 28*28, ensuring that the input one-dimensional array is of size 784, consistent with the network input.
Those interested can read the source files, no changes were made to other files.
./Mnist_CMSIS/applications/main.c
./Mnist_CMSIS/applications/mnist_parameters.h
4. Compile & Flash
Windows (recommended using MDK)
MDK one-click compilation and flashing, observe output through Putty.
Linux (recommended using Scons)
Scons compilation, flash using STM32 Cube Programmer, observe output through minicom.
In the ./Mnist_CMSIS/SConscript file, change it to as shown in the picture.
RT-Thread
Making the development of IoT terminals simple and fast, maximizing the value of chips. Licensed under Apache 2.0, can be used freely in commercial products without the need to disclose the source code, with no potential commercial risks.