
This project is a tiny neural network that recognises handwritten digits (0 to 9).
You give it a small black-and-white picture of a digit, 8x8 pixels, and it tells you which digit it thinks it is. The answer appears on the 7-segment display.
The network was trained with my own neural network software, written from scratch in C (github.com/sergiothcreator/Neural-Network-from-Scratch-in-C-for-MNIST). The code that trains this small version and exports it to the chip is in github.com/sergiothcreator/Neural-Network-from-Scratch-in-C-for-TinyTapeout. I trained it on the MNIST dataset of handwritten digits, shrunk from 28x28 to 8x8 pixels so that it fits on a single tile. To fit in the chip, the trained weights were then rounded to small whole numbers (-2 to 2) and built directly into the logic.
How well it works, on the 10,000 MNIST test images:
| Network | Accuracy |
|---|---|
| My original network (28x28 images, run on a PC) | 92.5 % |
| Small version (8x8 images, run on a PC) | 86.0 % |
| Small version with rounded weights (this chip) | 82.8 % |
Inside the chip, the network looks at one pixel at a time. For each of the 10 digits it adds up a score from the pixels that are on, and the digit with the highest score wins. The whole answer takes 640 clock cycles.
None required. The demo board's 7-segment display shows the result, and the picture can be sent from the RP2040.
The test code is in the demo folder of this project's GitHub repository:
a Python program where you draw a digit on the PC and send it to the board, and an FPGA version of the
test setup that I used to try the design before it was manufactured.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | DIN: image bit (1 bit/pixel, pixel 0 first) | 7-seg a (predicted digit) | predicted digit bit 0 |
| 1 | STROBE: rising edge shifts DIN into the image register | 7-seg b | predicted digit bit 1 |
| 2 | GO: rising edge starts inference | 7-seg c | predicted digit bit 2 |
| 3 | 7-seg d | predicted digit bit 3 | |
| 4 | 7-seg e | busy | |
| 5 | 7-seg f | done | |
| 6 | 7-seg g | ||
| 7 | done (decimal point) |