Skip to content

CreatePredictionEngine overrides inputSchemaDefinition by calling into DataViewConstructionUtils.GetSchemaDefinition #3234

@codemzs

Description

@codemzs

It seems CreatePredictionEngine overrides inputSchemaDefinition by calling into DataViewConstructionUtils.GetSchemaDefinition. This is problematic if some of the features of input schema is determined at runtime, example, length of feature vector.

            ITransformer trainedModel;
            using (var stream = new FileStream(modelFileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
               var trainedModel = mlContext.Model.Load(stream, out var inputSchema);
               var = mlContext.Model.CreatePredictionEngine<TInput, TOutput>(trainedModel, inputSchemaDefinition: inputSchema); 

//Throws an exception features<vector<single>> does not match features<vector<single, 432>> because CreatePredictionEngine returns transformer.CreatePredictionEngine<TSrc, TDst>(_env, false, DataViewConstructionUtils.GetSchemaDefinition<TSrc>(_env, inputSchema)); and `DataViewConstructionUtils.GetSchemaDefinition` overrides inputSchema.

            }

//Below workaround that overrides the override!

using (var stream = new FileStream(modelFileFullPath", FileMode.Open, FileAccess.Read, FileShare.Read))
            {

                var trainedModel = mlContext.Model.Load(stream, out var inputSchema);
                var outputSchemaDefinition = SchemaDefinition.Create(typeof(TInput));
                outputSchemaDefinition["Features"].ColumnType = new VectorDataViewType(NumberDataViewType.Single, Convert.ToInt32(432));
                var p = mlContext.Model.CreatePredictionEngine<TInput, TOutput>(trainedModel, inputSchemaDefinition: outputSchemaDefinition);
            }

CC: @TomFinley

Metadata

Metadata

Labels

P1Priority of the issue for triage purpose: Needs to be fixed soon.bugSomething isn't workingloadsaveBugs related loading and saving data or models

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions