machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

Saved model returning a different prediction when using AddPredictionEnginePool

Open tearlant opened this issue 1 year ago • 0 comments

System Information (please complete the following information):

  • OS & Version: Windows 10
  • ML.NET Version: 3.0.0
  • .NET Version: 7.0

Describe the bug I'm trying to build something with an API endpoint exposed. While prototyping, I'm essentially following the documentation here: https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/serve-model-web-api-ml-net

I would expect the following two blocks to give the same result:

(In a console app)

var mlContext = new MLContext();
            
SentimentIssue sampleStatement = new SentimentIssue { Text = "I love this movie!" };

string ModelPath = "path/to/SentimentModel.zip";

ITransformer loadedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<SentimentIssue, SentimentPrediction>(loadedModel);
var resultprediction = predEngine.Predict(sampleStatement);

Console.WriteLine($"=============== Single Prediction  ===============");
Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Positive" : "Negative")} sentiment | Probability of being positive: {resultprediction.Probability} ");

Actual output: image

In an API, in the Program.cs file:

builder.Services.AddPredictionEnginePool<SentimentAnalysisModelInput, SentimentAnalysisModelOutput>().FromFile(modelName: "SentimentAnalysisModel", filePath: "path/to/SentimentModel.zip", watchForChanges: true);

// ...

var predictionHandler =
    (PredictionEnginePool<SentimentAnalysisModelInput, SentimentAnalysisModelOutput> predictionEnginePool, SentimentAnalysisModelInput input) =>
    {
        return predictionEnginePool.Predict(modelName: "SentimentAnalysisModel", input);
    };

app.MapPost("/predict", predictionHandler);

app.Run();

The output (for example, using Postman with the same input) image

Expected behavior I would expect the two to be equal, with the same input. Also, I'm getting the same result/probability no matter what input I use:

image

tearlant avatar Dec 16 '23 02:12 tearlant