DEV Community

Cover image for AI and Machine Learning Integration in .NET Core with ML.NET
Paulo Torres
Paulo Torres

Posted on

AI and Machine Learning Integration in .NET Core with ML.NET

Artificial intelligence (AI) and machine learning (ML) are transforming industries by enabling data-driven decision-making, automating complex tasks, and providing deep insights through predictive analytics. With .NET Core and ML.NET, Microsoft’s open-source machine learning framework, developers can integrate AI and ML capabilities directly into their .NET applications. This article delves into advanced techniques for integrating AI and ML in .NET Core using ML.NET, with real-world examples to illustrate these concepts.

Introduction to ML.NET

ML.NET is a cross-platform, open-source machine learning framework developed by Microsoft. It allows .NET developers to build, train, and deploy custom machine learning models directly within .NET applications. ML.NET provides a variety of pre-built algorithms for tasks such as classification, regression, clustering, and more. The framework also supports integration with popular ML tools like TensorFlow, ONNX, and AutoML for more advanced scenarios.

Key Features of ML.NET:

  • Custom ML Models: Build and train custom models using C# or F#.
  • Integration with .NET Core: Seamlessly integrate ML models into .NET Core applications.
  • Support for Popular ML Algorithms: Includes support for a wide range of machine learning algorithms.
  • Cross-Platform: Run on Windows, Linux, and macOS.

Setting Up ML.NET in a .NET Core Project

Step 1: Create a New .NET Core Project

First, create a new .NET Core console application:

dotnet new console -n AIWithMLNET
cd AIWithMLNET
Enter fullscreen mode Exit fullscreen mode

Step 2: Add ML.NET NuGet Packages

Add the ML.NET package to your project:

dotnet add package Microsoft.ML
Enter fullscreen mode Exit fullscreen mode

Step 3: Import Necessary Namespaces

In your Program.cs file, import the necessary ML.NET namespaces:

using Microsoft.ML;
using Microsoft.ML.Data;
Enter fullscreen mode Exit fullscreen mode

Real-World Example: Predicting Housing Prices

To demonstrate the integration of ML.NET in a .NET Core application, we’ll build a model to predict housing prices based on various features such as the number of bedrooms, square footage, and location.

Step 4: Define the Data Model

Create a data model class to represent the input features and the prediction output:

public class HousingData
{
    [LoadColumn(0)]
    public float Size { get; set; }

    [LoadColumn(1)]
    public float Bedrooms { get; set; }

    [LoadColumn(2)]
    public string Location { get; set; }

    [LoadColumn(3)]
    public float Price { get; set; }
}

public class HousingPricePrediction
{
    [ColumnName("Score")]
    public float Price { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Load and Prepare the Data

Load the data from a CSV file and prepare it for training:

var context = new MLContext();
var dataPath = "housing_data.csv";
IDataView data = context.Data.LoadFromTextFile<HousingData>(dataPath, hasHeader: true, separatorChar: ',');

var split = context.Data.TrainTestSplit(data, testFraction: 0.2);
var trainData = split.TrainSet;
var testData = split.TestSet;
Enter fullscreen mode Exit fullscreen mode

Step 6: Define the Data Processing Pipeline

ML.NET allows you to define a pipeline for data preprocessing and model training. In this example, we’ll encode categorical data, normalize the numeric data, and define a regression algorithm for training:

var pipeline = context.Transforms.Categorical.OneHotEncoding("Location")
    .Append(context.Transforms.Concatenate("Features", "Size", "Bedrooms", "Location"))
    .Append(context.Transforms.NormalizeMinMax("Features"))
    .Append(context.Regression.Trainers.FastTree());
Enter fullscreen mode Exit fullscreen mode

Step 7: Train the Model

Train the model using the training dataset:

var model = pipeline.Fit(trainData);
Enter fullscreen mode Exit fullscreen mode

Step 8: Evaluate the Model

Evaluate the model’s performance using the test dataset:

var predictions = model.Transform(testData);
var metrics = context.Regression.Evaluate(predictions, labelColumnName: "Price");

Console.WriteLine($"R^2: {metrics.RSquared}");
Console.WriteLine($"MAE: {metrics.MeanAbsoluteError}");
Enter fullscreen mode Exit fullscreen mode

Step 9: Make Predictions

Once the model is trained and evaluated, you can use it to make predictions on new data:

var predictionEngine = context.Model.CreatePredictionEngine<HousingData, HousingPricePrediction>(model);
var newHouse = new HousingData { Size = 2000, Bedrooms = 3, Location = "Downtown" };
var prediction = predictionEngine.Predict(newHouse);

Console.WriteLine($"Predicted Price: {prediction.Price}");
Enter fullscreen mode Exit fullscreen mode

Advanced Integration: Using Pre-Trained Models with TensorFlow

ML.NET also allows integration with pre-trained models built using TensorFlow. This is particularly useful for tasks like image classification, where training a model from scratch can be time-consuming and require a large dataset.

Step 10: Integrate a Pre-Trained TensorFlow Model

To integrate a TensorFlow model, first, ensure that the TensorFlow .NET binding is installed:

dotnet add package SciSharp.TensorFlow.Redist
Enter fullscreen mode Exit fullscreen mode

Next, load the TensorFlow model and use it for predictions:

var tensorFlowModel = context.Model.LoadTensorFlowModel("model.pb");
var pipeline = tensorFlowModel.ScoreTensorName("output_score")
    .Append(tensorFlowModel.Output("input", "output_score"))
    .Append(context.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));
Enter fullscreen mode Exit fullscreen mode

Deploying ML.NET Models in ASP.NET Core

Step 11: Deploying the Model

Integrating ML.NET models into ASP.NET Core allows you to expose the model as a RESTful API. First, create an ASP.NET Core Web API project:

dotnet new webapi -n MLWebAPI
cd MLWebAPI
Enter fullscreen mode Exit fullscreen mode

In the controller, load the trained model and create an endpoint to return predictions:

[Route("api/[controller]")]
[ApiController]
public class PredictionController : ControllerBase
{
    private readonly PredictionEngine<HousingData, HousingPricePrediction> _predictionEngine;

    public PredictionController(PredictionEngine<HousingData, HousingPricePrediction> predictionEngine)
    {
        _predictionEngine = predictionEngine;
    }

    [HttpPost]
    public ActionResult<HousingPricePrediction> Predict([FromBody] HousingData data)
    {
        var prediction = _predictionEngine.Predict(data);
        return Ok(prediction);
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Integrating AI and machine learning into .NET Core applications using ML.NET opens up a world of possibilities for developers. Whether you’re building predictive models for business applications or leveraging pre-trained models for more complex tasks like image recognition, ML.NET provides a robust and flexible framework for integrating machine learning into your software solutions. With its seamless integration into the .NET ecosystem, ML.NET allows you to build, train, and deploy machine learning models with ease, all within the comfort of the .NET environment.

Top comments (0)