Optical Character Recognition With C#, ML.NET, And NET Core
In this article, I’m going to build an app that recognizes handwritten digits from the famous MNIST machine learning dataset:
The MNIST challenge requires machine learning models to read images of handwritten digits and correctly predict which digit is visible in each image.
This may seem like an easy challenge, but look at this:
These are actual digits from the dataset. Are you able to identify each one? It probably won’t surprise you to hear that the human error rate on this exercise is about 2.5%.
There are 70,000 images of 28x28 pixels in the dataset. I’m going to use a truncated set of 5,000 images to speed up the training.
And I’ll build my app in C# with ML.NET and NET Core.
ML.NET is Microsoft’s new machine learning library. It can run linear regression, logistic classification, clustering, deep learning, and many other machine learning algorithms.
And NET Core is the Microsoft multi-platform NET Framework that runs on Windows, OS/X, and Linux. It’s the future of cross-platform NET development.
The first thing I need for my app is the MNIST data file. You can download it from here: http://yann.lecun.com/exdb/mnist/
Here’s what the dataset looks like:
It’s a CSV file with 785 columns:
- The first column contains the label. It indicates which one of the 10 possible digits is visible in the image.
- The next 784 columns are the pixel intensity values (0..1) for each pixel in the image, counting from left to right and top to bottom.
Let’s get started. Here’s how to set up a new console project in NET Core:
$ dotnet new console -o Mnist
$ cd Mnist
Next, I need to install the ML.NET base package: