#ML.NET Image Classification with ML.NET Command-Line Interface (CLI), no #VS2019 needed !

Buy Me A Coffee

Hi !

I haven’t write a lot about ML.NET Command-Line Interface (CLI) (see references), and that’s a shame. It’s very powerful and also it supports most of the ML.Net features, like in example: Model Builder Image Classification scenario.

The ML.NET CLI automates model generation for .NET developers.
To use the ML.NET API by itself, (without the ML.NET AutoML CLI) you need to choose a trainer (implementation of a machine learning algorithm for a particular task), and the set of data transformations (feature engineering) to apply to your data. The optimal pipeline will vary for each dataset and selecting the optimal algorithm from all the choices adds to the complexity. Even further, each algorithm has a set of hyperparameters to be tuned. Hence, you can spend weeks and sometimes months on machine learning model optimization trying to find the best combinations of feature engineering, learning algorithms, and hyperparameters.
The ML.NET CLI simplifies this process using automated machine learning (AutoML).

ML.Net CLI Documentation

So, let’s take a look at the basic sample for an Image Recognition scenario with ML.Net CLI. It starts with a set of images, organized in folders. Each folder will represent the label for the image.

The only required parameter is [–dataset], with the location if the folder with images.

I’ll use the following command to also define the output project name and the log file location

mlnet image-classification --dataset "SimpleDrawings" --log-file-path "d:\src\Labs\mlnet\logs" --name "SimpleDrawingsBlog"

The process takes sometime, and when it is finished, it generates 2 projects, to consume the model and to generate a new model.

Note: somehow the parameter -name, didn’t generate the specified name for my output project. Time to refactor later.

mlnet 04 visual studio

Testing the generated model with a custom image, give us the following ouput.

2020-10-19 14:09:47.512322: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Using model to make single prediction -- Comparing actual Label with predicted Label from sample data...


ImageSource: D:\src\Labs\mlnet\SimpleDrawings\fish\i-1238b28c03144be496b7f54d82667fc6.jpg


Predicted Label value fish
Predicted Label scores: [0.98024565,0.015911957,0.0038424365]


=============== End of process, hit any key to finish ===============

Which comes, from the predicted label (top scorer), and also the scores for each one of the labels.

            // Create single instance of sample data from first line of dataset for model input
            ModelInput sampleData = new ModelInput()
            {
                ImageSource = @"D:\src\Labs\mlnet\SimpleDrawings\fish\i-1238b28c03144be496b7f54d82667fc6.jpg",
            };

            // Make a single prediction on the sample data and print results
            var predictionResult = ConsumeModel.Predict(sampleData);

            Console.WriteLine("Using model to make single prediction -- Comparing actual Label with predicted Label from sample data...\n\n");
            Console.WriteLine($"ImageSource: {sampleData.ImageSource}");
            Console.WriteLine($"\n\nPredicted Label value {predictionResult.Prediction} \nPredicted Label scores: [{String.Join(",", predictionResult.Score)}]\n\n");
            Console.WriteLine("=============== End of process, hit any key to finish ===============");

Super easy !

Happy coding!

Greetings

El Bruno

Resources

#VS2019 – Let’s do some image classification with #MLNET Model Builder! (AKA, let’s create an image classifier model without a line of code)

Buy Me A Coffee

Hi!

I’m getting ready for my last event of the year, and I just realize that in the latest update of Model Builder, we have the chance to build our own Image Classifier scenario. Let’s start with the official Model Builder definition (see references):

ML.NET Model Builder provides an easy to understand visual interface to build, train, and deploy custom machine learning models. Prior machine learning expertise is not required. Model Builder supports AutoML, which automatically explores different machine learning algorithms and settings to help you find the one that best suits your scenario.

Working with images was supported for a while in Machine Learning.Net. In the Machine Learning .Net Samples, we have sample scenarios like

Image Classification Model Training – Preferred API (Based on native TensorFlow transfer learning)

In this sample app you create your own custom image classifier model by natively training a TensorFlow model from ML.NET API with your own images.

We even have an amazing tutorial, to create our own image classification model from zero

Tutorial: Generate an ML.NET image classification model from a pre-trained TensorFlow model

Learn how to transfer the knowledge from an existing TensorFlow model into a new ML.NET image classification model. The TensorFlow model was trained to classify images into a thousand categories. The ML.NET model makes use of part of the TensorFlow model in its pipeline to train a model to classify images into 3 categories.

Training an Image Classification model from scratch requires setting millions of parameters, a ton of labeled training data and a vast amount of compute resources (hundreds of GPU hours). While not as effective as training a custom model from scratch, transfer learning allows you to shortcut this process by working with thousands of images vs. millions of labeled images and build a customized model fairly quickly (within an hour on a machine without a GPU). This tutorial scales that process down even further, using only a dozen training images.

And now, I found that Model Builder, also supports an Image Classification Scenario.

It follows the Model Builder standard workflow, starting with the selection of the scenario:

model builder select scenario

And then selecting a folder with the Images.

model builder images for training

Important: Model Builder expects image data to be JPG or PNG files organized in folders that correspond to the categories of the classification.

To load images into Model Builder, provide the path to a single top-level directory:

  •     This top-level directory contains one subfolder for each of the categories to predict.
  •     Each subfolder contains the image files belonging to its category.

Once the folder is selected, we can see a preview of the images and labels loaded from the folder.

model builder folder selected image preview

For more information about how to organize images for this scenario, refer to Load training data into Model Builder.

And now we start the training process. This may take a while, depending on your hardware. I’m using the sample set of drawings that we used on the InsiderDev Tour, for Custom Vision. These are 24 drawings images, with 3 labels, and in a PC with a I7, 32GB of Ram and an SSD, the training process took a little longer than 2 minutes.

model builder train images complete

Once the training is complete, we have a decent accuracy in our model, so it’s time to test. Before Model Builder last step, we have the chance to test the model with some test images.

Using one of the images that I created at Ignite in Orlando, the trained model get’s a human with a 99% of accuracy.

model builder model trained test image

And, the final step is to add the generated model and code to our project. I’ll write about how to use this generated code on the near future.

model builder code generated

Happy Coding!

Greetings @ Burlington

El Bruno

References