machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

v0.6 Simple ONNX model initialization failed

Open jilongliao opened this issue 8 years ago • 10 comments

System information

  • OS version/distro: Windows 10 17134.345
  • .NET Version (eg., dotnet --info): 2.1.402

Issue

  • What did you do? I tried to load a ONNX model that generated by Python onnxmltools and want to load the onnx model file, then apply to my test data.
  • What happened? I got exceptions System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
  • What did you expect? The model could be loaded and scored.

Source code / logs

Please paste or attach the code or logs or traces that would be helpful to diagnose the issue you are reporting.

public class OnnxRunner
    {
        public class InputData
        {
            [VectorType(10)]
            public float[] data_0;
        }

        public class PredictionOutput
        {
            [ColumnName("label")]
            public uint Label { get; set; }
            [ColumnName("probabilities")]
            public float[] Probabilities { get; set; }
        }

        public void Run()
        {
            using (var env = new ConsoleEnvironment(null, false, 0, 1, null, null))
            {
                var dataPath = Path.Combine(Environment.CurrentDirectory, "sample_test_data.csv");
                var modelPath = Path.Combine(Environment.CurrentDirectory, "sample.onnx");
                var outputPath = Path.Combine(Environment.CurrentDirectory, "output.txt");

                var inputData = File.ReadLines(dataPath).Skip(1).Select(row => new InputData
                {
                    data_0 = row.Split(new char[] {  '\t' }).Select(token => float.Parse(token)).ToArray()
                }).ToList();

                var inputName = "data_0";
                var outputName = "label";

                var data = ComponentCreation.CreateDataView<InputData>(env, inputData);
                var pipeline = new OnnxEstimator(env, modelPath, inputName, outputName);
                var transformer = pipeline.Fit(data);
                var result = transformer.Transform(data);
            }
        }
    }

Sample ONNX file and data are here: sample.zip

I tracked the exception that it throws from here: https://github.com/dotnet/machinelearning/blob/06b5ea6252e2c2766045d5f1436c55c1ccd8929d/src/Microsoft.ML.OnnxTransform/OnnxUtils.cs#L178

The ONNX file is generated by Python onnxmltools 1.2.2.129, the classifier in the model is lightgbm multi class classifier (v2.2.1)

jilongliao avatar Oct 19 '18 23:10 jilongliao

@jignparm @vaeksare

Ivanidzo4ka avatar Oct 22 '18 19:10 Ivanidzo4ka

@jignparm , could you please look at this bug? It looks well defined.

Zruty0 avatar Oct 22 '18 20:10 Zruty0

@little-eyes -- sorry for the way too untimely response, and thanks for the sample file. I was able to repro the issue in the underlying library that MLNet relies on.

The OnnxTransform at this time only supports Onnx 1.2 opset. There is likely an operator mismatch (in version), or a model conversion problem which prevents the model from running against the underlying library.

I'll follow up with you offline regarding this-- the root cause/fix would need to be in the onnx runtime library.

jignparm avatar Nov 03 '18 01:11 jignparm

This is a limitation of the Microsoft.ML.Scoring underlying library – currently the API only supports graphs where the inputs and outputs are Tensor types.

One of the outputs of the model is of type = ‘Sequence’, and there’s no API in the library to process this type yet.

The upcoming release of the OnnxTransform will depend on the newer OnnxRuntime library, which will have better error messages, so you’ll get a clearer message if something is not supported.

For a quick fix, if you can switch the output to tensor instead of sequence, the model should run fine -- until sequence and map types are supported in both the OnnxTransform as well as OnnxRuntime library.

outputpic

jignparm avatar Nov 09 '18 04:11 jignparm

@jignparm, does #1717 fix this issue?

shmoradims avatar Nov 28 '18 22:11 shmoradims

This is a design question -- "how to consume complex sequences and maps in MLNet?". Tensor types have fixed dimensions, and therefore we can determine the output schema accurately. If a transform emits sequence<map<type_A, type_B>>, where types A or B can again be sequences or maps, then what does the MLNet schema look like? Also, which elements are rows, which are columns -- those semantics need to be auto-inferred.

There's a separate issue for basic OnnxRuntime library functionality here https://github.com/Microsoft/onnxruntime/issues/156.

jignparm avatar Dec 12 '18 18:12 jignparm

What people usually do with this map? Without knowing answer I and for simple plain case I would suggest to map it to following column: ColumnType: R4 and it contains values. Column has metadata SlotNames which is array of casted to string keys.

Ivanidzo4ka avatar Dec 12 '18 18:12 Ivanidzo4ka

Column has metadata SlotNames which is array of casted to string keys.

The solution above works if the values are primitive types (such as 'float' or 'int'). This is less likely to work when the values are complex types (e.g. another map, or sequence of maps). For example, if we had Sequence<Map<int, float>>, where the sequence can be arbitrarily long (in the case of this particular model, the length of the Sequence was 1, but another model have been longer), which values should be converted to columns (and which ones should be rows)?

It seems like we need an algorithm (or a language) which can map an N-dimensional data structure into a 2-D sparse matrix (which is what MLNET is able to consume easily).

jignparm avatar Dec 13 '18 18:12 jignparm

@jignparm can you please update the issue with progress.

shauheen avatar Jan 08 '19 19:01 shauheen

Tracking here -> https://github.com/Microsoft/onnxruntime/issues/156

The underlying C-API is being planned to support non-Tensor types, which will surface in C#.

jignparm avatar Jan 25 '19 03:01 jignparm