Exception: Must have input rank for 165 in order to convert axis for NHWC op

Hi, I encounter this problem when I try to import a custom trained yolov3-tiny model into Unity.
I am using Barracuda 2.4.0
and the model is convert into onnx from pytorch file(.pth)

Exception: Must have input rank for 165 in order to convert axis for NHWC op
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.ConvertAxis (Unity.Barracuda.Layer layer, Unity.Barracuda.ModelBuilder net) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWC/RewriterNCHWToNHWC.cs:467)
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.Rewrite (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWCPass.cs:139)
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.Run (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWCPass.cs:39)
Unity.Barracuda.Compiler.Passes.IntermediateToRunnableNHWCPass.Run (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/Core/Compiler/Passes/IntermediateToRunnableNHWCPass.cs:38)
Unity.Barracuda.ONNX.ONNXModelConverter.Convert (Google.Protobuf.CodedInputStream inputStream) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/ONNX/ONNXModelConverter.cs:188)
Unity.Barracuda.ONNX.ONNXModelConverter.Convert (System.String filePath) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Runtime/ONNX/ONNXModelConverter.cs:98)
Unity.Barracuda.ONNXModelImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) (at Library/PackageCache/com.unity.barracuda@b1eac6c34b/Barracuda/Editor/ONNXModelImporter.cs:65)
UnityEditor.AssetImporters.ScriptedImporter.GenerateAssetData (UnityEditor.AssetImporters.AssetImportContext ctx) (at <55729f52d042492e9efc384182ae2feb>:0)
UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

If anybody can give me any suggestion on how to fix it, that would be great.

Thanks in advance.

1 Like

Bumping this.
I am having the same issue with another NN and have tried using different opset versions, with no avail.

Update: I have found the issue, but it could be a bug on Barracuda’s side.

The asset import error disappeared after exporting the ONNX model without the dynamic_axes parameter in PyTorch’s torch.onnx.export() function. The model works perfectly fine with constant size inputs (the ones sepcified while exporting). That’s confusing, considering the Barracuda docs clearly state that dynamic input sizes and shapes are supported.
Maybe it’s a bug on Barracuda’s side?

We are more robust with constant input size. Dynamic input sizes are supported, but can cause some problems for us right now. But we are working on making that more flexible.
So it is probably a bug on our side. I’d recommend sticking to fixed input size for now until we release the dynamic shape handling improvements

Thanks for the quick reply!
Definitely looking forward to those improvements since my trained model is specifically made with flexible input size in mind, so getting to use that would be sweet.

I am using a Barracuda 3.0.0 and I encounter the same problem when I try to import a custom model into Unity.

Exception: Must have input rank for 169 in order to convert Reshape to NHWC
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.b__4_0 (Unity.Barracuda.Layer layer, Unity.Barracuda.ModelBuilder net) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWC/RewriterNCHWToNHWC.cs:80)
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.Rewrite (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWCPass.cs:139)
Unity.Barracuda.Compiler.Passes.NCHWToNHWCPass.Run (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/Core/Compiler/Passes/NCHWToNHWCPass.cs:39)
Unity.Barracuda.Compiler.Passes.IntermediateToRunnableNHWCPass.Run (Unity.Barracuda.Model& model) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/Core/Compiler/Passes/IntermediateToRunnableNHWCPass.cs:38)
Unity.Barracuda.ONNX.ONNXModelConverter.Convert (Google.Protobuf.CodedInputStream inputStream) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/ONNX/ONNXModelConverter.cs:188)
Unity.Barracuda.ONNX.ONNXModelConverter.Convert (System.String filePath) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Runtime/ONNX/ONNXModelConverter.cs:98)
Unity.Barracuda.ONNXModelImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) (at Library/PackageCache/com.unity.barracuda@3.0.0/Barracuda/Editor/ONNXModelImporter.cs:65)
UnityEditor.AssetImporters.ScriptedImporter.GenerateAssetData (UnityEditor.AssetImporters.AssetImportContext ctx) (at <5456dbfeccf644f7ab5b4d4a01ccf33e>:0)
UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

My input size is fixed,that is (1,2121), but the net include some operators about shape in Transformer. I found that if the tensors don’t go through the attention layer,the import process of onnx file is ok. How to figure out this issue?

Hi,

I recommend testing this with the Sentis package, as it’s a newer version of Barracuda and there’s a high chance that the issue is already fixed there.

If you still manage to reproduce the issue with Sentis, please write about it in the Discussion forum Sentis category and we will investigate it further.

More about Sentis: About Sentis Beta