Made with Tensorflow and Pygame
This project was made with the MNIST dataset of handwritten digits, while the user interface was made using Pygame.
The neural network architecture was provided by Tensorflow library, and in this case I used one hidden layer with 128 neurons and the Rectified Linear Unit as the activation function, while the ouptut layer was activated with the Softmax function.
The compiler was a standard one (adam optmization and categorical crossentropy as loss function).
The data was gathered from this site, which contains the MNIST dataset in .csv files, so I could easily work with pandas library and preprocess the data.
Despite the accuracy of the model being in average 90%, with a loss cost of something near 0.07, the prediction doesn't always work perfectly, whose cause I suppose has something to do with Pygame conversion of the surface to an array. I first tried to simply draw onto the canvas and then convert the surface to an array, but to do so I had to scale the surface down, to match the 28 x 28 format from the dataset. I'm not sure of the way that Pygame does it, but it has a ridiculous downgrade in resolution, and if even I couldn't recognize the numbers after the scale-down, imagine the Neural Network.
To handle the problem, I made myself a pixel-ish class, that is painted white when clicked, and then scaled it to the size of the screen that I had - however it has a downside, which is an awful resolution. For my surprise, this approach not only made the predictions more precise, but also improved the performance. My guess is that it has something to do with the manner that Pygame does the array conversion, but again, I'm not sure.
- Draw with the mouse
- Reset the screen
- Make a guess
Command | Function |
---|---|
Keyboard R | Erase the content of the screen |
Keyboard G | Make a guess |
Left Mouse Button | Draw |
The prediction is shown in the console that you are running the program. It is just a simple print(prediction) line of code.