Mask.ML/Mask.MLML.Model/ConsumeModel.cs

38 lines
1.3 KiB
C#
Raw Normal View History

2022-06-13 17:41:33 +08:00
// This file was auto-generated by ML.NET Model Builder.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ML;
using Mask_MLML.Model;
2024-08-05 17:12:21 +08:00
using System.IO;
2022-06-13 17:41:33 +08:00
namespace Mask_MLML.Model
{
public class ConsumeModel
{
private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);
// For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
// Method for consuming model in your app
public static ModelOutput Predict(ModelInput input)
{
ModelOutput result = PredictionEngine.Value.Predict(input);
return result;
}
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
2024-08-05 17:12:21 +08:00
string modelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MLModel.zip");
2022-06-13 17:41:33 +08:00
ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
}
}