C# Image Classification With ML.NET

C# Code Snippets Image Classification With ML.NET


In this code snippet, we’ll see how to do image classification in C# using the ML.NET machine learning framework.

It provides a user friendly GUI for creating, training and deploying different kinds of machine learning models within Visual Studio. It makes it very quick and easy to add machine learning to your .NET projects.

I knew about ML.NET for some time but just recently decided to try it out and add this “tool” to have in my “toolbelt”. I thought I’d build an image classifier that recognizes different electrical components.

I only trained the model for recognizing resistors and toggle switches to avoid having to collect and sort through hundreds if not thousands of images of various electrical components. Granted this doesn’t make the best component classifier but my main goal was to test out ML.NET.


If you don’t already have the ML.NET Model Builder component installed you can do so by opening the Visual Studio installer, going to “Individual components” searching for “ML.NET Model Builder“, selecting it and installing it.

Preparing The Data

I just googled “leaded resistor” and “toggle switch” and downloaded the images using a browser extension. I then deleted some of the images that weren’t explicitly resistors or toggle switches and placed the images in folders like named “leaded resistor” and “toggle switch”.

Machine Learning Model

First, let’s add the ML model to our project by right clicking on it, going under Add and selecting Machine Learning Model.
In this case, we want to do image classification so let’s select that under the “Computer Vision” section. Next, select where you want to train your model(CPU in my case).
For the data select the folder containing the subfolders that contain the images we prepared previously. Start training your model and wait for it to finish.
Finally, we can test the model and integrate it into our code by copying the code snippet provided to us.

Code To Run The Model:

Here I integrated the code snippet provided by the ML model builder into the console application.
namespace ML_Test
    internal class Program
        static void Main(string[] args)
            //Load sample data.
            var imageBytes = File.ReadAllBytes(@"C:\Users\DTPC\Desktop\test imgs\toggle switch example 1.jpg");
            MLModel1.ModelInput sampleData = new MLModel1.ModelInput()
                ImageSource = imageBytes,

            //Load model and predict output.
            var result = MLModel1.Predict(sampleData);
            var labeledScores = MLModel1.GetSortedScoresWithLabels(result);

            Console.WriteLine("**************************************** Result ****************************************");

            //Print out the results.
            foreach (var labeledScore in labeledScores)
                Console.WriteLine($"Item: {labeledScore.Key} Prediction score: {labeledScore.Value}");

            Console.WriteLine($"Highest scoring result is \"{result.PredictedLabel}\" with a score of {result.Score.Last()}.");



Resulting output:

For the test, I took an image of a toggle switch from my parts bin. As you can see it predicted with a high degree of certainty that this was an image of a a toggle switch.
For the next test, I took a picture of some resistors. And as you can it also predicted with a high degree of certainty that this was an image of a resistor.

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger