如何在中间件(ML.NET)中将模型添加到PredictionEnginePool? [英] How to add model to PredictionEnginePool in middleware (ML.NET)?

查看:203
本文介绍了如何在中间件(ML.NET)中将模型添加到PredictionEnginePool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET Core应用程序中使用ML.NET,并且在Startup中使用以下代码:

I'm using ML.NET in an ASP.NET Core application, and I am using the following code in Startup:

var builder = services.AddPredictionEnginePool<Foo, Bar>();

if (File.Exists("model.zip"))
{
    builder.FromFile(String.Empty, "model.zip", true);
}

如果 model.zip 不存在,我稍后将在中间件中创建它.如何将其添加到注入的PredictionEnginePool中?

If model.zip doesn't exist, I create it later in the middleware. How do I add it to the PredictionEnginePool that is injected?

没有任何选项可以通过PredictionEnginePool加载模型,而实例化或注入PredictionEnginePoolBuilder则不是一个选项,因为它需要IServiceCollection(因此必须在Startup.ConfigureServices期间进行配置).

There are no options to load a model via PredictionEnginePool, and instantiating or injecting a PredictionEnginePoolBuilder isn't an option as it requires IServiceCollection (so must be configured during Startup.ConfigureServices).

此刻我唯一能看到的选项是,如果文件在启动时不存在,则设置一个标志,然后稍后在中间件中创建 model.zip 后重新启动服务. (使用IApplicationLifetime.StopApplication),但我真的不喜欢将此作为选项.

The only option I can see at the moment is to set a flag if the file doesn't exist at startup, and then restart the service after model.zip is created in the middleware later on (using IApplicationLifetime.StopApplication), but I really don't like this as an option.

推荐答案

PredictionEnginePool的设计方式是,您可以编写自己的ModelLoader实现.开箱即用的Microsoft.Extensions.ML具有2个加载程序,即File和Uri.当这些不能满足您的需求时,您可以下拉菜单并自己编写.

PredictionEnginePool is designed in such a way that you can write your own ModelLoader implementation. Out of the box, Microsoft.Extensions.ML has 2 loaders, File and Uri. When those don't meet your needs, you can drop down and write your own.

请参见 https://github.com/dotnet/machinelearning-samples/pull/560 更改了一个dotnet/机器学习示例,以使用内存中"模型加载器,但不会从文件或Uri中获取模型.您可以遵循相同的模式,并编写获取模型所需的任何代码.

See https://github.com/dotnet/machinelearning-samples/pull/560 which changes one of the dotnet/machine-learning samples to use an "in-memory" model loader, it doesn't get the model from a file or a Uri. You can follow the same pattern and write whatever code you need to get your model.

    public class InMemoryModelLoader : ModelLoader
    {
        private readonly ITransformer _model;

        public InMemoryModelLoader(ITransformer model)
        {
            _model = model;
        }

        public override ITransformer GetModel() => _model;

        public override IChangeToken GetReloadToken() =>
            // This IChangeToken will never notify a change.
            new CancellationChangeToken(CancellationToken.None);
    }

然后在Startup.cs中

And then in Startup.cs

            services.AddPredictionEnginePool<ImageInputData, ImageLabelPredictions>();
            services.AddOptions<PredictionEnginePoolOptions<ImageInputData, ImageLabelPredictions>>()
                .Configure(options =>
                {
                    options.ModelLoader = new InMemoryModelLoader(_mlnetModel);
                });

这篇关于如何在中间件(ML.NET)中将模型添加到PredictionEnginePool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆