#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

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Resources

Advertisement

2 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: