ML.Net Invalid format when loading model

43 Views Asked by At

I'm trying to implement an ML.Net system to predict prices and I'm getting a strange error when loading the stored model.

This is my code for the training:

private async Task TrainModel()
{
    var historicPrices = await _priceRepository.GetPricesByQueryAsync(x => !x.Processed);

    var mlContext = new MLContext(seed: 0); 

    var newData = mlContext.Data.LoadFromEnumerable(historicPrices.Select(p => new PriceForML
    {
        Value = (float)p.Value
    }).ToList());

    var trainer = mlContext.Forecasting.ForecastBySsa(
    outputColumnName: nameof(ForecastResult.Forecast),
    inputColumnName: nameof(PriceForML.Value),
    windowSize: 60,
    seriesLength: 60 * 60 * 24,
    trainSize: 60 * 60 * 24,
    horizon: 5,
    confidenceLevel: 0.95f,
    confidenceLowerBoundColumn: "ConfidenceLowerBound",
    confidenceUpperBoundColumn: "ConfidenceUpperBound");

   ITransformer trainedModel = trainer.Fit(newData);
   mlContext.Model.Save(trainedModel, newData.Schema, $"MLModels/MinuteModel.zip");
}

Those are the models that hold the data:

    public class PriceForML
    {
        public float Value { get; set; }
    }
    public class ForecastResult
    {
        public float[] Forecast { get; set; }
    }

This is the code of the prediction generator:

 private async Task MakePredictionsMinuteAsync()
{         
            var lastPrice = await _priceRepository.GetLatestPriceByCoinIdAsync(coin.Id);
            if (File.Exists($"MLModels/MinuteModel.zip") && lastPrice != null)
            {                 
                var mlContext = new MLContext(seed: 0);
                ITransformer forecaster;
                try
                {
                    await using (var file = File.OpenRead($"MLModels/MinuteModel.zip"))
                    {
                        forecaster = mlContext.Model.Load(file, out DataViewSchema schema);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                    continue;
                }

                var predictions = forecaster.CreateTimeSeriesEngine<PriceForML, ForecastResult>(mlContext).Predict(new PriceForML() { Value = (float)lastPrice.Value });
                if(predictions.Forecast == null) continue;
                var predictionTime = DateTime.UtcNow;
                var predictionsToSave = new List<Prediction>();
                for (int i = 1; i < predictions.Forecast.Length; i++)
                {
                    var predictionDT = predictionTime.AddMinutes(i);
                    var prediction = new Prediction
                    {
                        CoinId = coin.Id,
                        PredictedValue = (decimal)predictions.Forecast[i],
                        PredictionDateTime = new DateTime(predictionDT.Year, predictionDT.Month, predictionDT.Day, predictionDT.Hour, predictionDT.Minute, 0),
                        Interval = IntervalTypes.Minute,
                        CreatedDate = DateTime.UtcNow
                    };
                    predictionsToSave.Add(prediction);
                }
                await _predictionRepository.SetPredictionsAsync(predictionsToSave);
            }
            
        }

I trained the model with 60 entries, and it generated the .zip "correctly". But when I try to create predictions, and it reaches the line

forecaster = mlContext.Model.Load(file, out DataViewSchema schema);

It throws the following exception:

2023-11-15 16:15:23.6143|0|ERROR|Microsoft.Extensions.Hosting.Internal.Host|BackgroundService failed System.InvalidOperationException: Error during class instantiation
 ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: Error during class instantiation
 ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.FormatException: One of the identified items was in an invalid format.
   at Microsoft.ML.Transforms.TimeSeries.AdaptiveSingularSpectrumSequenceModelerInternal..ctor(IHostEnvironment env, ModelLoadContext ctx)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
   --- End of inner exception stack trace ---
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes](IHostEnvironment env, Type signatureType, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig](IHostEnvironment env, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig](IHostEnvironment env, TRes& result, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, String name, Object[] extra)
   at Microsoft.ML.Transforms.TimeSeries.SsaForecastingBaseWrapper.SsaForecastingBase..ctor(IHostEnvironment env, ModelLoadContext ctx, String name)
   at Microsoft.ML.Transforms.TimeSeries.SsaForecastingTransformer.Create(IHostEnvironment env, ModelLoadContext ctx)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
   --- End of inner exception stack trace ---
   at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes](IHostEnvironment env, Type signatureType, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig](IHostEnvironment env, TRes& result, String name, String options, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig](IHostEnvironment env, TRes& result, Object[] extra)
   at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
   at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
   at Microsoft.ML.ModelOperationsCatalog.Load(Stream stream, DataViewSchema& inputSchema)
   at Midas.API.Services.PitiasService.MakePredictionsMinuteAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 460
   at Midas.API.Services.PitiasService.MakePredictionsMinuteAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 461
   at Midas.API.Services.PitiasService.MakePredictionsAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 58
   at Midas.API.Services.OrchestratorService.TrainModelAndMakePredictions() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\OrchestratorService.cs:line 70
   at Midas.API.Services.OrchestratorService.ExecuteAsync(CancellationToken stoppingToken) in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\OrchestratorService.cs:line 40
   at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)

Does anyone have any idea which "item" is the one the exception talks about ("One of the identified items was in an invalid format.")? OR how can I find out what's going on?

Thank you so much for the help.

0

There are 0 best solutions below