How to use a pickle file resulting from auto ml from azure studio in a c# .NET api?

AMROUN Lysa 221 Reputation points
2023-03-29T08:50:19.3433333+00:00

Hello to all,

I am currently working on a Machine Learning project, I have launched the auto ML of Microsoft Azure Studio on my data and I have downloaded the best model in a folder with the appropriate Pickle file. However I would like to use this pickle in my prediction API in c# .NET, for this fact I used Microsoft.ML, to load the pickle and use it, here is my code snippet :

"MLContext mLContext = new MLContext();

var model = mLContext.Model.Load(PicklePath, out DataViewSchema schema); "

However I have an error at this level (2nd line), the error tells me : "Repository doesn't contain entry DataLoaderModel\Model.key".

From what I understand the pickle is incompatible with Microsoft.ML, how can I use the resulting pickle of auto ml in this api please?

Thanks in advance,

Lysa

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,580 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,616 Reputation points
    2023-03-30T07:32:14.4966667+00:00

    @AMROUN Lysa One possible solution to this issue is to use the ONNX (Open Neural Network Exchange) format to export your Azure AutoML model and then load it into your C# .NET application using the Microsoft.ML library. ONNX is a standard format for representing machine learning models that is supported by many different libraries and frameworks, including both Azure AutoML and Microsoft.ML.

    To export your Azure AutoML model to ONNX format, you can use the ModelProxy class in the azureml.automl.runtime module. Here is an example of how to do this:

    from azureml.automl.runtime import ModelProxy
    model_proxy = ModelProxy(model_path='path/to/your/model.pkl')
    onnx_model = model_proxy.serialize_to_onnx('path/to/save/onnx/model.onnx')
    

    Once you have exported your Azure AutoML model to ONNX format, you can load it into your C# .NET application using the ApplyOnnxModel method of the Microsoft.ML.Transforms.Onnx namespace. Here is an example of how to do this:

    using Microsoft.ML;
    using Microsoft.ML.Transforms.Onnx;
    
    var mlContext = new MLContext();
    var onnxModel = mlContext.Transforms.ApplyOnnxModel("path/to/onnx/model.onnx");
    

    This should allow you to use your Azure AutoML model in your C# .NET application using the Microsoft.ML library.