MLNet Image Classification

The first thing you must do is create a .Net Core Console Application in your Visual Studio, after that I named the project "Image Classification".

So after that let's add some NuGet packages to work with ML.NET.

  1. Microsoft.ML (1.5.0-preview2)
  2. Microsoft.ML.ImageAnalytics (1.5.0-preview2)
  3. Microsoft.ML.Vision (1.5.0-preview2)
  4. SciSharp.Tensorflow.Redist-Windows-GPU (1.15.1)

It must look like this:

We're going to continue with the Build Configuration on our project.

Go to Project Properties > Build > Platform Target : x64

This is because sometimes when runnning the application, it doesn't know which library take for tensorflow processing, and it'll say that tensorflow dll wasn't found.

Create 3 Folders on Project ( assets, Models, workspace).

Then we need a dataset, I'm using one public dataset from here.

Once it's downloaded unzip the content on the assets folder previously created, it'll look like this:


Right now we are able to code our image classifier.

For that first we're going to create our models.

On Models Folder create 3 classes ( ModelOutput.cs, ModelInput.cs and ImageData.cs)

ImageData must look like this:


ModelInput must look like this:


ModelOutput must look like this:


So thats our models ready to interact with the next processing.

Let's work on out Program.cs

  • On Main method add some namespaces to work with libraries:
  • Next we're going to add some logic to retrieve the data from our assets folder:
  • Initialize the ML Context:
  • Load image and mix the dataset for training and testing:
  • After our Main Method add this logic to retrieve image from a Directory:
  • Fit or preprocess our data:
  • Split our Data on test, validations and train set:
  • Create the Pipeline:
  • Define an Estimator Chain:
  • Fit Model:
  • Create this methods to make predictions for one image and for a set of images:
  • Call this methods at last of Main method:

*If you don't have GPU on your computer please install the normal version of SciSharp NuGet *

And that's it, if you run your application it'll take like 10 minutes to complete the process so after processing it will look like this:

That's not the best part, if you look at your workspace folder, you must watch a .pb file and all metadata for training model.

Great !!! Now you have done the image processing with ML.Net.


You can download the complete solution from here: GitHub

Share this post