Skip to content

API for loading ONNX model lacks access to input schema and is not consistent with regular model loading API #4335

Open

Description

@Dmitry-A

Current API for loading models is as follows:
ITransformer mlModel = mlContext.Model.Load(path, out DataViewSchema inputSchema);

Note you have a transformer back and the input schema, plus transformer gives access to output schema as well.
For ONNX the same functionality will look something like this:
var estimator = mlContext.Transforms.ApplyOnnxModel(path);
var dataView = mlContext.Data.LoadFromEnumerable(new ModelInput[] { });
// Fit() will check the input schema of the model against the input dataview you're passing in
var transformer = estimator.Fit(dataView);

Note there's no way to get the input schema, you have to know it before you can do anything with the model. Also Fit() call is confusing and inconsistent with the much cleaner API above, it doesn't actually do anything other than verify the [unavailable from public interface] input schema against the schema that got loaded with the model.

The fix is to bring ONNX model loading API in line with what we have for regular ML.NET models.

Activity

  1. eerhardt commented on Oct 15, 2019

    @eerhardt
    Member

    I assume an .onnx model file contains the expected input schema, correct? Is there a way we can programmatically create a DataViewSchema given an .onnx file? Inspect all the input types, and build a DataViewSchema from it.

    I totally agree that we should have a higher-level ONNX API here that doesn't necessarily fit into the base Estimator-Transformer programming model. That programming model is necessary when you want the Onnx model to be just a single step in the pipeline. But if your whole pipeline is contained in the Onnx model, there is no reason to go through the Estimator to get a transformer. Instead, we could have:

    OnnxTransformer mlModel = mlContext.Model.LoadOnnx(path, out DataViewSchema inputSchema);
    
    // use `mlModel` (which is an `ITransformer`) as normal, including creating a PredictionEngine<TIn, TOut> around it

    This would eliminate the need for going through an Estimator, and then Fiting it with an empty data view.

  2. Dmitry-A commented on Oct 15, 2019

    @Dmitry-A
    ContributorAuthor

    I actually get what I need by using reflection from the OnnxTransformer instance, it was pretty painful to do but it works, my hope is I won't need any of that hackery once this issue is addressed. I can't say you can get a DataViewSchema in general case with complete confidence, but from what I've seen yes it should be pretty straightforward.

  3. matouskozak commented on Sep 1, 2026

    @matouskozak
    Member

    Triage: The feature request looks valuable but it is not a current priority for the team.

  4. added this to the ML.NET Future milestone on Sep 1, 2026
  5. removed
    bugSomething isn't working
    on Sep 1, 2026
  6. added
    Priority:2Work that is important, but not critical for the release
    area-ONNXONNX model export and inference
    and removed on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:2Work that is important, but not critical for the releasearea-ONNXONNX model export and inferenceyellowmaintenance only

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions