Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence / Keras

Deep Learning in C#: Coin Recognition with a Pre-Trained Model in ML.NET

5.00/5 (1 vote)
9 Nov 2020CPOL3 min read 9.7K  
To end off this series, we will present the alternative of adapting a pre-trained CNN to the coin recognition problem we have been examining all along.
Here we take a look at transfer learning, adjust a predefined CNN, and train our model for the coin recognition problem, using the Model Builder extension.

We’ll be using ML.NET instead of Keras.NET this time. Why not use Keras.NET? Even though Keras.NET is very simple and easy to learn, and while it does include the previously mentioned pre-defined models, its simplicity prevents us from customizing CNN architectures to fit our problem.

ML.NET is a Microsoft machine learning framework that it is free and aimed at development using C# and F#. Most importantly, we can use ML.NET in conjunction with Azure, meaning we can use a cloud-based infrastructure to train our model. Having multiple VMs running our code in a distributed way makes training faster and more accurate.

Why are pre-trained CNNs so valuable? Because someone spent a lot of time and resources training them and we can take advantage of that. We can reuse the knowledge embedded in the weights calculated for the network and we can reapply them to similar problems. That is, they can be applied to more than just the problem the CNN was originally trained to solve. This technique is known as transfer learning. It can save us a lot of time on training and considerably improve the results obtained.

In transfer learning, we don’t start from scratch as we did before. Instead, we start with a known model with a predefined architecture and calculated weights that are downloaded the first time the model is requested. Popular models include Inception, ResNet, and VGG16, among others.

To adjust a predefined CNN for our problem, we must do three things. First, we must change the shape of the input layer to the dimensions of the images in our dataset. Second, we need to alter at least the output layer so the model has the same number of classes that our dataset has. Third, we must adjust the model to let it know that we are not interested in training the layers of the predefined model. After these steps have been completed, we can train or fit our model to the given dataset.

Let’s get started. In Visual Studio, go to Extensions > Manage Extensions and browse for ML.NET Model Builder.

Image 1

We also need to install the Nuget package ML.NET.

Image 2

To train our model for the coin recognition problem, we will use the Model Builder extension.

Image 3

Using this tool, we can easily set up our dataset and train the model, which, by the way, will be automatically selected by the Auto ML feature added to the Model Builder. Automated Machine Learning (Auto ML) is the automating the time-consuming task of machine learning model development. So Model Builder is going to ease for us the process of preparing the dataset and the selection of pre-trained models and all the parameters involved. One thing regarding the pre-trained model selected is that only the last layer will be retrained; all others keep their weights.

For the case of the dataset, the only requirement is that it be organized in folders such that the folder name is the class or label of all the images inside it. When the coin dataset that we have used so far was inputted in the Model Builder, the AutoML engine selected ResNet as the pre-trained architecture to use for our problem.

Image 4

Once the training has finished, we can predict new incoming data and even get the code that corresponds to the model generated for prediction.

With as little work as that, we can do ML with almost without any prior knowledge in the area, simply doing everything though a UI, selecting, clicking and getting the code at the end.

This finishes our series on coin recognition using C#. I hope you enjoyed it! Don’t forget to leave comments and send messages if you have any questions. Thank you for reading. Until next time!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)