Beispiel #1
0
        /// <summary>
        /// Enable model monitoring for the <see cref="IModelStorageProvider"/>.
        /// Overrides the default model storage provider.
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <typeparam name="TPrediction"></typeparam>
        /// <param name="builder"></param>
        /// <param name="storageName">The name of the ML model in the storage provider.</param>
        /// <param name="modelStorageProvider">The model storage provider. The default model storage provider is <see cref="InMemoryModelStorageProvider"/>.</param>
        /// <returns></returns>
        public static IModelPredictionEngineBuilder <TData, TPrediction> WithStorageProvider <TData, TPrediction>(
            this IModelPredictionEngineBuilder <TData, TPrediction> builder,
            string storageName,
            IModelStorageProvider modelStorageProvider = null)
            where TData : class where TPrediction : class, new()
        {
            builder.Services.Configure(builder.ModelName, (Action <ModelPredictionEngineOptions <TData, TPrediction> >)((mlOptions) =>
            {
                mlOptions.CreateModel = (mlContext) =>
                {
                    if (modelStorageProvider == null)
                    {
                        modelStorageProvider = new InMemoryModelStorageProvider();
                    }

                    ChangeToken.OnChange(
                        () => modelStorageProvider.GetReloadToken(),
                        () => mlOptions.Reload());

                    return(GetTransfomer(storageName, mlContext, modelStorageProvider));
                };
            }));

            return(builder);
        }
        public static IModelPredictionEngineBuilder <TInput, TPrediction> From <TInput, TPrediction, TLoader>(
            this IModelPredictionEngineBuilder <TInput, TPrediction> builder,
            Action <ModelLoderFileOptions>?configure = default,
            ServiceLifetime serviceLifetime          = ServiceLifetime.Transient)
            where TInput : class
            where TPrediction : class, new()
            where TLoader : ModelLoader
        {
            builder.Services.AddModelLoader <TInput, TLoader>(builder.ModelName, configure, serviceLifetime);

            builder.Services
            .AddOptions <ModelPredictionEngineOptions <TInput, TPrediction> >(builder.ModelName)
            .Configure <IServiceProvider>(
                (mlOptions, sp) =>
            {
                mlOptions.ModelName = builder.ModelName;

                mlOptions.ModelLoader = sp.GetRequiredService <IOptionsMonitor <ModelLoaderOptions> >()
                                        .Get(builder.ModelName).ModalLoader;
                mlOptions.ServiceProvider = sp;

                mlOptions.CreateModel = (mlContext) =>
                {
                    var model = mlOptions.ModelLoader.LoadAsync(CancellationToken.None).GetAwaiter().GetResult();

                    return(mlContext.Model.Load(model, out var inputSchema));
                };
            });

            return(builder);
        }
        public static IModelPredictionEngineBuilder <TInput, TPrediction> FromAzureBlob <TInput, TPrediction>(
            this IModelPredictionEngineBuilder <TInput, TPrediction> builder,
            string blobContainerName,
            string sectionName     = AzureStorageConstants.DefaultAccount,
            string rootSectionName = AzureStorageConstants.AzureStorage,
            Action <StorageAccountOptions>?configureStorage = null,
            Action <ModelLoderFileOptions>?configure        = default) where TInput : class
            where TPrediction : class, new()
        {
            builder.Services.AddAzureStorageAccount(builder.ModelName, sectionName, rootSectionName, configureStorage)
            .AddAzureBlobContainer(builder.ModelName, blobContainerName);

            builder.From <TInput, TPrediction, AzureStorageModelLoader>(configure);

            return(builder);
        }
        private static IModelPredictionEngineBuilder <TInput, TPrediction> AddModelLoader <TInput, TPrediction, TLoader>(
            this IModelPredictionEngineBuilder <TInput, TPrediction> builder,
            Action <ModelLoderFileOptions>?configure = null,
            ServiceLifetime serviceLifetime          = ServiceLifetime.Transient)
            where TInput : class
            where TPrediction : class, new()
            where TLoader : ModelLoader
        {
            // adds model loader to DI
            builder.Services.TryAdd(ServiceDescriptor.Describe(typeof(TLoader), typeof(TLoader), serviceLifetime));

            // adds configuration for model file and model result file
            builder.Services.Configure <ModelLoderFileOptions>(
                builder.ModelName,
                options =>
            {
                options.ModelName           = builder.ModelName;
                options.ModelResultFileName = $"{options.ModelName}.json";
                options.ModelFileName       = $"{options.ModelName}.zip";

                // overrides the defaults
                configure?.Invoke(options);
            });

            // adds model loader options to be used.
            builder.Services.AddOptions <ModelLoaderOptions>(builder.ModelName)
            .Configure <IServiceProvider, TLoader>(
                (options, sp, loader) =>
            {
                var setupOptions = sp.GetRequiredService <IOptionsMonitor <ModelLoderFileOptions> >().Get(builder.ModelName);
                loader.Setup(setupOptions);

                options.ModalLoader = loader;
            });
            return(builder);
        }