BobLd / YOLOv4MLNet

Use the YOLO v4 and v5 (ONNX) models for object detection in C# using ML.Net
MIT License
79 stars 31 forks source link

YoloV5 C# detection is less accurate #10

Closed afisk closed 3 years ago

afisk commented 3 years ago

I am finding that the object detection is different and maybe less accurate in C# with the onnx model than it was in Python with the PyTorch model. Is there any fix or reason for this? I have resized my images to be 640x640 to hopefully avoid any differences in image resizing. I also have the same confidence and IOU thresholds in both and get different results.

C#: 623 _pro Python: 623

C#: 656 _pro Python: 656

Any idea why the Python & PyTorch model seems to preform better?

BobLd commented 3 years ago

I can't really answer on the PyTorch/onnx difference as I'm no specialist, but I would expect the models to behave similarly... (also depending on how you did the conversion)

Below are stuffs I would invertigate:

Hope this helps

EDIT: I've just noticed that's you've edited your post and previously display some positive resutlts from the C#/onnx model and the scores were already differents. This makes me think that there is a difference in the post-processing step. Have a look at how the NMS is done in the Python code and maybe play with the scoreThres and iouThres parameters in the GetResults() function:

public IReadOnlyList<YoloV5Result> GetResults(string[] categories, float scoreThres = 0.5f, float iouThres = 0.5f)
afisk commented 3 years ago

I turned off the NMS (never let it delete a box) and it appears that it is still not finding the images.. do you think the .pt to onnx converter might be bad? I used the one that came with the ultralytics/yolov5 .

C# NMS at .45 - Finds right side deer 629 _pro C# no NML - finds right side deer 629 _proc Python .45 NMS - finds both deer 629

BobLd commented 3 years ago

okay that's strange... The scores are still different for the correctly found bounding box (0.88 in C# vs. 0.94 in Python)

afisk commented 3 years ago

This is pretty much every image with the threshold turned off: 621 _proc

The pre-processing looks okay to me. There should be no resizing since I am feeding in the size of the model's inputs, 640x640. There is scaling: img/255. Python is nice and does most of the hard work and thinking for you :)

I'm not married to YoloV5 so maybe it would be easier to switch to YoloV4. With your YoloV4, how were you creating your onnx files and were all of your objects being found both in the original YoloV4 model and the Onnx?

BobLd commented 3 years ago

haha quite messy

afisk commented 3 years ago

Darn. It starts detecting the second deer once the confidence score gets below 0.0003: 629 _proc

I'll go look through that YoloV3 and see if I can find something useful in that too! :)

afisk commented 3 years ago

YOU WERE RIGHT! Very close!! :) :) :) My pytorch model output is 10647x17classes (for 416x416 images ((52 x 52) + (26 x 26) + 13 x 13)) x 3 = 10647).. the converted model (onnx) has an output size of 25200x17 classes. I converted the pt to onnx converter to take in 416x416 images. The drawings are a little off, but the confidences are almost on point and the boxes show up in the relative areas of where the objects are in relation to one another. I'll try and get the bounding boxes to show in the correct places tomorrow and update here. Thank you very very much for all of your help (and encouragement) so far!!

C# 416x416: 629 _proc Python Original Model: 629

C# 416x416: 656 _proc Python Original Model: 656

BobLd commented 3 years ago

That's great news! This now seems to be a scaling issue. Just checking but make sure you've updated the following to match your dimensions: https://github.com/BobLd/YOLOv4MLNet/blob/0104728917e3001e5b04feb794c3ffcb6dd8a819/YOLOv4MLNet/DataStructures/YoloV4Prediction.cs#L31-L32

afisk commented 3 years ago

That was it! Works great now!! Thank you sooo much @BobLd !

BobLd commented 3 years ago

Great!