See the Inner Workings of a Convolutional Neural Network with This PCB Business Card
This business card made by Paul Klinger is able to classify digits by running a CNN and show each layer's state.
Within almost every community exists some sort of ambition project that members can point to as a source of pride when they accomplish it. For example, makers tend to create electronic badges for when they attend conventions like Maker Faire. In the embedded systems/electronics community, they enjoy designing and building electronic business cards to spread their brand, so that's exactly what data scientist Paul Klinger did when he made this card.
It's not like most other electronic business cards that simply light up when a button is pressed, or make a noise in the presence of a light source. It features an integrated circuit running a classifier model that takes in data from a button matrix and displays the output at the end, all while showing the current state of each layer in neural network.
How a CNN Works
A convolutional neural network (CNN) is a class of neural networks that takes in a tensor of a certain shape, like an array of pixel data. This is then passed through a convolutional layer (or layers), in which a given neuron only has access to a subset of the preceding neurons. The convolutional layer consists of a set of learnable filters that compute the dot product between the entries of the filter and the input. This results in a neuron activating when a specific feature is "found" within the input. Finally, these neurons are subsampled into a fully connected layer which shows the output.
This method of classification using CNNs is common in most image recognition applications, such as GoogLeNet and AlexNet. This is partially due to its efficiency when dealing with arrays of data, making it a prime candidate for Klinger's project.
The Electronics
In order for a user to input a digit, they interact with the business card through a five-by-five matrix of buttons. Each button also has an accompanying LED to indicate if that particular pixel is active. Then to the right is a series of smaller matrices that display the current state of each layer in the CNN, along with the final dense layer below them. Because the microcontroller (an ATtiny3217) doesn't have nearly enough GPIO pins to drive each LED individually, there is a pair of shift registers next to it that handle current delivery for the LEDs.
Power is provided by a single CR2032 coin cell battery on the back of the card in order to save space and weight. The card's back also has a few test pads for uploading new programs.
Creating a Model
When using a neural network to classify something, it must first be trained. This entails entering some kind of input, seeing the output, and then comparing the two and providing feedback. Over time, the weights of each neuron in the network will get closer and closer, eventually showing the correct output for inputs that aren't part of the original training set. One problem that Klinger ran into, however, was trying to generate enough training data while avoiding overfitting. This is because the matrix is only five by five elements large, so there are only so many ways to draw a certain digit. He was able to get it to work, even though the number of parameters for each filter is quite large.
The Code
A standard CR2032 battery can only output around 3V, which means the ATtiny MCU can only run 10MHz. This creates a problem when trying to drive the LEDs, since flickering begins to appear when the update rate drops too low. Through some clever coding and the use of an internal hardware timer, Klinger was able to mostly avoid this issue.
The button matrix is polled at a regular interval too, and it works by reading the row in a loop and checking if the corresponding column has also been activated. Finally, a simple animation starts whenever the card is powered on, and it's created with an animation layout tool written in Python.
Power Consumption
Since this entire circuit runs off of a single CR2032 cell, power consumption is a pressing concern. That's why there are several features that actively and passively reduce the amount of current drawn. These include the use of white LEDs (less power needed for a certain brightness), soft power buttons, and an automatic sleep timer that causes the ATtiny to enter a sleep mode if two minutes have elapsed without a button press.
More Information
This project is an excellent way to learn about how convolutional neural networks function to classify inputs. To see the code, PCB design files, and more, head on over to Klinger's Github repository for this project.