Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with a custom trained yolov5 ONNX model #6

Closed
HamidEbr opened this issue Feb 7, 2021 · 6 comments
Closed

Problem with a custom trained yolov5 ONNX model #6

HamidEbr opened this issue Feb 7, 2021 · 6 comments

Comments

@HamidEbr
Copy link

HamidEbr commented Feb 7, 2021

Hi, thanks for providing a source code for yolov5, I, ve tried it and it works for me with yolov5 default ONNX files.
I trained a custom model with PyTorch (.pt) and converted it into ONNX.
I try to change your code parameters to my own ONNX file by open it on Netron app.
My model's input type is type: float32[16,3,640,640] and output is type: float32[16,3,80,80,33], that is not same dimension of yolov5s_full_layer output (float32[1,25200,85]) and also if I try to run code it throws an exception like this at var model = pipeline.Fit(mlContext.Data.LoadFromEnumerable(new List<YoloV4BitmapData>())); code:

System.InvalidOperationException: 'Input shape mismatch: Input 'images' has shape 16,3,640,640, but input data is of length 1228800.'

Please help me to do the right changes.

@BobLd
Copy link
Owner

BobLd commented Feb 7, 2021

Concerning your Error message

If the inputs and ouputs layers are properly defined in your ONNX, I would suggest removing or commenting the following lines (keeping in mind that your issue comes from the definition of the images input shape):

shapeDictionary: new Dictionary<string, int[]>()
{
{ "images", new[] { 1, 3, 640, 640 } },
{ "output", new[] { 1, 25200, 85 } },
},

If this doesn't work, you need to investigate further:

  • According to the error message, it seems the input layer of your model is actually 1,3,640,640 (and not 16,3,640,640). This wouldn't be a surprise as the first parameter is the batch size, and ONNX converters tend to set to 1 (even if your original model size is 16)
  • Can you share a screenshot of your ONNX (and not PyTorch) in Neutron?

Concerning the differences in output layers

Please see my #2 (comment), I think the difference is related to the Detect() method being (or not being - I don't remember) in your ONNX model.
Few comments:

  • Given your model output shape 16,3,80,80,33, I doubt you have only one output layer (I'd guess you have 3, with shapes: 16,3,20,20,33, 16,3,40,40,33 and 16,3,80,80,33)
  • Again, please share a screenshot of your ONNX model in Neutron so that we can see the input and output layers

@HamidEbr
Copy link
Author

HamidEbr commented Feb 7, 2021

Thanks, Here is the screenshot of Netron preview:
image

I updated my code to that you mentioned, but this time throws another exception:

image

@BobLd
Copy link
Owner

BobLd commented Feb 7, 2021

The new error comes from the fact that your model expect 16 images as input (batch size is 16) and you only input 1 image (input data is of size 1,228,800 = 1 x 3 x 640 x 640).

Try changing the batch size when you export your model to 1, and change the batch to 1 in the C# code (or you could try to input 16 images in the C# code, but I don't know how to do that).

Concerning your ouput layers, you indeed have 3:

@HamidEbr
Copy link
Author

HamidEbr commented Feb 7, 2021

Thanks for your help, I check it and tell you,
What can I do for this part:

image

I know 25200 came from your model output but the dimension of it is different than my model.

@HamidEbr
Copy link
Author

HamidEbr commented Feb 7, 2021

I convert pt file again this time into an ONNX file with batch size = 1, this time I've no exception for my model.
Now I don't have any prediction I think because of predCell indexes or objConf value. so, is there any changes if my class count (28) is deferent that you (80) because of objConf value always Is a negative number?
I check run of your model and don't see any result of negative, but in my model most of the results are negative!

@HamidEbr
Copy link
Author

HamidEbr commented Feb 8, 2021

At last, with help of this link
Solved!

@HamidEbr HamidEbr closed this as completed Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants