Skip to content

Update ConsumeModel.cs to enhance it's performance when being called for multiple times - #4913

Merged
LittleLittleCloud merged 3 commits into
dotnet:masterfrom
LittleLittleCloud:u/xiaoyun/updateConsumeModel
Mar 4, 2020
Merged

Update ConsumeModel.cs to enhance it's performance when being called for multiple times#4913
LittleLittleCloud merged 3 commits into
dotnet:masterfrom
LittleLittleCloud:u/xiaoyun/updateConsumeModel

Conversation

@LittleLittleCloud

@LittleLittleCloud LittleLittleCloud commented Mar 4, 2020

Copy link
Copy Markdown
Member

Sample Code

// 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 Test.Model;

namespace Test.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/model-builder-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();

            // Register LabelMapping
            mlContext.ComponentCatalog.RegisterAssembly(typeof(LabelMapping).Assembly);

            // Load model & create prediction engine
            string modelPath = @"\path\to\model";
            ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

            return predEngine;
        }
    }
}

Related Issue

@LittleLittleCloud
LittleLittleCloud requested a review from a team as a code owner March 4, 2020 01:08
@briacht

briacht commented Mar 4, 2020

Copy link
Copy Markdown
Contributor

Can you please change aka link to: https://aka.ms/mlnet-consume

@LittleLittleCloud
LittleLittleCloud merged commit d463660 into dotnet:master Mar 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants