How do i resolve the error "Cannot reshape array of size 4145280 into shape with multiple of 41472 elements" while i am trying to inference from an ONNX model

I tried running this unity project on my system - GitHub - tapiralec/UnityInferenceTutorial: A quick start to using machine learning in Unity with Barracuda
which involves predicting the handwritten image. you can refer this video - Intro to Machine Learning in Unity with Barracuda - YouTube

After the image upon uploading in the script predicted the correct number, I thought i could use my own image classification model and convert it into onnx and execute the same

  • this is the onnx model - model_sev_onnx.onnx - Google Drive

  • this is the complete log of error after i hit play button - ArgumentException: Cannot reshape array of size 4145280 into shape with multiple of 41472 elements
    Unity.Barracuda.TensorExtensions.Reshape (Unity.Barracuda.TensorShape shape, System.Int32 size4Dor8D) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/TensorExtensions.cs:579)
    Unity.Barracuda.ModelAnalyzer.ListTemporaryTensorShapes (Unity.Barracuda.Model model, System.Collections.Generic.IDictionary2[TKey,TValue] inputShapes, System.Collections.Generic.IDictionary2[System.String,System.Nullable`1[Unity.Barracuda.TensorShape]]& shapesByName) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/ModelAnalyzer.cs:432)
    Unity.Barracuda.ModelAnalyzer.ListTemporaryTensorShapes (Unity.Barracuda.Model model, System.Collections.Generic.IDictionary`2[TKey,TValue] inputShapes) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/ModelAnalyzer.cs:70)
    Unity.Barracuda.ModelAnalyzer.FindLargestNecessaryTensorShape (Unity.Barracuda.Model model, System.Collections.Generic.IDictionary`2[TKey,TValue] inputShapes) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/ModelAnalyzer.cs:736)
    Unity.Barracuda.GenericVarsWithPreallocation.PrepareStorage (Unity.Barracuda.Model model, Unity.Barracuda.IOps ops, System.Collections.Generic.IDictionary`2[TKey,TValue] inputShapes, System.Boolean takeoverWeights, Unity.Barracuda.DataType dataType) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/GenericWorker.cs:1748)
    Unity.Barracuda.GenericWorker.PrepareForInput (System.Collections.Generic.IDictionary`2[TKey,TValue] inputShapes, Unity.Barracuda.DataType dataType) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/GenericWorker.cs:139)
    Unity.Barracuda.GenericWorker.SetInput (System.String name, Unity.Barracuda.Tensor x) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/GenericWorker.cs:155)
    Unity.Barracuda.GenericWorker.SetInput (Unity.Barracuda.Tensor x) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/GenericWorker.cs:164)
    Unity.Barracuda.GenericWorker.Execute (Unity.Barracuda.Tensor input) (at Library/PackageCache/com.unity.barracuda@80909e3320/Barracuda/Runtime/Core/Backends/GenericWorker.cs:178)
    GetInferenceFromModel.Update () (at Assets/Scripts/GetInferenceFromModel.cs:63)

Well, to be honest it’s hard to tell what’s actually going on here. Though just based on the numbers in the error, the value 4145280 is not divisible by 41472. It’s just shy by 1920 to decompose into 41472 * 100. Currently it’s not 100 but 99.9537…

That 1920 looks a lot like an image width? Maybe you have your height setup wrong and it is missing a single line? Though that’s just a wild guess :slight_smile: