Skip to content

Using the wrong type with OnnxSequenceType throws an non-helpful message #4122

@eerhardt

Description

@eerhardt

We should give the user a better exception message when they use the wrong type in their class vs. the type in an ONNX model. The current exception doesn't help point them in the right direction and only confuses them.

If you pull the following code:

https://github.com/sethjuarez/SeeSharp/tree/c864892252880d4ef87006008823dee3b29adf9c

And change the code to be:

    public class ImagePrediction
    {
        [ColumnName("classLabel")]
        [VectorType]
        public string[] Prediction;

        [OnnxSequenceType(typeof(IEnumerable<float>))]
        public IEnumerable<float> loss;
    }

    public class ImageInput
    {
        [ImageType(224, 224)]
        public Bitmap Image { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var modelFile = Directory.EnumerateFiles(".", "*.onnx").FirstOrDefault();
            Console.WriteLine($"Attempting to load {modelFile}...");
            var predictor = LoadModel(modelFile);


            Console.WriteLine("\n\nReading Folder...");
            foreach (var file in Directory.EnumerateFiles("images", "*.jpg"))
            {
                Console.WriteLine(file);
                var output = predictor.Predict(new ImageInput { Image = (Bitmap)Image.FromFile(file) });
                Console.WriteLine($"Label: {output.Prediction[0]}");
                var loss = output.loss.FirstOrDefault();
                Console.WriteLine($"\t{loss}");
                Console.WriteLine("------------------------------------\n\n");
            }
            
            Console.ReadKey();
        }

You get the following exception:

Unhandled Exception: System.ArgumentOutOfRangeException: Could not determine an IDataView type for member loss
Parameter name: rawType
   at Microsoft.ML.Data.InternalSchemaDefinition.GetVectorAndItemType(String name, Type rawType, IEnumerable`1 attributes, Boolean& isVector, Type& itemType)
   at Microsoft.ML.Data.SchemaDefinition.Create(Type userType, Direction direction)
   at Microsoft.ML.Data.TypedCursorable`1.Create(IHostEnvironment env, IDataView data, Boolean ignoreMissingColumns, SchemaDefinition schemaDefinition)
   at Microsoft.ML.PredictionEngineBase`2.PredictionEngineCore(IHostEnvironment env, InputRow`1 inputRow, IRowToRowMapper mapper, Boolean ignoreMissingColumns, SchemaDefinition outputSchemaDefinition, Action& disposer, IRowReadableAs`1& outputRow)
   at Microsoft.ML.PredictionEngineBase`2..ctor(IHostEnvironment env, ITransformer transformer, Boolean ignoreMissingColumns, SchemaDefinition inputSchemaDefinition, SchemaDefinition outputSchemaDefinition)
   at Microsoft.ML.PredictionEngineExtensions.CreatePredictionEngine[TSrc,TDst](ITransformer transformer, IHostEnvironment env, Boolean ignoreMissingColumns, SchemaDefinition inputSchemaDefinition, SchemaDefinition outputSchemaDefinition)
   at SeeSharp.Program.LoadModel(String onnxModelFilePath) in /Users/eerhardt/git/SeeSharp/SeeSharp/Program.cs:line 72
   at SeeSharp.Program.Main(String[] args) in /Users/eerhardt/git/SeeSharp/SeeSharp/Program.cs:line 38

When the error here is that the Onnx model expects the loss property to be

        [OnnxSequenceType(typeof(IDictionary<string, float>))]
        public IEnumerable<IDictionary<string, float>> loss;

But when you use the wrong type (like I did above), you get this unhelpful exception.

Instead the message should tell you that "The expected type IEnumerable<IDictionary<string, float>> does not match the type of the loss property: IEnumerable<float>. Please change the loss property to IEnumerable<IDictionary<string, float>>"

/cc @wschin

Metadata

Metadata

Assignees

Labels

P1Priority of the issue for triage purpose: Needs to be fixed soon.imageBugs related image datatype tasksloadsaveBugs related loading and saving data or modelsonnxExporting ONNX models or loading ONNX 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